2016-05-20 16:10:40 +00:00
|
|
|
/*
|
|
|
|
* 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: {},
|
2016-05-20 23:51:30 +00:00
|
|
|
|
2016-05-20 16:10:40 +00:00
|
|
|
// Application Constructor
|
|
|
|
initialize: function() {
|
2016-05-20 23:51:30 +00:00
|
|
|
this.bindEvents();
|
|
|
|
}, doScan: function() {
|
2016-05-20 16:10:40 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
$('#tbody').empty();
|
|
|
|
ble.startScan([], function(device) {
|
2016-05-20 23:51:30 +00:00
|
|
|
console.log(JSON.stringify(device));
|
2016-05-20 16:10:40 +00:00
|
|
|
|
2016-05-20 23:51:30 +00:00
|
|
|
var newId = device.id.replace(/:/g, '');
|
|
|
|
console.log(newId);
|
2016-05-20 16:10:40 +00:00
|
|
|
|
2016-05-20 23:51:30 +00:00
|
|
|
this.list[newId] = device.id;
|
2016-05-20 16:10:40 +00:00
|
|
|
|
2016-05-20 23:51:30 +00:00
|
|
|
var newTR = $('<tr id="' + newId + '" class="clickRow">');
|
2016-05-20 16:10:40 +00:00
|
|
|
|
2016-05-20 23:51:30 +00:00
|
|
|
newTR.append($('<td>').text(device.id));
|
2016-05-20 16:10:40 +00:00
|
|
|
|
2016-05-20 23:51:30 +00:00
|
|
|
if (device.hasOwnProperty('name')) {
|
|
|
|
newTR.append($('<td>').text(device.name));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
newTR.append($('<td>').text('*** Unknown'));
|
|
|
|
}
|
2016-05-20 16:10:40 +00:00
|
|
|
|
2016-05-20 23:51:30 +00:00
|
|
|
newTR.append($('<td>').text(device.rssi));
|
2016-05-20 16:10:40 +00:00
|
|
|
|
2016-05-20 23:51:30 +00:00
|
|
|
$('#tbody').append(newTR);
|
2016-05-20 16:10:40 +00:00
|
|
|
|
2016-05-20 23:51:30 +00:00
|
|
|
$('#output').append(JSON.stringify(device) + '<br/>');
|
2016-05-20 16:10:40 +00:00
|
|
|
|
2016-05-20 23:51:30 +00:00
|
|
|
}.bind(this), function(e) {
|
|
|
|
'use strict';
|
|
|
|
console.error(e);
|
|
|
|
});
|
2016-05-20 16:10:40 +00:00
|
|
|
|
|
|
|
setTimeout(ble.stopScan,
|
2016-05-20 23:51:30 +00:00
|
|
|
5000,
|
|
|
|
function() { console.log('Scan complete'); },
|
|
|
|
function() { console.log('stopScan failed'); });
|
2016-05-20 16:10:40 +00:00
|
|
|
|
2016-05-20 23:51:30 +00:00
|
|
|
}, // Bind Event Listeners
|
2016-05-20 16:10:40 +00:00
|
|
|
//
|
|
|
|
// Bind any events that are required on startup. Common events are:
|
|
|
|
// 'load', 'deviceready', 'offline', and 'online'.
|
|
|
|
bindEvents: function() {
|
2016-05-20 23:51:30 +00:00
|
|
|
var self = this;
|
|
|
|
document.addEventListener('deviceready', this.onDeviceReady, false);
|
|
|
|
$('#scan').on('click', function() {
|
|
|
|
'use strict';
|
|
|
|
this.doScan();
|
|
|
|
}.bind(this));
|
2016-05-20 16:10:40 +00:00
|
|
|
|
2016-05-20 23:51:30 +00:00
|
|
|
$('#tbody').on('click', 'tr', function() {
|
|
|
|
'use strict';
|
|
|
|
var tID = $(this).context.id;
|
2016-05-20 16:10:40 +00:00
|
|
|
|
2016-05-20 23:51:30 +00:00
|
|
|
var id = self.list[tID];
|
2016-05-20 16:10:40 +00:00
|
|
|
|
2016-05-20 23:51:30 +00:00
|
|
|
console.log(tID, id);
|
2016-05-20 16:10:40 +00:00
|
|
|
|
2016-05-20 23:51:30 +00:00
|
|
|
self.connect(id);
|
|
|
|
});
|
2016-05-20 16:10:40 +00:00
|
|
|
|
2016-05-20 23:51:30 +00:00
|
|
|
}, // Deviceready Event Handler
|
2016-05-20 16:10:40 +00:00
|
|
|
//
|
|
|
|
// 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);
|
|
|
|
|
2016-05-20 23:51:30 +00:00
|
|
|
}, sensorMpu9250GyroConvert: function(data) {
|
|
|
|
return data / (65536 / 500);
|
2016-05-20 16:10:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
sensorMpu9250AccConvert: function(data) {
|
2016-05-20 23:51:30 +00:00
|
|
|
// Change /2 to match accel range...i.e. 16 g would be /16
|
|
|
|
return data / (32768 / 2);
|
|
|
|
}
|
2016-05-20 16:10:40 +00:00
|
|
|
|
2016-05-20 23:51:30 +00:00
|
|
|
, 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) {
|
2016-05-20 16:10:40 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
app.initialize();
|