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 cr.define('bluetooth_internals', function() { | 10 cr.define('bluetooth_internals', function() { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 deviceChanged: function(deviceInfo) { | 51 deviceChanged: function(deviceInfo) { |
52 console.log(new Date(), deviceInfo); | 52 console.log(new Date(), deviceInfo); |
53 assert(this.devices.getByAddress(deviceInfo.address), | 53 assert(this.devices.getByAddress(deviceInfo.address), |
54 'Device does not exist.'); | 54 'Device does not exist.'); |
55 | 55 |
56 var device = this.devices.getByAddress(deviceInfo.address); | 56 var device = this.devices.getByAddress(deviceInfo.address); |
57 | 57 |
58 // Update rssi if it's valid | 58 // Update rssi if it's valid |
59 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) || | 59 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) || |
60 (device.info.rssi && device.info.rssi.value); | 60 (device.info.rssi && device.info.rssi.value); |
61 device.info = deviceInfo; | 61 |
| 62 // Merge given |deviceInfo| with current device info. |
| 63 Object.assign(device.info, deviceInfo); |
62 device.info.rssi = { value: rssi }; | 64 device.info.rssi = { value: rssi }; |
63 this.devices.update(device); | 65 this.devices.update(device); |
64 } | 66 } |
65 }; | 67 }; |
66 | 68 |
67 /** | 69 /** |
68 * TODO(crbug.com/652361): Move to shared location. | 70 * TODO(crbug.com/652361): Move to shared location. |
69 * Helper to convert callback-based define() API to a promise-based API. | 71 * Helper to convert callback-based define() API to a promise-based API. |
70 * @param {!Array<string>} moduleNames | 72 * @param {!Array<string>} moduleNames |
71 * @return {!Promise} | 73 * @return {!Promise} |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 adapter = connection.bindHandleToProxy(response.adapter, | 113 adapter = connection.bindHandleToProxy(response.adapter, |
112 bluetoothAdapter.Adapter); | 114 bluetoothAdapter.Adapter); |
113 | 115 |
114 // Create a message pipe and bind one end to client | 116 // Create a message pipe and bind one end to client |
115 // implementation and the other to the Adapter service. | 117 // implementation and the other to the Adapter service. |
116 adapterClient = new AdapterClient(); | 118 adapterClient = new AdapterClient(); |
117 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); | 119 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); |
118 }); | 120 }); |
119 } | 121 } |
120 | 122 |
| 123 function connectDevice(device) { |
| 124 return adapter.connectToDevice(device.info.address).then( |
| 125 function(response) { |
| 126 if (!response.error) { |
| 127 device.proxy = connection.bindHandleToProxy(response.device, |
| 128 bluetoothDevice.Device); |
| 129 return; |
| 130 } |
| 131 |
| 132 // TODO(mbrunson): Replace with more descriptive error messages. |
| 133 var errorString = Object.keys( |
| 134 bluetoothAdapter.ConnectError.Code)[response.error.code]; |
| 135 |
| 136 throw new Error(errorString); |
| 137 }).then(function() { |
| 138 // TODO(mbrunson): Remove when ServicesDiscovered() callback function is |
| 139 // added to AdapterClient. |
| 140 var done = false; |
| 141 var fetchJob = setInterval(function() { |
| 142 if (!done) { |
| 143 if (device.proxy) { |
| 144 device.proxy.getServices().then(function(response) { |
| 145 device.info.services = response.services; |
| 146 done = true; |
| 147 |
| 148 if (device.info.services) { |
| 149 adapterClient.devices.update(device); |
| 150 clearInterval(fetchJob); |
| 151 } |
| 152 }); |
| 153 } else { |
| 154 done = true; |
| 155 clearInterval(fetchJob); |
| 156 } |
| 157 } |
| 158 }, 5000); |
| 159 }); |
| 160 } |
| 161 |
| 162 function disconnectDevice(device) { |
| 163 device.proxy = null; |
| 164 return Promise.resolve(); |
| 165 } |
| 166 |
121 function initialize() { | 167 function initialize() { |
122 initializeProxies() | 168 initializeProxies() |
123 .then(function() { return adapter.getInfo(); }) | 169 .then(function() { return adapter.getInfo(); }) |
124 .then(function(response) { console.log('adapter', response.info); }) | 170 .then(function(response) { console.log('adapter', response.info); }) |
125 .then(function() { return adapter.getDevices(); }) | 171 .then(function() { return adapter.getDevices(); }) |
126 .then(function(response) { | 172 .then(function(response) { |
127 response.devices.forEach(adapterClient.deviceAdded, | 173 response.devices.forEach(adapterClient.deviceAdded, |
128 adapterClient /** this */); | 174 adapterClient /** this */); |
129 | 175 |
130 var deviceTable = new device.DeviceTable(); | 176 var deviceTable = new device.DeviceTable(); |
| 177 |
| 178 deviceTable.setConnectionHandlers(connectDevice, disconnectDevice); |
131 deviceTable.setDevices(adapterClient.devices); | 179 deviceTable.setDevices(adapterClient.devices); |
132 document.body.appendChild(deviceTable); | 180 document.body.appendChild(deviceTable); |
133 }) | 181 }) |
134 .catch(function(error) { console.error(error); }); | 182 .catch(function(error) { console.error(error); }); |
135 } | 183 } |
136 | 184 |
137 return { | 185 return { |
138 initialize: initialize | 186 initialize: initialize |
139 }; | 187 }; |
140 | 188 |
141 }); | 189 }); |
142 | 190 |
143 document.addEventListener('DOMContentLoaded', bluetooth_internals.initialize); | 191 document.addEventListener('DOMContentLoaded', bluetooth_internals.initialize); |
OLD | NEW |