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