Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(840)

Side by Side Diff: chrome/common/extensions/docs/templates/articles/app_bluetooth.html

Issue 189263004: Bluetooth: write documentation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address armansito review comments Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 <h1>Bluetooth</h1>
mkearney1 2014/03/10 21:13:37 Throughout this doc you say 'event does not begin
keybuk 2014/03/11 17:03:39 Done.
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
mkearney1 2014/03/10 21:13:37 Small nit-- add comma after 'discover nearby devic
keybuk 2014/03/11 17:03:39 Done.
keybuk 2014/03/11 17:03:39 Done.
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
mkearney1 2014/03/10 21:13:37 Small nit-- add comma between nearby devices and u
keybuk 2014/03/11 17:03:39 Done.
142 <code>chrome.bluetooth.startDiscovery</code> method, discovery can be
143 resource intensive so you should call
mkearney1 2014/03/10 21:13:37 'discovery can be resource intensive...' should be
keybuk 2014/03/11 17:03:39 Done.
144 <code>chrome.bluetooth.stopDiscovery</code> when done.
145 </p>
146
147 <p>
148 You should call <code>chrome.bletooth.startDiscovery</code> whenever your
149 app needs to discover nearby devices. Do not check the
150 <code>discovering</code> property of <code>AdapterState</code>. The call
151 to start discovery will succeed even if another app is discovering nearby
152 devices, and will ensure the adapter continues to perform discovery after
153 that other app has stopped.
154 </p>
155
156 <p>
157 Information about each newly discovered device is received using the
158 <code>chrome.bluetooth.onDeviceAdded</code> event. For devices that have
159 already been discovered recently, or have been previously paired with or
160 connected to, the event will not be sent, instead you should call
mkearney1 2014/03/10 21:13:37 'instead you should call...' should be its own sen
keybuk 2014/03/11 17:03:39 Done.
161 <code>chrome.bluetooth.getDevices</code> to obtain the current information,
162 and use the <code>chrome.bluetooth.onDeviceChanged</code> event to be notified of changes to that information as a result of discovery.
163 </p>
164
165 <p>
166 Example:
167 </p>
168
169 <pre>
170 var device_names = {};
171 var updateDeviceName = function(device) {
172 device_names[device.address] = device.name;
173 };
174 var removeDeviceName = function(device) {
175 delete device_names[device.address];
176 }
177
178 // Add listeners to receive newly found devices and updates
179 // to the previously known devices.
180 chrome.bluetooth.onDeviceAdded.addListener(updateDeviceName);
181 chrome.bluetooth.onDeviceChanged.addListener(updateDeviceName);
182 chrome.bluetooth.onDeviceRemoved.addListener(removeDeviceName);
183
184 // With the listeners in place, get the list of devices found in
185 // previous discovery sessions, or any currently active ones,
186 // along with paired devices.
187 chrome.bluetooth.getDevices(function(devices) {
188 for (var i = 0; i < devices.length; i++) {
189 updateDeviceName(devices[i]);
190 }
191 });
192
193 // Now begin the discovery process.
194 chrome.bluetooth.startDiscovery(function() {
195 // Stop discovery after 30 seconds.
196 setTimeout(function() {
197 chrome.bluetooth.stopDiscovery(function() {});
198 }, 30000);
199 });
200 </pre>
201
202 <p>
203 If the user turns off the Bluetooth radio, all discovery sessions will be
204 ended and not resumed automatically when the radio is switched on. If this
205 matters to your app you should watch the
mkearney1 2014/03/10 21:13:37 Add comma between 'app' and 'you': 'matters to you
keybuk 2014/03/11 17:03:39 Done.
206 <code>chrome.bluetooth.onAdapterStateChanged</code> event. If the
207 <code>discovering</code> property changes to <code>false</code> then your app
mkearney1 2014/03/10 21:13:37 Add comma between 'false' and 'then': '<code>false
keybuk 2014/03/11 17:03:39 Done.
208 will need to call <code>chrome.bluetooth.startDiscovery</code> again to
209 resume. Be cautious of the resource intensive nature of discovery.
mkearney1 2014/03/10 21:13:37 Is this caution important enough to put in a warni
keybuk 2014/03/11 17:03:39 No, it's more just a continual reminder to enforce
210 </p>
211
212 <h2 id="identifying_devices">Identifying devices</h2>
213
214 <p>
215 A number of different options are provided for identifying devices returned
216 by <code>chrome.bluetooth.getDevices</code> and the related events.
217 </p>
218
219 <p>
220 If the device supports the Bluetooth
221 <a href="https://developer.bluetooth.org/TechnologyOverview/Pages/DI.aspx">Dev ice ID specification</a>,
222 several properties are added to the Device object containing the fields
223 defined by that specification. Example:
224 </p>
225
226 <pre>
227 chrome.bluetooth.getDevices(function(devices) {
228 for (var i = 0; i < devices.length; i++) {
229 if (devices[0].vendorIdSource != undefined) {
230 console.log(devices[0].address + ' = ' +
231 devices[0].vendorIdSource + ':' +
232 devices[0].vendorId.toString(16) + ':' +
233 devices[0].productId.toString(16) + ':' +
234 devices[0].deviceId.toString(16));
235 }
236 }
237 });
238 </pre>
239
240 <p>
241 The Device ID specification is usually sufficient to identify a particular
242 model, and even revision, of a device from a vendor. Where it is not present
mkearney1 2014/03/10 21:13:37 Add comma after 'present': 'Where it is not presen
keybuk 2014/03/11 17:03:39 Done.
243 you must instead rely on information about the class or type of the device,
244 optionally combined with the manufacturer prefix in the <code>address</code>.
245 </p>
246
247 <p>
248 Most Bluetooth devices provide Class of Device information as a bit-field
249 interpreted according to the
250 <a href="https://www.bluetooth.org/en-us/specification/assigned-numbers/baseba nd">Baseband Assigned Numbers</a>
251 document. This bit-field is available in the <code>deviceClass</code>
252 property.
253 </p>
254
255 <pre>
256 chrome.bluetooth.getDevices(function(devices) {
257 for (var i = 0; i < devices.length; i++) {
258 if (devices[0].vendorIdSource != undefined) {
259 console.log(devices[0].address + ' = ' +
260 devices[0].deviceClass.toString(16));
261 }
262 }
263 });
264 </pre>
265
266 <p>
267 Parsing the field can be complex so for the most common device types Chrome
268 handles this for you and sets the <code>type</code> field. Where this is
269 not available, or insufficient for your needs, you'll need to parse the
270 <code>deviceClass</code> yourself.
271 </p>
272
273 <pre>
274 chrome.bluetooth.getDevices(function(devices) {
275 for (var i = 0; i < devices.length; i++) {
276 if (devices[0].vendorIdSource != undefined) {
277 console.log(devices[0].address + ' = ' + devices[0].type);
278 }
279 }
280 });
281 </pre>
282
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698