Index: chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
diff --git a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
index 024fedbb04d8b53e88ad55970a59b01201f3a060..cbc94bf72444bac1f080d31a3833b0f4b40f90e5 100644 |
--- a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
+++ b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
@@ -18,8 +18,7 @@ AdapterClient.prototype = { |
* @param {!Object} device the device that was added |
*/ |
deviceAdded: function(device) { |
- console.log('Device added'); |
- console.log(device); |
+ console.log('Device added', device); |
}, |
/** |
@@ -27,27 +26,15 @@ AdapterClient.prototype = { |
* @param {!Object} device the device that was removed |
*/ |
deviceRemoved: function(device) { |
- console.log('Device removed'); |
- console.log(device); |
+ console.log('Device removed', device); |
} |
}; |
(function() { |
- var adapterFactory = null; |
- var adapter = null; |
- var adapterClient = null; |
- |
- /** |
- * TODO: Move to shared location. See crbug.com/652361. |
- * Logs basic information retrieved from the adapter. |
- */ |
- function logAdapterInfo() { |
- console.log('Getting adapter info'); |
- |
- adapter.getInfo().then(function(response) { console.log(response.info); }); |
- } |
+ var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; |
/** |
+ * TODO: Move to shared location. See http://crbug.com/652361. |
ortuno
2016/10/10 00:11:20
Please follow TODO format of the style guide[1] e.
mbrunson
2016/10/10 19:21:28
Done.
|
* Helper to convert callback-based define() API to a promise-based API. |
* @param {!Array<string>} moduleNames |
* @return {!Promise} |
@@ -62,46 +49,84 @@ AdapterClient.prototype = { |
/** |
* Initializes Mojo proxies for page and Bluetooth services. |
- * @return {!Promise} resolves if adapter is acquired |
+ * @return {!Promise} resolves if adapter is acquired, rejects if Bluetooth |
+ * is not supported. |
*/ |
function initializeProxies() { |
return importModules([ |
'content/public/renderer/frame_interfaces', |
'device/bluetooth/public/interfaces/adapter.mojom', |
+ 'device/bluetooth/public/interfaces/le_device.mojom', |
'mojo/public/js/connection', |
- ]).then(function([frameInterfaces, bluetoothAdapter, connection]) { |
+ ]).then(function([frameInterfaces, ...modules]) { |
ortuno
2016/10/10 00:11:20
I thought you weren't allowed to use ES6 features?
mbrunson
2016/10/10 19:21:27
I was told destructuring is supported.
|
console.log('Loaded modules'); |
+ [bluetoothAdapter, bluetoothDevice, connection] = modules; |
+ |
// Hook up the instance properties. |
AdapterClient.prototype.__proto__ = |
bluetoothAdapter.AdapterClient.stubClass.prototype; |
- adapterFactory = connection.bindHandleToProxy( |
+ var adapterFactory = connection.bindHandleToProxy( |
ortuno
2016/10/10 00:11:20
I think you have to rebase; I remember you making
mbrunson
2016/10/10 19:21:28
Yeah. My repo is in a weird state right now. I'll
|
frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), |
bluetoothAdapter.AdapterFactory); |
// Get an Adapter service. |
- return adapterFactory.getAdapter().then(function(response) { |
- if (!response.adapter) { |
- throw new Error('Bluetooth Not Supported on this platform.'); |
- } |
- |
- adapter = connection.bindHandleToProxy(response.adapter, |
- bluetoothAdapter.Adapter); |
- |
- // Create a message pipe and bind one end to client |
- // implementation and the other to the Adapter service. |
- adapterClient = new AdapterClient(); |
- adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); |
- }); |
+ return adapterFactory.getAdapter(); |
+ }).then(function(response) { |
+ if (!response.adapter) { |
+ throw new Error('Bluetooth Not Supported on this platform.'); |
+ } |
+ |
+ adapter = connection.bindHandleToProxy(response.adapter, |
+ bluetoothAdapter.Adapter); |
+ |
+ // Create a message pipe and bind one end to client |
+ // implementation and the other to the Adapter service. |
+ adapterClient = new AdapterClient(); |
+ adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); |
}); |
} |
+ /** |
+ * Prints device info from the device service. |
+ * @param {!bluetoothDevice.LEDeviceInfo} deviceInfo the device for which to |
+ * get the information. |
+ * @return {!Promise} resolves if device service is retrieved, rejects |
+ * otherwise. |
+ */ |
+ function logDeviceService(deviceInfo) { |
+ return adapter.getDeviceService(deviceInfo.address) |
+ .then(function(response) { |
+ var deviceHandle = response.device; |
+ |
+ if (deviceHandle) { |
+ var device = connection.bindHandleToProxy( |
+ deviceHandle, bluetoothDevice.LEDevice); |
+ return device.getInfo(); |
+ } |
+ |
+ throw new Error('Device cannot be found.'); |
+ }) |
+ .then(function(response) { |
+ console.log(deviceInfo.name, response.info); |
+ }); |
+ } |
+ |
document.addEventListener('DOMContentLoaded', function() { |
initializeProxies() |
- .then(function() { logAdapterInfo(); }) |
+ .then(function() { return adapter.getInfo(); }) |
+ .then(function(response) { console.log('adapter', response.info); }) |
.then(function() { return adapter.getDevices(); }) |
- .then(function(response) { console.log(response.devices); }) |
+ .then(function(response) { |
+ var devices = response.devices; |
+ console.log('devices', devices.length); |
+ |
+ for (var i = 0; i < devices.length; i++) { |
ortuno
2016/10/10 00:11:20
nit:
devices.forEach(function(deviceInfo) {
log
mbrunson
2016/10/10 19:21:28
Done.
|
+ logDeviceService(devices[i]) |
+ .catch(function(error) { console.error(devices[i].name, error); }); |
+ } |
+ }) |
.catch(function(error) { console.error(error); }); |
}); |
})(); |