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->Device cache. | |
14 var devices = {}; | |
15 | |
16 // Data model for a cached device. | |
17 function Device() {}; | |
18 Device.prototype = { info: null }; | |
19 | |
20 /** | |
21 * The implementation of AdapterClient in | |
22 * device/bluetooth/public/interfaces/adapter.mojom. | |
23 */ | |
24 var AdapterClient = function() {}; | |
25 AdapterClient.prototype = { | |
26 /** | |
27 * Logs added device to console and caches the device info. | |
28 * @param {!bluetoothDevice.DeviceInfo} device | |
29 */ | |
30 deviceAdded: function(device) { | |
scheib
2016/10/19 23:49:24
function(deviceInfo) - so that we don't confuse th
mbrunson
2016/10/20 00:33:51
Done.
| |
31 console.log('Device added', device); | |
32 devices[device.address] = new Device(); | |
33 devices[device.address].info = device; | |
34 }, | |
35 | |
36 /** | |
37 * Logs removed device to console and removes the cached device. | |
38 * @param {!bluetoothDevice.DeviceInfo} device | |
39 */ | |
40 deviceRemoved: function(device) { | |
41 console.log('Device removed', device); | |
42 delete devices[device.address]; | |
43 }, | |
44 | |
45 /** | |
46 * Logs received advertising packet to console. caches the last valid | |
ortuno
2016/10/19 23:19:19
nit: s/caches/Caches/
mbrunson
2016/10/20 00:33:51
This whole comment needs to be fixed. Done.
| |
47 * RSSI, and refreshes the device list. | |
48 * @param {!bluetoothDevice.AdvertisingPacket} advertisingPacket the | |
49 * advertising packet received from the device. | |
50 */ | |
51 deviceChanged: function(deviceInfo) { | |
52 console.log(new Date(), deviceInfo); | |
53 | |
54 if (deviceInfo.rssi) { | |
scheib
2016/10/19 23:49:24
Shouldn't this cache all the deviceInfo, always?
mbrunson
2016/10/20 00:33:51
I think it's fine if the data model has the update
| |
55 devices[deviceInfo.address].info.rssi = deviceInfo.rssi; | |
56 } | |
57 } | |
58 }; | |
59 | |
32 /** | 60 /** |
33 * TODO(crbug.com/652361): Move to shared location. | 61 * TODO(crbug.com/652361): Move to shared location. |
34 * Helper to convert callback-based define() API to a promise-based API. | 62 * Helper to convert callback-based define() API to a promise-based API. |
35 * @param {!Array<string>} moduleNames | 63 * @param {!Array<string>} moduleNames |
36 * @return {!Promise} | 64 * @return {!Promise} |
37 */ | 65 */ |
38 function importModules(moduleNames) { | 66 function importModules(moduleNames) { |
39 return new Promise(function(resolve, reject) { | 67 return new Promise(function(resolve, reject) { |
40 define(moduleNames, function(var_args) { | 68 define(moduleNames, function(var_args) { |
41 resolve(Array.prototype.slice.call(arguments, 0)); | 69 resolve(Array.prototype.slice.call(arguments, 0)); |
(...skipping 13 matching lines...) Expand all Loading... | |
55 'device/bluetooth/public/interfaces/device.mojom', | 83 'device/bluetooth/public/interfaces/device.mojom', |
56 'mojo/public/js/connection', | 84 'mojo/public/js/connection', |
57 ]).then(function([frameInterfaces, ...modules]) { | 85 ]).then(function([frameInterfaces, ...modules]) { |
58 console.log('Loaded modules'); | 86 console.log('Loaded modules'); |
59 | 87 |
60 // Destructure here to assign global variables. | 88 // Destructure here to assign global variables. |
61 [bluetoothAdapter, bluetoothDevice, connection] = modules; | 89 [bluetoothAdapter, bluetoothDevice, connection] = modules; |
62 | 90 |
63 // Hook up the instance properties. | 91 // Hook up the instance properties. |
64 AdapterClient.prototype.__proto__ = | 92 AdapterClient.prototype.__proto__ = |
65 bluetoothAdapter.AdapterClient.stubClass.prototype; | 93 bluetoothAdapter.AdapterClient.stubClass.prototype; |
66 | 94 |
67 var adapterFactory = connection.bindHandleToProxy( | 95 var adapterFactory = connection.bindHandleToProxy( |
68 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), | 96 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), |
69 bluetoothAdapter.AdapterFactory); | 97 bluetoothAdapter.AdapterFactory); |
70 | 98 |
71 // Get an Adapter service. | 99 // Get an Adapter service. |
72 return adapterFactory.getAdapter(); | 100 return adapterFactory.getAdapter(); |
73 }).then(function(response) { | 101 }).then(function(response) { |
74 if (!response.adapter) { | 102 if (!response.adapter) { |
75 throw new Error('Bluetooth Not Supported on this platform.'); | 103 throw new Error('Bluetooth Not Supported on this platform.'); |
76 } | 104 } |
77 | 105 |
78 adapter = connection.bindHandleToProxy(response.adapter, | 106 adapter = connection.bindHandleToProxy(response.adapter, |
79 bluetoothAdapter.Adapter); | 107 bluetoothAdapter.Adapter); |
80 | 108 |
81 // Create a message pipe and bind one end to client | 109 // Create a message pipe and bind one end to client |
82 // implementation and the other to the Adapter service. | 110 // implementation and the other to the Adapter service. |
83 adapterClient = new AdapterClient(); | 111 adapterClient = new AdapterClient(); |
84 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); | 112 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); |
85 }); | 113 }); |
86 } | 114 } |
87 | 115 |
88 /** | |
89 * Prints device info from the device service. | |
90 * @param {!bluetoothDevice.DeviceInfo} deviceInfo the device for which to | |
91 * get the information. | |
92 * @return {!Promise} resolves if device service is retrieved, rejects | |
93 * otherwise. | |
94 */ | |
95 function logDevice(deviceInfo) { | |
96 return adapter.getDevice(deviceInfo.address).then(function(response) { | |
97 var deviceHandle = response.device; | |
98 if (!deviceHandle) { | |
99 throw new Error(deviceInfo.name_for_display + ' cannot be found.'); | |
100 } | |
101 | |
102 var device = connection.bindHandleToProxy( | |
103 deviceHandle, bluetoothDevice.Device); | |
104 return device.getInfo(); | |
105 }).then(function(response) { | |
106 console.log(deviceInfo.name_for_display, response.info); | |
107 }); | |
108 } | |
109 | |
110 document.addEventListener('DOMContentLoaded', function() { | 116 document.addEventListener('DOMContentLoaded', function() { |
111 initializeProxies() | 117 initializeProxies() |
112 .then(function() { return adapter.getInfo(); }) | 118 .then(function() { return adapter.getInfo(); }) |
113 .then(function(response) { console.log('adapter', response.info); }) | 119 .then(function(response) { console.log('adapter', response.info); }) |
114 .then(function() { return adapter.getDevices(); }) | 120 .then(function() { return adapter.getDevices(); }) |
115 .then(function(response) { | 121 .then(function(response) { |
116 var devices = response.devices; | 122 console.log('devices', response.devices.length); |
117 console.log('devices', devices.length); | |
118 | 123 |
119 return Promise.all(devices.map(logDevice)); | 124 response.devices.forEach(function(deviceInfo) { |
125 devices[deviceInfo.address] = new Device(); | |
126 devices[deviceInfo.address].info = deviceInfo; | |
127 console.log(deviceInfo.name_for_display, deviceInfo); | |
128 }); | |
120 }) | 129 }) |
121 .catch(function(error) { console.error(error); }); | 130 .catch(function(error) { console.error(error); }); |
122 }); | 131 }); |
123 })(); | 132 })(); |
OLD | NEW |