OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Javascript for bluetooth_internals.html, served from | 6 * Javascript for bluetooth_internals.html, served from |
7 * chrome://bluetooth-internals/. | 7 * chrome://bluetooth-internals/. |
8 */ | 8 */ |
9 | 9 |
10 /** | |
11 * The implementation of AdapterClient in | |
12 * device/bluetooth/public/interfaces/adapter.mojom. | |
13 */ | |
14 var AdapterClient = function() {}; | |
15 AdapterClient.prototype = { | |
16 /** | |
17 * Prints added device to console. | |
18 * @param {!bluetoothDevice.DeviceInfo} device | |
19 */ | |
20 deviceAdded: function(device) { console.log('Device added', device); }, | |
21 | |
22 /** | |
23 * Prints removed device to console. | |
24 * @param {!bluetoothDevice.DeviceInfo} device | |
25 */ | |
26 deviceRemoved: function(device) { console.log('Device removed', device); } | |
27 }; | |
28 | |
29 (function() { | 10 (function() { |
30 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; | 11 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; |
31 | 12 |
13 // Dictionary for address->client mapping | |
scheib
2016/10/14 07:14:51
Close with .
mbrunson
2016/10/14 18:25:25
Done.
| |
14 var gattClients = {}; | |
15 | |
16 // Dictionary for address->device mapping | |
17 var devices = {}; | |
18 | |
19 /** | |
20 * The implementation of AdapterClient in | |
21 * device/bluetooth/public/interfaces/adapter.mojom. | |
22 */ | |
23 var AdapterClient = function() {}; | |
24 AdapterClient.prototype = { | |
25 /** | |
26 * Prints added device to console. | |
27 * @param {!bluetoothDevice.DeviceInfo} device | |
28 */ | |
29 deviceAdded: function(device) { | |
30 console.log('Device added', device); | |
31 setDeviceClient(device); | |
32 }, | |
33 | |
34 /** | |
35 * Prints removed device to console. | |
36 * @param {!bluetoothDevice.DeviceInfo} device | |
37 */ | |
38 deviceRemoved: function(device) { | |
39 console.log('Device removed', device); | |
40 delete gattClients[device.address]; | |
41 delete devices[device.address]; | |
42 } | |
43 }; | |
44 | |
45 /** | |
46 * The implementation of GattClient in | |
47 * device/bluetooth/public/interfaces/device.mojom. | |
48 */ | |
49 var GattClient = function() {}; | |
50 GattClient.prototype = { | |
51 /** | |
52 * Prints received advertising packet to console. | |
53 * @param {!bluetoothDevice.AdvertisingPacket} advertisingPacket the | |
54 * advertising packet received from the device. | |
55 */ | |
56 deviceChanged: function(advertisingPacket) { | |
57 console.log(new Date(advertisingPacket.timestamp), advertisingPacket); | |
58 } | |
59 }; | |
60 | |
32 /** | 61 /** |
33 * TODO(crbug.com/652361): Move to shared location. | 62 * TODO(crbug.com/652361): Move to shared location. |
34 * Helper to convert callback-based define() API to a promise-based API. | 63 * Helper to convert callback-based define() API to a promise-based API. |
35 * @param {!Array<string>} moduleNames | 64 * @param {!Array<string>} moduleNames |
36 * @return {!Promise} | 65 * @return {!Promise} |
37 */ | 66 */ |
38 function importModules(moduleNames) { | 67 function importModules(moduleNames) { |
39 return new Promise(function(resolve, reject) { | 68 return new Promise(function(resolve, reject) { |
40 define(moduleNames, function(var_args) { | 69 define(moduleNames, function(var_args) { |
41 resolve(Array.prototype.slice.call(arguments, 0)); | 70 resolve(Array.prototype.slice.call(arguments, 0)); |
(...skipping 15 matching lines...) Expand all Loading... | |
57 ]).then(function([frameInterfaces, ...modules]) { | 86 ]).then(function([frameInterfaces, ...modules]) { |
58 console.log('Loaded modules'); | 87 console.log('Loaded modules'); |
59 | 88 |
60 // Destructure here to assign global variables. | 89 // Destructure here to assign global variables. |
61 [bluetoothAdapter, bluetoothDevice, connection] = modules; | 90 [bluetoothAdapter, bluetoothDevice, connection] = modules; |
62 | 91 |
63 // Hook up the instance properties. | 92 // Hook up the instance properties. |
64 AdapterClient.prototype.__proto__ = | 93 AdapterClient.prototype.__proto__ = |
65 bluetoothAdapter.AdapterClient.stubClass.prototype; | 94 bluetoothAdapter.AdapterClient.stubClass.prototype; |
66 | 95 |
96 GattClient.prototype.__proto__ = | |
97 bluetoothDevice.GattClient.stubClass.prototype; | |
98 | |
67 var adapterFactory = connection.bindHandleToProxy( | 99 var adapterFactory = connection.bindHandleToProxy( |
68 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), | 100 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), |
69 bluetoothAdapter.AdapterFactory); | 101 bluetoothAdapter.AdapterFactory); |
70 | 102 |
71 // Get an Adapter service. | 103 // Get an Adapter service. |
72 return adapterFactory.getAdapter(); | 104 return adapterFactory.getAdapter(); |
73 }).then(function(response) { | 105 }).then(function(response) { |
74 if (!response.adapter) { | 106 if (!response.adapter) { |
75 throw new Error('Bluetooth Not Supported on this platform.'); | 107 throw new Error('Bluetooth Not Supported on this platform.'); |
76 } | 108 } |
77 | 109 |
78 adapter = connection.bindHandleToProxy(response.adapter, | 110 adapter = connection.bindHandleToProxy(response.adapter, |
79 bluetoothAdapter.Adapter); | 111 bluetoothAdapter.Adapter); |
80 | 112 |
81 // Create a message pipe and bind one end to client | 113 // Create a message pipe and bind one end to client |
82 // implementation and the other to the Adapter service. | 114 // implementation and the other to the Adapter service. |
83 adapterClient = new AdapterClient(); | 115 adapterClient = new AdapterClient(); |
84 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); | 116 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); |
85 }); | 117 }); |
86 } | 118 } |
87 | 119 |
88 /** | 120 /** |
89 * Prints device info from the device service. | 121 * Logs the latest info from the device. Creates a message pipe to the device |
122 * if none exists and caches it. | |
90 * @param {!bluetoothDevice.DeviceInfo} deviceInfo the device for which to | 123 * @param {!bluetoothDevice.DeviceInfo} deviceInfo the device for which to |
91 * get the information. | 124 * get the information. |
92 * @return {!Promise} resolves if device service is retrieved, rejects | 125 * @return {!Promise} resolves if device service is retrieved, rejects |
93 * otherwise. | 126 * otherwise. |
94 */ | 127 */ |
95 function logDevice(deviceInfo) { | 128 function logDevice(deviceInfo) { |
129 return getDevice(deviceInfo).then(function(device) { | |
130 return device.getInfo(); | |
131 }).then(function(response) { | |
132 console.log(response.info.name_for_display, response.info); | |
133 }); | |
134 } | |
135 | |
136 /** | |
137 * Creates a logging client to the device with the given |deviceInfo|. | |
138 * @param {!bluetoothDevice.DeviceInfo} deviceInfo the device for which to | |
139 * get the information. | |
140 * @return {!Promise} resolves if device service is retrieved, rejects | |
141 * otherwise. | |
142 */ | |
143 function setDeviceClient(deviceInfo) { | |
144 return getDevice(deviceInfo).then(function(device) { | |
145 var gattClient = new GattClient(); | |
146 device.setClient(connection.bindStubDerivedImpl(gattClient)); | |
147 gattClients[deviceInfo.address] = gattClient; | |
148 return Promise.resolve(); | |
149 }); | |
150 } | |
151 | |
152 /** | |
153 * Gets a message pipe for the device. If none exists, a message pipe is | |
154 * created and cached. | |
155 * @param {!bluetoothDevice.DeviceInfo} deviceInfo the device for which to | |
156 * get the information. | |
157 * @return {!Promise<bluetoothDevice.Device>} resolves with Device if | |
158 * message pipe was created successfully, reject otherwise. | |
159 */ | |
160 function getDevice(deviceInfo) { | |
161 if (devices[deviceInfo.address]) { | |
162 return Promise.resolve(devices[deviceInfo.address]); | |
163 } | |
164 | |
96 return adapter.getDevice(deviceInfo.address).then(function(response) { | 165 return adapter.getDevice(deviceInfo.address).then(function(response) { |
97 var deviceHandle = response.device; | 166 var deviceHandle = response.device; |
98 if (!deviceHandle) { | 167 if (!deviceHandle) { |
99 throw new Error(deviceInfo.name_for_display + ' cannot be found.'); | 168 throw new Error(deviceInfo.name_for_display + ' cannot be found.'); |
100 } | 169 } |
101 | 170 |
102 var device = connection.bindHandleToProxy( | 171 var device = connection.bindHandleToProxy( |
103 deviceHandle, bluetoothDevice.Device); | 172 deviceHandle, bluetoothDevice.Device); |
104 return device.getInfo(); | 173 |
105 }).then(function(response) { | 174 devices[deviceInfo.address] = device; |
106 console.log(deviceInfo.name_for_display, response.info); | 175 return device; |
107 }); | 176 }); |
108 } | 177 } |
109 | 178 |
110 document.addEventListener('DOMContentLoaded', function() { | 179 document.addEventListener('DOMContentLoaded', function() { |
111 initializeProxies() | 180 initializeProxies() |
112 .then(function() { return adapter.getInfo(); }) | 181 .then(function() { return adapter.getInfo(); }) |
113 .then(function(response) { console.log('adapter', response.info); }) | 182 .then(function(response) { console.log('adapter', response.info); }) |
114 .then(function() { return adapter.getDevices(); }) | 183 .then(function() { return adapter.getDevices(); }) |
115 .then(function(response) { | 184 .then(function(response) { |
116 var devices = response.devices; | 185 var deviceInfos = response.devices; |
117 console.log('devices', devices.length); | 186 console.log('devices', deviceInfos.length); |
118 | 187 |
119 return Promise.all(devices.map(logDevice)); | 188 return Promise.all([...deviceInfos.map(logDevice), |
189 ...deviceInfos.map(setDeviceClient)]); | |
120 }) | 190 }) |
121 .catch(function(error) { console.error(error); }); | 191 .catch(function(error) { console.error(error); }); |
122 }); | 192 }); |
123 })(); | 193 })(); |
OLD | NEW |