1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 var BluetoothDevice = function() {
13 // The text label of the selected device class.
14 this.class = 'Computer';
16 // The uint32 value of the selected device class.
17 this.classValue = 0x104;
19 this.isTrusted = true;
23 // The label of the selected pairing method option.
24 this.pairingMethod = 'None';
26 // The text containing a PIN key or passkey for pairing.
27 this.pairingAuthToken = '';
31 is: 'bluetooth-settings',
35 * The title to be displayed in a heading element for the element.
42 * A set of bluetooth devices.
43 * @type !Array<!BluetoothDevice>
47 value: function() { return []; }
51 * A set of options for the possible bluetooth device classes/types.
52 * Object |value| attribute comes from values in the WebUI, set in
53 * setDeviceClassOptions.
54 * @type !Array<! {text: string, value: int} >
59 return [{ text: 'Unknown', value: 0 },
60 { text: 'Mouse', value: 0x2580 },
61 { text: 'Keyboard', value: 0x2540 },
62 { text: 'Audio', value: 0x240408 },
63 { text: 'Phone', value: 0x7a020c },
64 { text: 'Computer', value: 0x104 }];
69 * A set of strings representing the method to be used for
70 * authenticating a device during a pair request.
71 * @type !Array<string>
73 deviceAuthenticationMethods: {
75 value: function() { return ['None', 'PIN Code', 'PassKey']; }
79 * Contains keys for all the device paths which have been discovered. Used
80 * to look up whether or not a device is listed already.
85 value: function() { return {}; }
90 this.title = 'Bluetooth Settings';
94 * Checks whether or not the PIN/passkey input field should be shown.
95 * It should only be shown when the pair method is not 'None' or empty.
96 * @param {string} pairMethod The label of the selected pair method option
97 * for a particular device.
98 * @return {boolean} Whether the PIN/passkey input field should be shown.
100 showAuthToken: function(pairMethod) {
101 return pairMethod && pairMethod != 'None';
105 * Called by the WebUI which provides a list of devices which are connected
106 * to the main adapter.
107 * @param {!Array<!BluetoothDevice>} devices A list of bluetooth devices.
109 updateBluetoothInfo: function(devices) {
110 /** @type {!Array<!BluetoothDevice>} */ var deviceList = [];
112 for (var i = 0; i < devices.length; ++i) {
113 // Get the label for the device class which should be selected.
114 devices[i].class = this.getTextForDeviceClass(devices[i]['classValue']);
115 deviceList.push(devices[i]);
116 this.devicePaths[devices[i]['path']] = true;
119 this.devices = deviceList;
122 pairDevice: function(e) {
123 var device = this.devices[e.path[2].dataIndex];
124 device.classValue = this.getValueForDeviceClass(device.class);
125 this.devicePaths[device.path] = true;
127 // Send device info to the WebUI.
128 chrome.send('requestBluetoothPair', [device]);
131 discoverDevice: function(e) {
132 var device = this.devices[e.path[2].dataIndex];
133 device.classValue = this.getValueForDeviceClass(device.class);
134 this.devicePaths[device.path] = true;
136 // Send device info to WebUI.
137 chrome.send('requestBluetoothDiscover', [device]);
140 // Adds a new device with default settings to the list of devices.
141 appendNewDevice: function() {
142 this.push('devices', new BluetoothDevice());
146 * This is called when a new device is discovered by the main adapter.
147 * The device is only added to the view's list if it is not already in
148 * the list (i.e. its path has not yet been recorded in |devicePaths|).
149 * @param {BluetoothDevice} device A bluetooth device.
151 addBluetoothDevice: function(device) {
152 if (this.devicePaths[device['path']] != undefined)
155 device.class = this.getTextForDeviceClass(device['classValue']);
156 this.push('devices', device);
157 this.devicePaths[device['path']] = true;
161 * Removes the bluetooth device with path |path|.
162 * @param {string} path A bluetooth device's path.
164 removeBluetoothDevice: function(path) {
165 if (this.devicePaths[path] == undefined)
168 for (var i = 0; i < this.devices.length; ++i) {
169 if (this.devices[i].path == path) {
170 this.splice('devices', i, 1);
177 * Returns the text for the label that corresponds to |classValue|.
178 * @param {number} classValue A number representing the bluetooth class
180 * @return {string} The label which represents |classValue|.
182 getTextForDeviceClass: function(classValue) {
183 for (var i = 0; i < this.deviceClassOptions.length; ++i) {
184 if (this.deviceClassOptions[i].value == classValue)
185 return this.deviceClassOptions[i].text;
190 * Returns the integer value which corresponds with the label |classText|.
191 * @param {string} classText The label for a device class option.
192 * @return {number} The value which |classText| represents.
194 getValueForDeviceClass: function(classText) {
195 for (var i = 0; i < this.deviceClassOptions.length; ++i) {
196 if (this.deviceClassOptions[i].text == classText)
197 return this.deviceClassOptions[i].value;