| 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 b9af7fb9c83e1c876d8d6555e259545d87b986da..b3bf252d1810b62e24cca38278720787c80042fe 100644
|
| --- a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js
|
| +++ b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js
|
| @@ -7,27 +7,53 @@
|
| * chrome://bluetooth-internals/.
|
| */
|
|
|
| -/**
|
| - * The implementation of AdapterClient in
|
| - * device/bluetooth/public/interfaces/adapter.mojom.
|
| - */
|
| -var AdapterClient = function() {};
|
| -AdapterClient.prototype = {
|
| - /**
|
| - * Prints added device to console.
|
| - * @param {!bluetoothDevice.DeviceInfo} device
|
| - */
|
| - deviceAdded: function(device) { console.log('Device added', device); },
|
| +(function() {
|
| + var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection;
|
| +
|
| + // Dictionary for address->Device cache.
|
| + var devices = {};
|
| +
|
| + // Data model for a cached device.
|
| + function Device() {};
|
| + Device.prototype = { info: null, proxy: null };
|
|
|
| /**
|
| - * Prints removed device to console.
|
| - * @param {!bluetoothDevice.DeviceInfo} device
|
| + * The implementation of AdapterClient in
|
| + * device/bluetooth/public/interfaces/adapter.mojom.
|
| */
|
| - deviceRemoved: function(device) { console.log('Device removed', device); }
|
| -};
|
| -
|
| -(function() {
|
| - var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection;
|
| + var AdapterClient = function() {};
|
| + AdapterClient.prototype = {
|
| + /**
|
| + * Logs added device to console, caches the device info, and sets the
|
| + * GattClient.
|
| + * @param {!bluetoothDevice.DeviceInfo} device
|
| + */
|
| + deviceAdded: function(device) {
|
| + console.log('Device added', device);
|
| + devices[device.address] = new Device();
|
| + devices[device.address].info = device;
|
| + },
|
| +
|
| + /**
|
| + * Logs removed device to console and removes the cached device.
|
| + * @param {!bluetoothDevice.DeviceInfo} device
|
| + */
|
| + deviceRemoved: function(device) {
|
| + console.log('Device removed', device);
|
| + delete devices[device.address];
|
| + },
|
| +
|
| + /**
|
| + * Logs received advertising packet to console. caches the last valid
|
| + * RSSI, and refreshes the device list.
|
| + * @param {!bluetoothDevice.AdvertisingPacket} advertisingPacket the
|
| + * advertising packet received from the device.
|
| + */
|
| + deviceChanged: function(deviceInfo) {
|
| + console.log(new Date(), deviceInfo);
|
| + devices[deviceInfo.address].info.rssi = deviceInfo.rssi;
|
| + }
|
| + };
|
|
|
| /**
|
| * TODO(crbug.com/652361): Move to shared location.
|
| @@ -62,11 +88,11 @@ AdapterClient.prototype = {
|
|
|
| // Hook up the instance properties.
|
| AdapterClient.prototype.__proto__ =
|
| - bluetoothAdapter.AdapterClient.stubClass.prototype;
|
| + bluetoothAdapter.AdapterClient.stubClass.prototype;
|
|
|
| var adapterFactory = connection.bindHandleToProxy(
|
| - frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name),
|
| - bluetoothAdapter.AdapterFactory);
|
| + frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name),
|
| + bluetoothAdapter.AdapterFactory);
|
|
|
| // Get an Adapter service.
|
| return adapterFactory.getAdapter();
|
| @@ -76,7 +102,7 @@ AdapterClient.prototype = {
|
| }
|
|
|
| adapter = connection.bindHandleToProxy(response.adapter,
|
| - bluetoothAdapter.Adapter);
|
| + bluetoothAdapter.Adapter);
|
|
|
| // Create a message pipe and bind one end to client
|
| // implementation and the other to the Adapter service.
|
| @@ -85,38 +111,19 @@ AdapterClient.prototype = {
|
| });
|
| }
|
|
|
| - /**
|
| - * Prints device info from the device service.
|
| - * @param {!bluetoothDevice.DeviceInfo} deviceInfo the device for which to
|
| - * get the information.
|
| - * @return {!Promise} resolves if device service is retrieved, rejects
|
| - * otherwise.
|
| - */
|
| - function logDevice(deviceInfo) {
|
| - return adapter.getDevice(deviceInfo.address).then(function(response) {
|
| - var deviceHandle = response.device;
|
| - if (!deviceHandle) {
|
| - throw new Error(deviceInfo.name_for_display + ' cannot be found.');
|
| - }
|
| -
|
| - var device = connection.bindHandleToProxy(
|
| - deviceHandle, bluetoothDevice.Device);
|
| - return device.getInfo();
|
| - }).then(function(response) {
|
| - console.log(deviceInfo.name_for_display, response.info);
|
| - });
|
| - }
|
| -
|
| document.addEventListener('DOMContentLoaded', function() {
|
| initializeProxies()
|
| .then(function() { return adapter.getInfo(); })
|
| .then(function(response) { console.log('adapter', response.info); })
|
| .then(function() { return adapter.getDevices(); })
|
| .then(function(response) {
|
| - var devices = response.devices;
|
| - console.log('devices', devices.length);
|
| + console.log('devices', response.devices.length);
|
|
|
| - return Promise.all(devices.map(logDevice));
|
| + response.devices.forEach(function(deviceInfo) {
|
| + devices[deviceInfo.address] = new Device();
|
| + devices[deviceInfo.address].info = deviceInfo;
|
| + console.log(deviceInfo.name_for_display, deviceInfo);
|
| + });
|
| })
|
| .catch(function(error) { console.error(error); });
|
| });
|
|
|