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, proxy: 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, caches the device info, and sets the |
| 28 * GattClient. |
| 29 * @param {!bluetoothDevice.DeviceInfo} device |
| 30 */ |
| 31 deviceAdded: function(device) { |
| 32 console.log('Device added', device); |
| 33 devices[device.address] = new Device(); |
| 34 devices[device.address].info = device; |
| 35 }, |
| 36 |
| 37 /** |
| 38 * Logs removed device to console and removes the cached device. |
| 39 * @param {!bluetoothDevice.DeviceInfo} device |
| 40 */ |
| 41 deviceRemoved: function(device) { |
| 42 console.log('Device removed', device); |
| 43 delete devices[device.address]; |
| 44 }, |
| 45 |
| 46 /** |
| 47 * Logs received advertising packet to console. caches the last valid |
| 48 * RSSI, and refreshes the device list. |
| 49 * @param {!bluetoothDevice.AdvertisingPacket} advertisingPacket the |
| 50 * advertising packet received from the device. |
| 51 */ |
| 52 deviceChanged: function(deviceInfo) { |
| 53 console.log(new Date(), deviceInfo); |
| 54 devices[deviceInfo.address].info.rssi = deviceInfo.rssi; |
| 55 } |
| 56 }; |
| 57 |
32 /** | 58 /** |
33 * TODO(crbug.com/652361): Move to shared location. | 59 * TODO(crbug.com/652361): Move to shared location. |
34 * Helper to convert callback-based define() API to a promise-based API. | 60 * Helper to convert callback-based define() API to a promise-based API. |
35 * @param {!Array<string>} moduleNames | 61 * @param {!Array<string>} moduleNames |
36 * @return {!Promise} | 62 * @return {!Promise} |
37 */ | 63 */ |
38 function importModules(moduleNames) { | 64 function importModules(moduleNames) { |
39 return new Promise(function(resolve, reject) { | 65 return new Promise(function(resolve, reject) { |
40 define(moduleNames, function(var_args) { | 66 define(moduleNames, function(var_args) { |
41 resolve(Array.prototype.slice.call(arguments, 0)); | 67 resolve(Array.prototype.slice.call(arguments, 0)); |
(...skipping 13 matching lines...) Expand all Loading... |
55 'device/bluetooth/public/interfaces/device.mojom', | 81 'device/bluetooth/public/interfaces/device.mojom', |
56 'mojo/public/js/connection', | 82 'mojo/public/js/connection', |
57 ]).then(function([frameInterfaces, ...modules]) { | 83 ]).then(function([frameInterfaces, ...modules]) { |
58 console.log('Loaded modules'); | 84 console.log('Loaded modules'); |
59 | 85 |
60 // Destructure here to assign global variables. | 86 // Destructure here to assign global variables. |
61 [bluetoothAdapter, bluetoothDevice, connection] = modules; | 87 [bluetoothAdapter, bluetoothDevice, connection] = modules; |
62 | 88 |
63 // Hook up the instance properties. | 89 // Hook up the instance properties. |
64 AdapterClient.prototype.__proto__ = | 90 AdapterClient.prototype.__proto__ = |
65 bluetoothAdapter.AdapterClient.stubClass.prototype; | 91 bluetoothAdapter.AdapterClient.stubClass.prototype; |
66 | 92 |
67 var adapterFactory = connection.bindHandleToProxy( | 93 var adapterFactory = connection.bindHandleToProxy( |
68 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), | 94 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), |
69 bluetoothAdapter.AdapterFactory); | 95 bluetoothAdapter.AdapterFactory); |
70 | 96 |
71 // Get an Adapter service. | 97 // Get an Adapter service. |
72 return adapterFactory.getAdapter(); | 98 return adapterFactory.getAdapter(); |
73 }).then(function(response) { | 99 }).then(function(response) { |
74 if (!response.adapter) { | 100 if (!response.adapter) { |
75 throw new Error('Bluetooth Not Supported on this platform.'); | 101 throw new Error('Bluetooth Not Supported on this platform.'); |
76 } | 102 } |
77 | 103 |
78 adapter = connection.bindHandleToProxy(response.adapter, | 104 adapter = connection.bindHandleToProxy(response.adapter, |
79 bluetoothAdapter.Adapter); | 105 bluetoothAdapter.Adapter); |
80 | 106 |
81 // Create a message pipe and bind one end to client | 107 // Create a message pipe and bind one end to client |
82 // implementation and the other to the Adapter service. | 108 // implementation and the other to the Adapter service. |
83 adapterClient = new AdapterClient(); | 109 adapterClient = new AdapterClient(); |
84 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); | 110 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); |
85 }); | 111 }); |
86 } | 112 } |
87 | 113 |
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() { | 114 document.addEventListener('DOMContentLoaded', function() { |
111 initializeProxies() | 115 initializeProxies() |
112 .then(function() { return adapter.getInfo(); }) | 116 .then(function() { return adapter.getInfo(); }) |
113 .then(function(response) { console.log('adapter', response.info); }) | 117 .then(function(response) { console.log('adapter', response.info); }) |
114 .then(function() { return adapter.getDevices(); }) | 118 .then(function() { return adapter.getDevices(); }) |
115 .then(function(response) { | 119 .then(function(response) { |
116 var devices = response.devices; | 120 console.log('devices', response.devices.length); |
117 console.log('devices', devices.length); | |
118 | 121 |
119 return Promise.all(devices.map(logDevice)); | 122 response.devices.forEach(function(deviceInfo) { |
| 123 devices[deviceInfo.address] = new Device(); |
| 124 devices[deviceInfo.address].info = deviceInfo; |
| 125 console.log(deviceInfo.name_for_display, deviceInfo); |
| 126 }); |
120 }) | 127 }) |
121 .catch(function(error) { console.error(error); }); | 128 .catch(function(error) { console.error(error); }); |
122 }); | 129 }); |
123 })(); | 130 })(); |
OLD | NEW |