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 (function() { | 10 (function() { |
(...skipping 11 matching lines...) Expand all Loading... | |
22 * device/bluetooth/public/interfaces/adapter.mojom. | 22 * device/bluetooth/public/interfaces/adapter.mojom. |
23 */ | 23 */ |
24 var AdapterClient = function() {}; | 24 var AdapterClient = function() {}; |
25 AdapterClient.prototype = { | 25 AdapterClient.prototype = { |
26 /** | 26 /** |
27 * Logs added device to console, caches the device info, and sets the | 27 * Logs added device to console, caches the device info, and sets the |
28 * GattClient. | 28 * GattClient. |
29 * @param {!bluetoothDevice.DeviceInfo} device | 29 * @param {!bluetoothDevice.DeviceInfo} device |
30 */ | 30 */ |
31 deviceAdded: function(device) { | 31 deviceAdded: function(device) { |
32 console.log('Device added', device); | 32 console.log('Device added', device); |
scheib
2016/10/15 03:51:45
Remove .log lines now that there's a display?
mbrunson
2016/10/18 00:21:25
I'll remove them but leave the log for adapter.get
| |
33 devices[device.address] = new Device(); | 33 devices[device.address] = new Device(); |
34 devices[device.address].info = device; | 34 devices[device.address].info = device; |
35 setDeviceClient(device); | 35 setDeviceClient(device); |
36 refreshDeviceList(); | |
36 }, | 37 }, |
37 | 38 |
38 /** | 39 /** |
39 * Logs removed device to console and removes the cached device. | 40 * Logs removed device to console and removes the cached device. |
40 * @param {!bluetoothDevice.DeviceInfo} device | 41 * @param {!bluetoothDevice.DeviceInfo} device |
41 */ | 42 */ |
42 deviceRemoved: function(device) { | 43 deviceRemoved: function(device) { |
43 console.log('Device removed', device); | 44 console.log('Device removed', device); |
44 delete devices[device.address]; | 45 delete devices[device.address]; |
46 refreshDeviceList(); | |
45 } | 47 } |
46 }; | 48 }; |
47 | 49 |
48 /** | 50 /** |
49 * The implementation of GattClient in | 51 * The implementation of GattClient in |
50 * device/bluetooth/public/interfaces/device.mojom. | 52 * device/bluetooth/public/interfaces/device.mojom. |
51 */ | 53 */ |
52 var GattClient = function() {}; | 54 var GattClient = function() {}; |
53 GattClient.prototype = { | 55 GattClient.prototype = { |
54 /** | 56 /** |
55 * Logs received advertising packet to console. caches the last valid | 57 * Logs received advertising packet to console. caches the last valid |
56 * RSSI, and refreshes the device list. | 58 * RSSI, and refreshes the device list. |
57 * @param {!bluetoothDevice.AdvertisingPacket} advertisingPacket the | 59 * @param {!bluetoothDevice.AdvertisingPacket} advertisingPacket the |
58 * advertising packet received from the device. | 60 * advertising packet received from the device. |
59 */ | 61 */ |
60 packetReceived: function(advertisingPacket) { | 62 packetReceived: function(advertisingPacket) { |
61 console.log(new Date(advertisingPacket.timestamp), advertisingPacket); | 63 console.log(new Date(advertisingPacket.timestamp), advertisingPacket); |
62 | 64 |
63 if (advertisingPacket.rssi) { | 65 if (advertisingPacket.rssi) { |
64 devices[advertisingPacket.device.address].info.rssi = | 66 devices[advertisingPacket.device.address].info.rssi = |
65 advertisingPacket.rssi; | 67 advertisingPacket.rssi; |
66 } | 68 } |
69 refreshDeviceList(); | |
67 } | 70 } |
68 }; | 71 }; |
69 | 72 |
70 /** | 73 /** |
71 * TODO(crbug.com/652361): Move to shared location. | 74 * TODO(crbug.com/652361): Move to shared location. |
72 * Helper to convert callback-based define() API to a promise-based API. | 75 * Helper to convert callback-based define() API to a promise-based API. |
73 * @param {!Array<string>} moduleNames | 76 * @param {!Array<string>} moduleNames |
74 * @return {!Promise} | 77 * @return {!Promise} |
75 */ | 78 */ |
76 function importModules(moduleNames) { | 79 function importModules(moduleNames) { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
167 | 170 |
168 if (!devices[deviceInfo.address]) { | 171 if (!devices[deviceInfo.address]) { |
169 devices[deviceInfo.address] = new Device(); | 172 devices[deviceInfo.address] = new Device(); |
170 } | 173 } |
171 | 174 |
172 devices[deviceInfo.address].proxy = device; | 175 devices[deviceInfo.address].proxy = device; |
173 return device; | 176 return device; |
174 }); | 177 }); |
175 } | 178 } |
176 | 179 |
180 /** | |
181 * Updates the device list with the current devices from the |devices| | |
182 * dictionary. | |
183 */ | |
184 function refreshDeviceList() { | |
185 var deviceRowTemplate = $('device-row-template'); | |
186 var deviceList = $('device-list'); | |
187 deviceList.innerHTML = ''; | |
188 | |
189 for (var address in devices) { | |
190 var info = devices[address].info; | |
191 var deviceRow = deviceRowTemplate.content.cloneNode(true); | |
192 deviceRow.querySelector('.device-name').innerText = | |
193 info.name_for_display; | |
194 deviceRow.querySelector('.device-address').innerText = info.address; | |
195 deviceRow.querySelector('.device-rssi').innerText = info.rssi || '0'; | |
196 deviceList.appendChild(deviceRow); | |
197 } | |
198 } | |
199 | |
177 document.addEventListener('DOMContentLoaded', function() { | 200 document.addEventListener('DOMContentLoaded', function() { |
178 initializeProxies() | 201 initializeProxies() |
179 .then(function() { return adapter.getInfo(); }) | 202 .then(function() { return adapter.getInfo(); }) |
180 .then(function(response) { console.log('adapter', response.info); }) | 203 .then(function(response) { console.log('adapter', response.info); }) |
181 .then(function() { return adapter.getDevices(); }) | 204 .then(function() { return adapter.getDevices(); }) |
182 .then(function(response) { | 205 .then(function(response) { |
183 console.log('devices', response.devices.length); | 206 console.log('devices', response.devices.length); |
184 | 207 |
185 response.devices.forEach(function(deviceInfo) { | 208 response.devices.forEach(function(deviceInfo) { |
186 devices[deviceInfo.address] = new Device(); | 209 devices[deviceInfo.address] = new Device(); |
187 devices[deviceInfo.address].info = deviceInfo; | 210 devices[deviceInfo.address].info = deviceInfo; |
188 console.log(deviceInfo.name_for_display, deviceInfo); | |
189 }); | 211 }); |
190 | 212 |
191 return Promise.all(response.devices.map(setDeviceClient)); | 213 return Promise.all(response.devices.map(setDeviceClient)); |
192 }) | 214 }) |
215 .then(refreshDeviceList) | |
193 .catch(function(error) { console.error(error); }); | 216 .catch(function(error) { console.error(error); }); |
194 }); | 217 }); |
195 })(); | 218 })(); |
OLD | NEW |