Index: chrome/browser/resources/bluetooth_internals/device_collection.js |
diff --git a/chrome/browser/resources/bluetooth_internals/device_collection.js b/chrome/browser/resources/bluetooth_internals/device_collection.js |
index ab62448c12433f64117e8a262ef5bf030596ab47..1e3624b1bedbc75058737b40c0a243fb54af2ee5 100644 |
--- a/chrome/browser/resources/bluetooth_internals/device_collection.js |
+++ b/chrome/browser/resources/bluetooth_internals/device_collection.js |
@@ -6,7 +6,6 @@ |
* Javascript for Device-related features, served from |
* chrome://bluetooth-internals/. |
*/ |
- |
cr.define('device_collection', function() { |
/* |
* Collection of devices. |
@@ -31,6 +30,7 @@ cr.define('device_collection', function() { |
} |
return null; |
}, |
+ |
/** |
* Adds or updates a Device with new DeviceInfo. |
* @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
@@ -52,11 +52,17 @@ cr.define('device_collection', function() { |
this.push(device); |
} |
}, |
+ |
+ /** |
+ * Marks the Device as removed. |
+ * @param {!interfaces.bluetoothDevice.DeviceInfo} deviceInfo |
+ */ |
remove: function(deviceInfo) { |
var device = this.getByAddress(deviceInfo.address); |
assert(device, |
'Device does not exist.'); |
device.removed = true; |
+ this.updateIndex(this.indexOf(device)); |
} |
}; |
@@ -68,6 +74,46 @@ cr.define('device_collection', function() { |
var Device = function(info) { |
this.info = info; |
this.removed = false; |
+ this.connectionPending = false; |
+ }; |
+ Device.prototype = { |
+ /** |
+ * Creates a connection to the device and updates the service listing. |
+ * @return {Promise} rejects if connection failed, resolves otherwise. |
+ */ |
+ connect: function() { |
+ this.connectionPending = true; |
+ return interfaces.DefaultAdapter.connectToDevice( |
+ this.info.address).then( |
+ function(response) { |
+ this.connectionPending = false; |
+ if (response.error == |
+ interfaces.BluetoothAdapter.ConnectResult.SUCCESS) { |
+ this.proxy = interfaces.Connection.bindHandleToProxy( |
+ response.device, |
+ interfaces.BluetoothDevice.Device); |
+ return this.proxy.getServices(); |
+ } |
+ |
+ // TODO(mbrunson): Replace with more descriptive error messages. |
+ var errorString = Object.keys( |
+ interfaces.BluetoothAdapter.ConnectResult)[response.error]; |
+ |
+ throw new Error(errorString); |
+ }.bind(this)).then(function(response) { |
+ this.info.services = response.services; |
+ }.bind(this)); |
+ }, |
+ |
+ /** |
+ * Disconnects a device and removes the Device interface proxy. |
+ */ |
+ disconnect: function() { |
+ if (this.proxy) { |
+ this.proxy.disconnect(); |
+ this.proxy = null; |
+ } |
+ } |
}; |
return { |