OLD | NEW |
---|---|
(Empty) | |
1 <h1>Bluetooth Devices</h1> | |
2 | |
3 <p> | |
4 This document describes how to use the <a href="bluetooth.html">Bluetooth | |
5 API</a> to communicate with Bluetooth and Bluetooth Low Energy devices. | |
6 </p> | |
7 | |
8 <p> | |
9 For background information about Bluetooth, see the official | |
10 <a href="http://www.bluetooth.org">Bluetooth specifications</a>. | |
11 </p> | |
12 | |
13 <h2 id="listing_devices">Listing known devices</h2> | |
14 | |
15 <p> | |
16 To get a list of the devices known to the Bluetooth adapter, use the | |
17 <code>chrome.bluetooth.getDevices</code> method: | |
18 </p> | |
19 | |
20 <pre> | |
21 chrome.bluetooth.getDevices(function(devices) { | |
22 for (var i = 0; i < devices.length; i++) { | |
23 console.log(devices[i].address); | |
24 } | |
25 }); | |
26 </pre> | |
27 | |
28 <p> | |
29 All devices are returned, including paired devices and devices recently | |
30 discovered. It will not begin discovery of new devices. | |
31 </p> | |
32 | |
33 <h2 id="device_notifications">Receiving device notifications</h2> | |
34 | |
35 <p> | |
36 Instead of repeatedly calling <code>chrome.bluetooth.getDevices</code>, you | |
37 can use the <code>chrome.bluetooth.onDeviceAdded</code>, | |
38 <code>chrome.bluetooth.onDeviceChanged</code> and | |
39 <code>chrome.bluetooth.onDeviceRemoved</code> events to receive notifications. | |
40 </p> | |
41 | |
42 <p> | |
43 The <code>chrome.bluetooth.onDeviceAdded</code> event is sent whenever a | |
44 device is discovered by the adapter or makes a connection to the adapter: | |
45 </p> | |
46 | |
47 <pre> | |
48 chrome.bluetooth.onDeviceAdded.addListener(function(device) { | |
49 console.log(device.address); | |
50 }); | |
51 </pre> | |
52 | |
53 <p> | |
54 Adding a listener for this event does not begin discovery of devices. | |
55 </p> | |
56 | |
57 <p> | |
58 Changes to devices, including previously discovered devices becoming paired, | |
59 are notified by the <code>chrome.bluetooth.onDeviceChanged</code> event: | |
60 </p> | |
61 | |
62 <pre> | |
63 chrome.bluetooth.onDeviceChanged.addListener(function(device) { | |
64 console.log(device.address); | |
65 }); | |
66 </pre> | |
67 | |
68 <p> | |
69 Finally the <code>chrome.bluetooth.onDeviceRemoved</code>code> event is sent w henever a paired device is removed from the system, or a discovered device | |
armansito
2014/03/05 21:44:15
Remove the stray tag there (i.e. "</code>code>" ->
keybuk
2014/03/06 21:33:44
Done.
| |
70 has not been seen recently: | |
71 </p> | |
72 | |
73 <pre> | |
74 chrome.bluetooth.onDeviceRemoved.addListener(function(device) { | |
75 console.log(device.address); | |
76 }); | |
77 </pre> | |
78 | |
79 <h2 id="discovery">Discovering nearby devices</h2> | |
80 | |
81 <p> | |
82 To begin discovery of nearby devices use the | |
83 <code>chrome.bluetooth.startDiscovery</code>code> method, discovery can be res ource intensive so you should call | |
armansito
2014/03/05 21:44:15
"</code>code>" -> "</code>"
keybuk
2014/03/06 21:33:44
Done.
| |
84 <code>chrome.bluetooth.stopDiscovery</code> when done. | |
85 </p> | |
86 | |
87 <p> | |
88 Information about each newly discovered device is received using the | |
89 <code>chrome.bluetooth.onDeviceAdded</code> event. For devices that have | |
90 already been discovered recently, or have been previously paired with or | |
91 connected to, the event will not be sent, instead you should call | |
92 <code>chrome.bluetooth.getDevices</code> to obtain the current information, | |
93 and use the <code>chrome.bluetooth.onDeviceChanged</code> event to be notified of changes to that information as a result of discovery. | |
94 </p> | |
95 | |
96 <p> | |
97 Example: | |
98 </p> | |
99 | |
100 <pre> | |
101 var device_names = {}; | |
102 var updateDeviceName = function(device) { | |
103 device_names[device.address] = device.name; | |
104 }; | |
105 var removeDeviceName = function(device) { | |
106 delete device_names[device.address]; | |
107 } | |
108 | |
109 chrome.bluetooth.onDeviceAdded.addListener(updateDeviceName); | |
110 chrome.bluetooth.onDeviceChanged.addListener(updateDeviceName); | |
111 chrome.bluetooth.onDeviceRemoved.addListener(removeDeviceName); | |
112 | |
113 chrome.bluetooth.getDevices(function(devices) { | |
114 for (var i = 0; i < devices.length; i++) { | |
115 updateDeviceName(devices[i]); | |
116 } | |
117 }); | |
118 | |
119 chrome.bluetooth.startDiscovery(function() { | |
120 // Stop discovery after 30 seconds. | |
121 setTimeout(function() { | |
122 chrome.bluetooth.stopDiscovery(function() {}); | |
123 }, 30000); | |
124 }); | |
125 </pre> | |
126 | |
OLD | NEW |