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 Device-related features, served from | 6 * Javascript for Device-related features, served from |
7 * chrome://bluetooth-internals/. | 7 * chrome://bluetooth-internals/. |
8 */ | 8 */ |
9 | 9 |
10 cr.define('device_collection', function() { | 10 cr.define('device_collection', function() { |
(...skipping 14 matching lines...) Expand all Loading... |
25 * @param {!string} address | 25 * @param {!string} address |
26 */ | 26 */ |
27 getByAddress: function(address) { | 27 getByAddress: function(address) { |
28 for (var i = 0; i < this.length; i++) { | 28 for (var i = 0; i < this.length; i++) { |
29 var device = this.item(i); | 29 var device = this.item(i); |
30 if (address == device.info.address) | 30 if (address == device.info.address) |
31 return device; | 31 return device; |
32 } | 32 } |
33 return null; | 33 return null; |
34 }, | 34 }, |
| 35 |
35 /** | 36 /** |
36 * Adds or updates a Device with new DeviceInfo. | 37 * Adds or updates a Device with new DeviceInfo. |
37 * @param {!interfaces.bluetoothDevice.DeviceInfo} deviceInfo | 38 * @param {!interfaces.bluetoothDevice.DeviceInfo} deviceInfo |
38 */ | 39 */ |
39 addOrUpdate: function(deviceInfo) { | 40 addOrUpdate: function(deviceInfo) { |
40 var oldDevice = this.getByAddress(deviceInfo.address); | 41 var oldDevice = this.getByAddress(deviceInfo.address); |
41 if (oldDevice) { | 42 if (oldDevice) { |
42 // Update rssi if it's valid | 43 // Update rssi if it's valid |
43 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) || | 44 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) || |
44 (oldDevice.info.rssi && oldDevice.info.rssi.value); | 45 (oldDevice.info.rssi && oldDevice.info.rssi.value); |
45 | 46 |
46 oldDevice.info = deviceInfo; | 47 oldDevice.info = deviceInfo; |
47 oldDevice.info.rssi = { value: rssi }; | 48 oldDevice.info.rssi = { value: rssi }; |
48 oldDevice.removed = false; | 49 oldDevice.removed = false; |
49 | 50 |
50 this.updateIndex(this.indexOf(oldDevice)); | 51 this.updateIndex(this.indexOf(oldDevice)); |
51 } else { | 52 } else { |
52 var device = new Device(deviceInfo); | 53 var device = new Device(deviceInfo); |
53 this.push(device); | 54 this.push(device); |
54 } | 55 } |
55 }, | 56 }, |
| 57 |
| 58 /** |
| 59 * Marks the Device as removed. |
| 60 * @param {!interfaces.bluetoothDevice.DeviceInfo} deviceInfo |
| 61 */ |
56 remove: function(deviceInfo) { | 62 remove: function(deviceInfo) { |
57 var device = this.getByAddress(deviceInfo.address); | 63 var device = this.getByAddress(deviceInfo.address); |
58 assert(device, | 64 assert(device, |
59 'Device does not exist.'); | 65 'Device does not exist.'); |
60 device.removed = true; | 66 device.removed = true; |
| 67 this.updateIndex(this.indexOf(device)); |
61 } | 68 } |
62 }; | 69 }; |
63 | 70 |
64 /* | 71 /* |
65 * Data model for a cached device. | 72 * Data model for a cached device. |
66 * @constructor | 73 * @constructor |
67 * @param {!interfaces.bluetoothDevice.DeviceInfo} info | 74 * @param {!interfaces.bluetoothDevice.DeviceInfo} info |
68 */ | 75 */ |
69 var Device = function(info) { | 76 var Device = function(info) { |
70 this.info = info; | 77 this.info = info; |
71 this.removed = false; | 78 this.removed = false; |
72 }; | 79 }; |
| 80 Device.prototype = { |
| 81 /** |
| 82 * Creates a connection to the device and updates the service listing. |
| 83 * @return {Promise} rejects if connection failed, resolves otherwise. |
| 84 */ |
| 85 connect: function() { |
| 86 return interfaces.DefaultAdapter.connectToDevice( |
| 87 this.info.address).then( |
| 88 function(response) { |
| 89 if (response.error == |
| 90 interfaces.BluetoothAdapter.ConnectErrorCode.SUCCESS) { |
| 91 this.proxy = interfaces.Connection.bindHandleToProxy( |
| 92 response.device, |
| 93 interfaces.BluetoothDevice.Device); |
| 94 return this.proxy.getServices(); |
| 95 } |
| 96 |
| 97 // TODO(mbrunson): Replace with more descriptive error messages. |
| 98 var errorString = Object.keys( |
| 99 interfaces.BluetoothAdapter.ConnectErrorCode)[response.error]; |
| 100 |
| 101 throw new Error(errorString); |
| 102 }.bind(this)).then(function(response) { |
| 103 this.info.services = response.services; |
| 104 }.bind(this)); |
| 105 }, |
| 106 |
| 107 /** |
| 108 * Disconnects a device and removes the Device interface proxy. |
| 109 */ |
| 110 disconnect: function() { |
| 111 if (this.proxy) { |
| 112 this.proxy.disconnect(); |
| 113 this.proxy = null; |
| 114 } |
| 115 } |
| 116 }; |
73 | 117 |
74 return { | 118 return { |
75 DeviceCollection, DeviceCollection, | 119 DeviceCollection, DeviceCollection, |
76 }; | 120 }; |
77 }); | 121 }); |
OLD | NEW |