Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: chrome/browser/resources/bluetooth_internals/bluetooth_internals.js

Issue 2448713002: bluetooth: Add Device connection logic and accompanying user interface. (Closed)
Patch Set: Move removed statement in DeviceCollection.addOrUpdate Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 bluetooth_internals.html, served from 6 * Javascript for bluetooth_internals.html, served from
7 * chrome://bluetooth-internals/. 7 * chrome://bluetooth-internals/.
8 */ 8 */
9 9
10 cr.define('bluetooth_internals', function() { 10 cr.define('bluetooth_internals', function() {
11
12 var deviceAddressToProxy = new Map();
13
11 function initializeViews() { 14 function initializeViews() {
12 var adapterBroker = null; 15 var adapterBroker = null;
13 adapter_broker.getAdapterBroker() 16 adapter_broker.getAdapterBroker()
14 .then(function(broker) { adapterBroker = broker; }) 17 .then(function(broker) { adapterBroker = broker; })
15 .then(function() { return adapterBroker.getInfo(); }) 18 .then(function() { return adapterBroker.getInfo(); })
16 .then(function(response) { console.log('adapter', response.info); }) 19 .then(function(response) { console.log('adapter', response.info); })
17 .then(function() { return adapterBroker.getDevices(); }) 20 .then(function() { return adapterBroker.getDevices(); })
18 .then(function(response) { 21 .then(function(response) {
19 // Hook up device collection events. 22 // Hook up device collection events.
20 var devices = new device_collection.DeviceCollection([]); 23 var devices = new device_collection.DeviceCollection([]);
21 adapterBroker.addEventListener('deviceadded', function(event) { 24 adapterBroker.addEventListener('deviceadded', function(event) {
22 devices.addOrUpdate(event.detail.deviceInfo); 25 devices.addOrUpdate(event.detail.deviceInfo);
23 }); 26 });
24 adapterBroker.addEventListener('devicechanged', function(event) { 27 adapterBroker.addEventListener('devicechanged', function(event) {
25 devices.addOrUpdate(event.detail.deviceInfo); 28 devices.addOrUpdate(event.detail.deviceInfo);
26 }); 29 });
27 adapterBroker.addEventListener('deviceremoved', function(event) { 30 adapterBroker.addEventListener('deviceremoved', function(event) {
28 devices.remove(event.detail.deviceInfo); 31 devices.remove(event.detail.deviceInfo);
29 }); 32 });
30 33
31 response.devices.forEach(devices.addOrUpdate, 34 response.devices.forEach(devices.addOrUpdate,
32 devices /* this */); 35 devices /* this */);
33 36
34 var deviceTable = new device_table.DeviceTable(); 37 var deviceTable = new device_table.DeviceTable();
38
39 deviceTable.addEventListener('inspectpressed', function(event) {
40 var address = event.detail.address;
41 var proxy = deviceAddressToProxy.get(address);
42
43 if (proxy) {
44 // Device is already connected, so disconnect.
45 device.disconnect();
46 deviceAddressToProxy.delete(address);
47 devices.updateConnectionStatus(address, 'disconnected');
48 } else {
49 devices.updateConnectionStatus(address, 'connecting');
50 adapterBroker.connectToDevice(address).then(function(deviceProxy) {
51 if (!devices.getByAddress(address)) {
52 // Device no longer in list, so drop the connection.
53 deviceProxy.disconnect();
54 return;
55 }
56
57 deviceAddressToProxy.set(address, deviceProxy);
58 devices.updateConnectionStatus(address, 'connected');
59
60 // Fetch services asynchronously.
61 deviceProxy.getServices().then(function(response) {
ortuno 2016/11/10 02:16:38 Should this information be saved in the DeviceDeta
mbrunson 2016/11/10 04:18:29 Yes. This will be cached in the DeviceDetailsView.
62 var deviceInfo = devices.getByAddress(address);
63 deviceInfo.services = response.services;
64 devices.addOrUpdate(deviceInfo);
65 });
66 }).catch(function(error) {
67 devices.updateConnectionStatus(address, 'disconnected', error);
68 });
69 }
70 });
71
35 deviceTable.setDevices(devices); 72 deviceTable.setDevices(devices);
36 document.body.appendChild(deviceTable); 73 document.body.appendChild(deviceTable);
37 }) 74 })
38 .catch(function(error) { console.error(error); }); 75 .catch(function(error) { console.error(error); });
39 } 76 }
40 77
41 return { 78 return {
42 initializeViews: initializeViews 79 initializeViews: initializeViews
43 }; 80 };
44 81
45 }); 82 });
46 83
47 document.addEventListener( 84 document.addEventListener(
48 'DOMContentLoaded', bluetooth_internals.initializeViews); 85 'DOMContentLoaded', bluetooth_internals.initializeViews);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698