sensortoy/www/js/standards/battery.js

62 lines
1.3 KiB
JavaScript
Raw Normal View History

2016-05-20 16:10:40 +00:00
/**
*
* User: Martin Donnelly
* Date: 2016-05-20
* Time: 10:13
*
*/
/* global CAPABILITY */
/* global ble */
/* jshint browser: true , devel: true*/
var BATTERY = function() {
this.name = 'Battery';
this.capabilityID = '180F';
this.serviceDef = {
2016-05-27 08:25:10 +00:00
service: '180F', level: '2A19'
};
2016-05-20 16:10:40 +00:00
this.onBatteryLevelChange = function(data) {
2016-05-27 08:25:10 +00:00
console.log(data);
var a = new Uint8Array(data);
this.state = a[0];
console.log('onBatteryLevelChange', this.state);
};
2016-05-20 16:10:40 +00:00
this.readBatteryState = function() {
2016-05-27 08:25:10 +00:00
console.log('readBatteryState');
ble.read(this.deviceID,
this.serviceDef.service,
this.serviceDef.level,
this.onReadBatteryLevel.bind(this),
this.onError);
};
2016-05-20 16:10:40 +00:00
this.onReadBatteryLevel = function(data) {
2016-05-27 08:25:10 +00:00
console.log(data);
var a = new Uint8Array(data);
this.state = a[0];
console.log('onReadBatteryLevel', this.state);
};
2016-05-20 16:10:40 +00:00
this.startService = function() {
2016-05-27 08:25:10 +00:00
'use strict';
if (this.deviceID !== null) {
console.log('Starting Battery Service on ', this.deviceID);
console.log(this.serviceDef);
2016-05-20 16:10:40 +00:00
2016-05-27 08:25:10 +00:00
this.insertFrame();
2016-05-20 16:10:40 +00:00
2016-05-27 08:25:10 +00:00
ble.startNotification(this.deviceID,
this.serviceDef.service,
this.serviceDef.level,
this.onBatteryLevelChange.bind(this),
this.onError);
}
2016-05-20 16:10:40 +00:00
2016-05-27 08:25:10 +00:00
};
2016-05-20 16:10:40 +00:00
};
inheritsFrom(BATTERY, CAPABILITY);