2 * Copyright (c) 2013, Intel Corporation.
4 * This program is licensed under the terms and conditions of the
5 * Apache License, version 2.0. The full text of the Apache License is at
6 * http://www.apache.org/licenses/LICENSE-2.0
10 function bluetoothPanelInit() {
12 settings.bluetooth.subscribeEvents(bluetoothEventReceived);
14 /* Bluetooth Settings Panel */
15 $('#page_bluetooth').on('pageshow', function(event, data) {
16 if (data.prevPage.attr('id') === 'page_bluetooth_detail') return;
18 var adapter = settings.bluetooth.getDefaultAdapter();
19 if (adapter === null) {
20 showMsg('Error', 'Bluetooth adapter not found');
23 console.log('Default BT adapter: ', adapter.name);
25 if (adapter.name != undefined) {
26 $('#label_bluetooth_adapter').html(adapter.name);
28 $('#label_bluetooth_adapter').html('Bluetooth adapter not found');
31 if (adapter.powered) {
33 bluetoothStartScan(adapter);
39 $('#toggle_bluetooth').change(function() {
40 var bt_switch = $(this);
41 var adapter = settings.bluetooth.getDefaultAdapter();
42 if (adapter === null) {
43 showMsg('Error', 'Bluetooth adapter not found');
48 if (bt_switch[0].selectedIndex === 0) {
49 console.log('Stop bluetooth scanning');
50 bluetoothStopScan(adapter);
51 /* first disable adapter, then disable bluetooth */
52 adapter.setPowered(false, function() {
54 console.log('Power off adapter success, adapter is powered: ' + adapter.powered);
55 settings.bluetooth.disableBluetooth(function() {
57 console.log('Bluetooth subsystem turned off');
60 console.log('Failed to turn off bluetooth subsystem: ' + e);
61 $('#toggle_bluetooth').val('on').slider('refresh');
62 showMsg('Error', 'Cannot turn off bluetooth subsystem: ' + e);
66 console.log('Power off adapter failed, adapter is powered: ' + adapter.powered);
67 $('#toggle_bluetooth').val('on').slider('refresh');
68 showMsg('Error', 'Cannot turn off bluetooth adapter: ' + e);
71 /* first enable bluetooth, then enable adapter */
72 settings.bluetooth.enableBluetooth(function() {
73 console.log('Bluetooth subsystem turned on');
75 adapter.setPowered(true, function() {
77 console.log('Power on adapter success, adapter is powered: ' + adapter.powered);
79 console.log('Start scanning...');
80 bluetoothStartScan(adapter);
83 $('#toggle_bluetooth').val('off').slider('refresh');
84 showMsg('Error', 'Cannot turn on bluetooth adapter: ' + e);
88 $('#toggle_bluetooth').val('off').slider('refresh');
89 showMsg('Error', 'Cannot turn on bluetooth subsystem: ' + e);
94 $('#button_bluetooth_scan').on('click', function() {
95 var adapter = settings.bluetooth.getDefaultAdapter();
96 if (adapter === null) {
97 showMsg('Error', 'Bluetooth adapter not found');
100 if ($('#button_bluetooth_scan').text() === 'Scan') {
101 bluetoothStartScan(adapter);
103 bluetoothStopScan(adapter);
107 /* Bluetooth device detail page */
108 $('#page_bluetooth_detail').on('pageshow', function(event, data) {
109 console.log('Entering bluetooth detail info page');
110 var device_id = localStorage.getItem('bluetooth_device_id');
111 if (device_id == undefined) return;
112 var device = $(jqId(device_id)).data('device-object');
113 bluetoothConstructDetailPanel(device);
117 function bluetoothEventReceived(event) {
118 if (event.type !== WS_EVENT_TYPE.BLUETOOTH) return;
119 console.log('Bluetooth event received: [' + event.name + ', ' + event.value + ']');
122 function bluetoothClearAvailableList() {
123 $('#listview_device_available').html('');
126 function bluetoothStartScan(adapter) {
127 /* clear the device list with new scan */
128 bluetoothClearAvailableList();
129 adapter.startScanDevices({
131 onstarted: function() {
132 console.log('Bluetooth scan started...');
133 $('#button_bluetooth_scan .ui-btn-text').text('Stop');
135 ondevicefound: function(device) {
136 console.log('Bluetoth device found - name: ' + device.name + ', address: ' + device.address + ', is paired: ' + device.paired + ', is connected: ' + device.connected);
137 bluetoothUpdateDevice(device);
139 ondevicedisappeared: function(address) {
140 if ($(jqId(address)).length) {
141 bluetoothRemoveFromAvailableList(address);
142 console.log('Remove device element from page ' + address);
145 onfinished: function(devices) {
146 $('#button_bluetooth_scan .ui-btn-text').text('Scan');
147 for (var i = 0; i < devices.length; i++) {
148 if (!$(jqId(devices[i].address)).length) {
149 bluetoothUpdateDevice(devices[i]);
155 showMsg('Error', 'Cannot scan: ' + e);
159 function bluetoothRefreshList() {
160 $('#listview_device_paired').listview('refresh');
161 $('#listview_device_available').listview('refresh');
164 function bluetoothStopScan(adapter) {
165 adapter.stopScanDevices(function() {
167 console.log('Bluetooth scan canceled');
168 $('#button_bluetooth_scan .ui-btn-text').text('Scan');
171 showMsg('Error', 'Cannot cancel scan: ' + e);
175 function bluetoothAppendToPairedList(device) {
176 console.log('Append to paired list - ' + device.address);
177 if ($('#listview_device_paired').find(jqId(device.address)).length != 0) return;
179 var parent = '#listview_device_paired';
180 bluetoothConstructDeviceElement(parent, device);
181 bluetoothUpdateDeviceButton(device);
182 bluetoothRefreshList();
185 function bluetoothRemoveFromPairedList(device_addr) {
186 var removeThis = $('#listview_device_paired li').filter(function() {
187 return $(this).find(jqId(device_addr)).length === 1;
190 if (removeThis.length !== 0) {
191 console.log('Remove from paired list - ' + device_addr);
193 bluetoothRefreshList();
197 function bluetoothAppendToAvailableList(device) {
198 console.log('Append to available list - ' + device.address);
199 if ($('#listview_device_available').find(jqId(device.address)).length != 0) return;
201 var parent = '#listview_device_available';
202 bluetoothConstructDeviceElement(parent, device);
203 bluetoothUpdateDeviceButton(device);
204 bluetoothRefreshList();
207 function bluetoothRemoveFromAvailableList(device_addr) {
208 var removeThis = $('#listview_device_available li').filter(function() {
209 return $(this).find(jqId(device_addr)).length === 1;
212 if (removeThis.length !== 0) {
213 console.log('Remove from available list: ' + device_addr);
215 bluetoothRefreshList();
219 function bluetoothConstructDeviceElement(parent, device) {
220 var html = '<li data-icon="false"><a href="#" id="' + jqId(device.address).replace('#', '') + '">';
221 html += '<img src="images/bluetooth.png" class="device-icon ui-li-icon"></img>';
222 html += '<div class="device-name">' + device.name + '</div>';
223 html += '<div class="device-address">' + device.address + '</div>';
224 html += '<div class="device-status"></div>';
225 html += '<div data-role="button" class="device-action-button ui-li-aside" data-inline="true"></div>';
227 $(parent).append(html).trigger('create');
229 /* store device object in the element so we can reference it later */
230 $(jqId(device.address)).data('device-object', device);
231 console.log('Storing device object: ' + jqId(device.address));
233 $(jqId(device.address)).on('click', function() {
234 localStorage.setItem('bluetooth_device_id', device.address);
235 $.mobile.changePage('#page_bluetooth_detail');
238 $(jqId(device.address)).find('div.device-action-button').on('click', function(e) {
239 var parent = $(this).parent().attr('id');
240 console.log('device action button clicked');
243 * prevent the click event to propagate up
245 e.stopImmediatePropagation();
248 var adapter = settings.bluetooth.getDefaultAdapter();
249 if (adapter === null) return;
251 /* retrieve the device object from element */
252 var device = $(jqId(parent)).data('device-object');
253 if (device == undefined) {
254 console.error('Bluetooth device not found');
258 if (!device.paired) {
259 createPopupDialog(false, false, 'Pair with', device.name, 'Pair', 'Cancel', function() {
260 console.log('Bluetooth pair with device: ' + device.address);
261 showSpinner(false, 'Pairing...');
262 adapter.pairDevice(device.address, function() {
265 device.paired = true;
267 /* changing from unpaired to paried */
268 bluetoothUpdateDevice(device);
272 showMsg('Error', 'Bluetooth pair failed: ' + e);
275 } else if (device.paired && !device.connected) {
276 console.log('Bluetooth connect with device: ' + device.address);
277 showMsg('Error', 'Not suppported');
278 } else if (device.connected) {
279 console.log('Bluetooth disconnect with device: ' + device.address);
280 showSpinner(false, 'Disconnecting...');
281 // adapter.disconnectDevice(device.address, function() {
282 setTimeout(function() {
285 device.connected = false;
286 bluetoothUpdateDevice(device);
292 function bluetoothUpdateDevice(device) {
293 console.log('Constructing or updating bluetooth device');
295 /* reposition device if it's paired or unpaired */
297 bluetoothRemoveFromAvailableList(device.address);
298 bluetoothAppendToPairedList(device);
300 bluetoothRemoveFromPairedList(device.address);
301 bluetoothAppendToAvailableList(device);
304 /* update device button for allowed action */
305 bluetoothUpdateDeviceButton(device);
307 /* update device connection status */
308 bluetoothUpdateConnectionStatus(device);
311 function bluetoothUpdateDeviceButton(device) {
313 if (device.connected) {
314 $(jqId(device.address)).find('div.device-action-button').find('span').text('Disconnect');
316 $(jqId(device.address)).find('div.device-action-button').find('span').text('Connect');
319 $(jqId(device.address)).find('div.device-action-button').find('span').text('Pair');
323 function bluetoothUpdateConnectionStatus(device) {
324 var status = 'unpaired';
326 $(jqId(device.address)).addClass('device-paired');
327 if (device.connected) {
328 status = 'connected';
329 $(jqId(device.address)).addClass('device-connected');
331 status = 'disconnected';
332 $(jqId(device.address)).removeClass('device-connected');
335 $(jqId(device.address)).removeClass('device-paired');
336 $(jqId(device.address)).removeClass('device-connected');
339 bluetoothUpdateConnectionStatusText(device, status);
342 function bluetoothUpdateConnectionStatusText(device, status) {
343 $(jqId(device.address)).find('div.device-status').text(status);
346 function bluetoothConstructDetailPanel(device) {
347 var status_paired = 'No';
348 var status_connected = 'No';
350 if (device == null) return;
351 if (device.paired) status_paired = 'Yes';
352 if (device.connected) status_connected = 'Yes';
354 $('#page_bluetooth_detail_content').html('');
355 var html = '<ul data-role="listview" id="listview_bluetooth_detail" data-inset="true" ' + 'class="device-list ui-listview">';
356 html += '<li id="bluetooth_detail_name"><h2>Device Name: ' + device.name + '</h2></li>';
357 html += '<li id="bluetooth_detail_address"><h2>Device Address: ' + device.address + '</h2></li>';
358 html += '<li id="bluetooth_detail_paired"><h2>Paired: ' + status_paired + '</h2></li>';
359 html += '<li id="bluetooth_detail_connected"><h2>Connected: ' + status_connected + '</h2></li>';
361 $('#page_bluetooth_detail_content').append(html).trigger('create');
364 html = '<div data-role="button" id="button_bluetooth_unpair">Unpair</div>';
365 $('#page_bluetooth_detail_content').append(html).trigger('create');
367 $('#button_bluetooth_unpair').on('click', function(e) {
368 console.log('Bluetooth unpair with device: ' + device.address);
369 var adapter = settings.bluetooth.getDefaultAdapter();
370 if (adapter === null) return;
372 createPopupDialog(false, false, 'Unpair with device', device.name, 'Unpair', 'Cancel', function() {
373 showSpinner(false, 'Unpairing...');
374 adapter.unpairDevice(device.address, function() {
377 device.paired = false;
378 device.connected = false;
379 $('#button_bluetooth_unpair').remove();
381 /* changing from paired to unparied */
382 bluetoothUpdateDevice(device);
383 setTimeout(function() {
384 $.mobile.changePage('#page_bluetooth');
389 showMsg('Error', 'Bluetooth unpair failed: ' + e);
391 /* Something is wrong, remove from paired list */
392 bluetoothRemoveFromPairedList(device.address);
397 $('#listview_bluetooth_detail').listview('refresh');
400 function bluetoothUpdateDetailPanel(device) {
401 var status_paired = 'No';
402 var status_connected = 'No';
404 if (device == null) return;
405 if (device.paired) status_paired = 'Yes';
406 if (device.connected) status_connected = 'Yes';
407 $('#bluetooth_detail_paired').text(connected);
408 $('#bluetooth_detail_connected').text(status_connected);
409 $('#listview_bluetooth_detail').listview('refresh');
412 function bluetoothToggleOn() {
413 $('#bluetooth_devices').show();
414 $('#toggle_bluetooth').val('on').slider('refresh');
417 function bluetoothToggleOff() {
418 $('#bluetooth_devices').hide();
419 $('#toggle_bluetooth').val('off').slider('refresh');