/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /* global BATTERY, BUTTON */ var app = { activeServices: [], list: {}, // Application Constructor initialize: function() { this.bindEvents(); }, doScan: function() { 'use strict'; $('#tbody').empty(); ble.startScan([], function(device) { console.log(JSON.stringify(device)); var newId = device.id.replace(/:/g, ''); console.log(newId); this.list[newId] = device.id; var newTR = $(''); newTR.append($('').text(device.id)); if (device.hasOwnProperty('name')) { newTR.append($('').text(device.name)); } else { newTR.append($('').text('*** Unknown')); } newTR.append($('').text(device.rssi)); $('#tbody').append(newTR); $('#output').append(JSON.stringify(device) + '
'); }.bind(this), function(e) { 'use strict'; console.error(e); }); setTimeout(ble.stopScan, 5000, function() { console.log('Scan complete'); }, function() { console.log('stopScan failed'); }); }, // Bind Event Listeners // // Bind any events that are required on startup. Common events are: // 'load', 'deviceready', 'offline', and 'online'. bindEvents: function() { var self = this; document.addEventListener('deviceready', this.onDeviceReady, false); $('#scan').on('click', function() { 'use strict'; this.doScan(); }.bind(this)); $('#tbody').on('click', 'tr', function() { 'use strict'; var tID = $(this).context.id; var id = self.list[tID]; console.log(tID, id); self.connect(id); }); }, // Deviceready Event Handler // // The scope of 'this' is the event. In order to call the 'receivedEvent' // function, we must explicitly call 'app.receivedEvent(...);' onDeviceReady: function() { }, serviceDiscovery: function(services) { 'use strict'; console.log(services); }, sensorMpu9250GyroConvert: function(data) { return data / (65536 / 500); }, sensorMpu9250AccConvert: function(data) { // Change /2 to match accel range...i.e. 16 g would be /16 return data / (32768 / 2); } , connect: function(deviceId) { $('#results').slideUp(); console.log('Connect to ', deviceId); var onConnect = function(a) { var services = []; 'use strict'; console.log('A:', a); services = a.services; for (var t = 0; t < services.length; t++) { var ident = services[t].toUpperCase(); switch (ident) { case '180F': var batteryStat = new BATTERY(deviceId); batteryStat.startService(); batteryStat.readBatteryState(); app.activeServices.push(batteryStat); break; case 'FFE0': var buttonState = new BUTTON(deviceId); buttonState.startService(); app.activeServices.push(buttonState); break; case 'F000AA80-0451-4000-B000-000000000000': var cc2650_accel = new CC2650_ACCEL(deviceId); cc2650_accel.startService(); app.activeServices.push(cc2650_accel); break; case 'F000AA40-0451-4000-B000-000000000000': var cc2650_bar = new CC2650_BAR(deviceId); cc2650_bar.startService(); app.activeServices.push(cc2650_bar); break; case 'F000AA70-0451-4000-B000-000000000000': var cc2650_lux = new CC2650_LUX(deviceId); cc2650_lux.startService(); app.activeServices.push(cc2650_lux); break; case 'F000AA00-0451-4000-B000-000000000000': var cc2650_tmp = new CC2650_TMP(deviceId); cc2650_tmp.startService(); app.activeServices.push(cc2650_tmp); break; case 'F000AA20-0451-4000-B000-000000000000': var cc2650_hum = new CC2650_HUM(deviceId); cc2650_hum.startService(); app.activeServices.push(cc2650_hum); break; default: console.log('Unknown service: ', ident); } } }; ble.connect(deviceId, onConnect, function(e) { 'use strict'; console.error(e); }); }, onError: function(reason) { console.error('ERROR: ' + reason); // Real apps should use notification.alert }, updateGyro: function(g) { 'use strict'; } }; app.initialize();