OLD | NEW |
---|---|
(Empty) | |
1 <h1>Bluetooth</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="manifest">Manifest requirements</h2> | |
14 | |
15 <p> | |
16 For Chrome Apps that use Bluetooth, add the | |
17 <a href="manifest/bluetooth.html">bluetooth</a> entry to the manifest | |
18 and specify, if appropriate, the UUIDs of profiles you wish to implement. | |
19 For example: | |
20 </p> | |
21 | |
22 <pre data-filename="manifest.json"> | |
23 "bluetooth": { | |
24 "profiles": [ "1105", "1106" ] | |
armansito
2014/03/09 05:51:33
We do allow specifying 128-bit UUIDs in the API, r
keybuk
2014/03/09 08:03:45
We only currently support 16-bit UUIDs - crbug.com
armansito
2014/03/09 18:38:10
Oh, that's right. If someone DOES go and put in a
| |
25 } | |
26 </pre> | |
27 | |
28 <p> | |
29 To only access adapter state, discover nearby devices and obtain basic | |
30 information about devices, omit the <code>profiles</code> list. | |
31 </p> | |
32 | |
33 <h2 id="adapter_state">Obtaining adapter state</h2> | |
34 | |
35 <p> | |
36 To obtain the state of the Bluetooth adapter, use the | |
37 <code>chrome.bluetooth.getAdapterState</code> method: | |
38 </p> | |
39 | |
40 <pre> | |
41 chrome.bluetooth.getAdapterState(function(adapter) { | |
42 console.log("Adapter " + adapter.address + ": " + adapter.name); | |
43 }); | |
44 </pre> | |
45 | |
46 <p> | |
47 The <code>chrome.bluetooth.onAdapterStateChanged</code> event is sent | |
48 whenever this state changes. This can be used, for example, to determine when | |
49 the adapter radio is powered on or off. | |
50 </p> | |
51 | |
52 <pre> | |
53 var powered = false; | |
54 chrome.bluetooth.getAdapterState(function(adapter) { | |
55 powered = adapter.powered; | |
56 }); | |
57 | |
58 chrome.bluetooth.onAdapterStateChanged.addListener( | |
59 function(adapter) { | |
60 if (adapter.powered != powered) { | |
61 powered = adapter.powered; | |
62 if (powered) { | |
63 console.log("Adapter radio is on"); | |
64 } else { | |
65 console.log("Adapter radio is off"); | |
66 } | |
67 } | |
68 }); | |
69 </pre> | |
70 | |
71 <h2 id="listing_devices">Listing known devices</h2> | |
72 | |
73 <p> | |
74 To get a list of the devices known to the Bluetooth adapter, use the | |
75 <code>chrome.bluetooth.getDevices</code> method: | |
76 </p> | |
77 | |
78 <pre> | |
79 chrome.bluetooth.getDevices(function(devices) { | |
80 for (var i = 0; i < devices.length; i++) { | |
81 console.log(devices[i].address); | |
82 } | |
83 }); | |
84 </pre> | |
85 | |
86 <p> | |
87 All devices are returned, including paired devices and devices recently | |
88 discovered. It will not begin discovery of new devices. | |
89 </p> | |
90 | |
91 <h2 id="device_notifications">Receiving device notifications</h2> | |
92 | |
93 <p> | |
94 Instead of repeatedly calling <code>chrome.bluetooth.getDevices</code>, you | |
95 can use the <code>chrome.bluetooth.onDeviceAdded</code>, | |
96 <code>chrome.bluetooth.onDeviceChanged</code> and | |
97 <code>chrome.bluetooth.onDeviceRemoved</code> events to receive notifications. | |
98 </p> | |
99 | |
100 <p> | |
101 The <code>chrome.bluetooth.onDeviceAdded</code> event is sent whenever a | |
102 device is discovered by the adapter or makes a connection to the adapter: | |
103 </p> | |
104 | |
105 <pre> | |
106 chrome.bluetooth.onDeviceAdded.addListener(function(device) { | |
107 console.log(device.address); | |
108 }); | |
109 </pre> | |
110 | |
111 <p> | |
112 Adding a listener for this event does not begin discovery of devices. | |
113 </p> | |
114 | |
115 <p> | |
116 Changes to devices, including previously discovered devices becoming paired, | |
117 are notified by the <code>chrome.bluetooth.onDeviceChanged</code> event: | |
118 </p> | |
119 | |
120 <pre> | |
121 chrome.bluetooth.onDeviceChanged.addListener(function(device) { | |
122 console.log(device.address); | |
123 }); | |
124 </pre> | |
125 | |
126 <p> | |
127 Finally the <code>chrome.bluetooth.onDeviceRemoved</code> event is sent | |
128 whenever a paired device is removed from the system, or a discovered device | |
129 has not been seen recently: | |
130 </p> | |
131 | |
132 <pre> | |
133 chrome.bluetooth.onDeviceRemoved.addListener(function(device) { | |
134 console.log(device.address); | |
135 }); | |
136 </pre> | |
137 | |
138 <h2 id="discovery">Discovering nearby devices</h2> | |
139 | |
140 <p> | |
141 To begin discovery of nearby devices use the | |
142 <code>chrome.bluetooth.startDiscovery</code> method, discovery can be | |
143 resource intensive so you should call | |
144 <code>chrome.bluetooth.stopDiscovery</code> when done. | |
armansito
2014/03/09 05:51:33
I would mention here that the call to startDiscove
keybuk
2014/03/09 16:24:39
Done.
| |
145 </p> | |
146 | |
147 <p> | |
148 Information about each newly discovered device is received using the | |
149 <code>chrome.bluetooth.onDeviceAdded</code> event. For devices that have | |
150 already been discovered recently, or have been previously paired with or | |
151 connected to, the event will not be sent, instead you should call | |
152 <code>chrome.bluetooth.getDevices</code> to obtain the current information, | |
153 and use the <code>chrome.bluetooth.onDeviceChanged</code> event to be notified of changes to that information as a result of discovery. | |
154 </p> | |
155 | |
156 <p> | |
157 Example: | |
158 </p> | |
159 | |
160 <pre> | |
161 var device_names = {}; | |
162 var updateDeviceName = function(device) { | |
163 device_names[device.address] = device.name; | |
164 }; | |
165 var removeDeviceName = function(device) { | |
166 delete device_names[device.address]; | |
167 } | |
168 | |
169 chrome.bluetooth.onDeviceAdded.addListener(updateDeviceName); | |
170 chrome.bluetooth.onDeviceChanged.addListener(updateDeviceName); | |
171 chrome.bluetooth.onDeviceRemoved.addListener(removeDeviceName); | |
172 | |
173 chrome.bluetooth.getDevices(function(devices) { | |
armansito
2014/03/09 05:51:33
The code is pretty self-explanatory, though I woul
keybuk
2014/03/09 16:24:39
Done.
| |
174 for (var i = 0; i < devices.length; i++) { | |
175 updateDeviceName(devices[i]); | |
176 } | |
177 }); | |
178 | |
179 chrome.bluetooth.startDiscovery(function() { | |
180 // Stop discovery after 30 seconds. | |
181 setTimeout(function() { | |
182 chrome.bluetooth.stopDiscovery(function() {}); | |
183 }, 30000); | |
184 }); | |
185 </pre> | |
186 | |
187 <h2 id="identifying_devices">Identifying devices</h2> | |
188 | |
189 <p> | |
190 A number of different options are provided for identifying devices returned | |
191 by <code>chrome.bluetooth.getDevices</code> and the related events. | |
192 </p> | |
193 | |
194 <p> | |
195 If the device supports the Bluetooth | |
196 <a href="https://developer.bluetooth.org/TechnologyOverview/Pages/DI.aspx">Dev ice ID specification</a>, | |
197 several properties are added to the Device object containing the fields | |
198 defined by that specification. Example: | |
199 </p> | |
200 | |
201 <pre> | |
202 chrome.bluetooth.getDevices(function(devices) { | |
203 for (var i = 0; i < devices.length; i++) { | |
204 if (devices[0].vendorIdSource != undefined) { | |
205 console.log(devices[0].address + ' = ' + | |
206 devices[0].vendorIdSource + ':' + | |
207 devices[0].vendorId.toString(16) + ':' + | |
208 devices[0].productId.toString(16) + ':' + | |
209 devices[0].deviceId.toString(16)); | |
210 } | |
211 } | |
212 }); | |
213 </pre> | |
214 | |
215 <p> | |
216 The Device ID specification is usually sufficient to identify a particular | |
217 model, and even revision, of a device from a vendor. Where it is not present | |
218 you must intead rely on information about the class or type of the device, | |
armansito
2014/03/09 05:51:33
s/intead/instead/
keybuk
2014/03/09 16:24:39
Done.
| |
219 optionally combined with the manufacturer prefix in the <code>address</code>. | |
220 </p> | |
221 | |
222 <p> | |
223 Most Bluetooth devices provide Class of Device information as a bit-field | |
224 interpreted according to the | |
225 <a href="https://www.bluetooth.org/en-us/specification/assigned-numbers/baseba nd">Baseband Assigned Numbers</a> | |
226 document. This bit-field is available in the <code>deviceClass</code> | |
227 property. | |
228 </p> | |
229 | |
230 <pre> | |
231 chrome.bluetooth.getDevices(function(devices) { | |
232 for (var i = 0; i < devices.length; i++) { | |
233 if (devices[0].vendorIdSource != undefined) { | |
234 console.log(devices[0].address + ' = ' + | |
235 devices[0].deviceClass.toString(16)); | |
236 } | |
237 } | |
238 }); | |
239 </pre> | |
240 | |
241 <p> | |
242 Parsing the field can be complex so for the most common device types Chrome | |
243 handles this for you and sets the <code>type</code> field. Where this is | |
244 not available, or insufficient for your needs, you'll need to parse the | |
245 <code>deviceClass</code> yourself. | |
246 </p> | |
247 | |
248 <pre> | |
249 chrome.bluetooth.getDevices(function(devices) { | |
250 for (var i = 0; i < devices.length; i++) { | |
251 if (devices[0].vendorIdSource != undefined) { | |
252 console.log(devices[0].address + ' = ' + devices[0].type); | |
253 } | |
254 } | |
255 }); | |
256 </pre> | |
257 | |
OLD | NEW |