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" ] |
| 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 (see |
| 89 <a href="#discovery">Discovering nearby devices</a>). |
| 90 </p> |
| 91 |
| 92 <h2 id="device_notifications">Receiving device notifications</h2> |
| 93 |
| 94 <p> |
| 95 Instead of repeatedly calling <code>chrome.bluetooth.getDevices</code>, you |
| 96 can use the <code>chrome.bluetooth.onDeviceAdded</code>, |
| 97 <code>chrome.bluetooth.onDeviceChanged</code> and |
| 98 <code>chrome.bluetooth.onDeviceRemoved</code> events to receive notifications. |
| 99 </p> |
| 100 |
| 101 <p> |
| 102 The <code>chrome.bluetooth.onDeviceAdded</code> event is sent whenever a |
| 103 device is discovered by the adapter or makes a connection to the adapter: |
| 104 </p> |
| 105 |
| 106 <pre> |
| 107 chrome.bluetooth.onDeviceAdded.addListener(function(device) { |
| 108 console.log(device.address); |
| 109 }); |
| 110 </pre> |
| 111 |
| 112 <p> |
| 113 Adding a listener for this event does not begin discovery of devices |
| 114 (see <a href="#discovery">Discovering nearby devices</a>). |
| 115 </p> |
| 116 |
| 117 <p> |
| 118 Changes to devices, including previously discovered devices becoming paired, |
| 119 are notified by the <code>chrome.bluetooth.onDeviceChanged</code> event: |
| 120 </p> |
| 121 |
| 122 <pre> |
| 123 chrome.bluetooth.onDeviceChanged.addListener(function(device) { |
| 124 console.log(device.address); |
| 125 }); |
| 126 </pre> |
| 127 |
| 128 <p> |
| 129 Finally the <code>chrome.bluetooth.onDeviceRemoved</code> event is sent |
| 130 whenever a paired device is removed from the system, or a discovered device |
| 131 has not been seen recently: |
| 132 </p> |
| 133 |
| 134 <pre> |
| 135 chrome.bluetooth.onDeviceRemoved.addListener(function(device) { |
| 136 console.log(device.address); |
| 137 }); |
| 138 </pre> |
| 139 |
| 140 <h2 id="discovery">Discovering nearby devices</h2> |
| 141 |
| 142 <p> |
| 143 To begin discovery of nearby devices, use the |
| 144 <code>chrome.bluetooth.startDiscovery</code> method. Discovery can be |
| 145 resource intensive so you should call |
| 146 <code>chrome.bluetooth.stopDiscovery</code> when done. |
| 147 </p> |
| 148 |
| 149 <p> |
| 150 You should call <code>chrome.bletooth.startDiscovery</code> whenever your |
| 151 app needs to discover nearby devices. Do not check the |
| 152 <code>discovering</code> property of <code>AdapterState</code>. The call |
| 153 to start discovery will succeed even if another app is discovering nearby |
| 154 devices, and will ensure the adapter continues to perform discovery after |
| 155 that other app has stopped. |
| 156 </p> |
| 157 |
| 158 <p> |
| 159 Information about each newly discovered device is received using the |
| 160 <code>chrome.bluetooth.onDeviceAdded</code> event. For devices that have |
| 161 already been discovered recently, or have been previously paired with or |
| 162 connected to, the event will not be sent. Instead you should call |
| 163 <code>chrome.bluetooth.getDevices</code> to obtain the current information, |
| 164 and use the <code>chrome.bluetooth.onDeviceChanged</code> event to be notified
of changes to that information as a result of discovery. |
| 165 </p> |
| 166 |
| 167 <p> |
| 168 Example: |
| 169 </p> |
| 170 |
| 171 <pre> |
| 172 var device_names = {}; |
| 173 var updateDeviceName = function(device) { |
| 174 device_names[device.address] = device.name; |
| 175 }; |
| 176 var removeDeviceName = function(device) { |
| 177 delete device_names[device.address]; |
| 178 } |
| 179 |
| 180 // Add listeners to receive newly found devices and updates |
| 181 // to the previously known devices. |
| 182 chrome.bluetooth.onDeviceAdded.addListener(updateDeviceName); |
| 183 chrome.bluetooth.onDeviceChanged.addListener(updateDeviceName); |
| 184 chrome.bluetooth.onDeviceRemoved.addListener(removeDeviceName); |
| 185 |
| 186 // With the listeners in place, get the list of devices found in |
| 187 // previous discovery sessions, or any currently active ones, |
| 188 // along with paired devices. |
| 189 chrome.bluetooth.getDevices(function(devices) { |
| 190 for (var i = 0; i < devices.length; i++) { |
| 191 updateDeviceName(devices[i]); |
| 192 } |
| 193 }); |
| 194 |
| 195 // Now begin the discovery process. |
| 196 chrome.bluetooth.startDiscovery(function() { |
| 197 // Stop discovery after 30 seconds. |
| 198 setTimeout(function() { |
| 199 chrome.bluetooth.stopDiscovery(function() {}); |
| 200 }, 30000); |
| 201 }); |
| 202 </pre> |
| 203 |
| 204 <p> |
| 205 If the user turns off the Bluetooth radio, all discovery sessions will be |
| 206 ended and not resumed automatically when the radio is switched on. If this |
| 207 matters to your app, you should watch the |
| 208 <code>chrome.bluetooth.onAdapterStateChanged</code> event. If the |
| 209 <code>discovering</code> property changes to <code>false</code>, then your app |
| 210 will need to call <code>chrome.bluetooth.startDiscovery</code> again to |
| 211 resume. Be cautious of the resource intensive nature of discovery. |
| 212 </p> |
| 213 |
| 214 <h2 id="identifying_devices">Identifying devices</h2> |
| 215 |
| 216 <p> |
| 217 A number of different options are provided for identifying devices returned |
| 218 by <code>chrome.bluetooth.getDevices</code> and the related events. |
| 219 </p> |
| 220 |
| 221 <p> |
| 222 If the device supports the Bluetooth |
| 223 <a href="https://developer.bluetooth.org/TechnologyOverview/Pages/DI.aspx">Dev
ice ID specification</a>, |
| 224 several properties are added to the Device object containing the fields |
| 225 defined by that specification. Example: |
| 226 </p> |
| 227 |
| 228 <pre> |
| 229 chrome.bluetooth.getDevices(function(devices) { |
| 230 for (var i = 0; i < devices.length; i++) { |
| 231 if (devices[0].vendorIdSource != undefined) { |
| 232 console.log(devices[0].address + ' = ' + |
| 233 devices[0].vendorIdSource + ':' + |
| 234 devices[0].vendorId.toString(16) + ':' + |
| 235 devices[0].productId.toString(16) + ':' + |
| 236 devices[0].deviceId.toString(16)); |
| 237 } |
| 238 } |
| 239 }); |
| 240 </pre> |
| 241 |
| 242 <p> |
| 243 The Device ID specification is usually sufficient to identify a particular |
| 244 model, and even revision, of a device from a vendor. Where it is not present, |
| 245 you must instead rely on information about the class or type of the device, |
| 246 optionally combined with the manufacturer prefix in the <code>address</code>. |
| 247 </p> |
| 248 |
| 249 <p> |
| 250 Most Bluetooth devices provide Class of Device information as a bit-field |
| 251 interpreted according to the |
| 252 <a href="https://www.bluetooth.org/en-us/specification/assigned-numbers/baseba
nd">Baseband Assigned Numbers</a> |
| 253 document. This bit-field is available in the <code>deviceClass</code> |
| 254 property. |
| 255 </p> |
| 256 |
| 257 <pre> |
| 258 chrome.bluetooth.getDevices(function(devices) { |
| 259 for (var i = 0; i < devices.length; i++) { |
| 260 if (devices[0].vendorIdSource != undefined) { |
| 261 console.log(devices[0].address + ' = ' + |
| 262 devices[0].deviceClass.toString(16)); |
| 263 } |
| 264 } |
| 265 }); |
| 266 </pre> |
| 267 |
| 268 <p> |
| 269 Parsing the field can be complex so for the most common device types Chrome |
| 270 handles this for you and sets the <code>type</code> field. Where this is |
| 271 not available, or insufficient for your needs, you'll need to parse the |
| 272 <code>deviceClass</code> yourself. |
| 273 </p> |
| 274 |
| 275 <pre> |
| 276 chrome.bluetooth.getDevices(function(devices) { |
| 277 for (var i = 0; i < devices.length; i++) { |
| 278 if (devices[0].vendorIdSource != undefined) { |
| 279 console.log(devices[0].address + ' = ' + devices[0].type); |
| 280 } |
| 281 } |
| 282 }); |
| 283 </pre> |
| 284 |
OLD | NEW |