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