Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Battery Service 0x180F example #76

Merged
merged 1 commit into from
Oct 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/battery-service/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
11 changes: 11 additions & 0 deletions examples/battery-service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# BLE Battery Service

This example provides a BLE battery service (0x180F) for your Mac.

Install dependencies

npm install

Run the example

node main.js
46 changes: 46 additions & 0 deletions examples/battery-service/battery-level-characteristic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var util = require('util'),
os = require('os'),
exec = require('child_process').exec,
bleno = require('bleno'),
Descriptor = bleno.Descriptor,
Characteristic = bleno.Characteristic;

var BatteryLevelCharacteristic = function() {
BatteryLevelCharacteristic.super_.call(this, {
uuid: '2A19',
properties: ['read'],
descriptors: [
new Descriptor({
uuid: '2901',
value: 'Battery level between 0 and 100 percent'
}),
new Descriptor({
uuid: '2904',
value: new Buffer([0x04, 0x01, 0x27, 0xAD, 0x01, 0x00, 0x00 ]) // maybe 12 0xC unsigned 8 bit
})
]
});
};

util.inherits(BatteryLevelCharacteristic, Characteristic);

BatteryLevelCharacteristic.prototype.onReadRequest = function(offset, callback) {

if (os.platform() === 'darwin') {
exec('pmset -g batt', function (error, stdout, stderr) {

var data = stdout.toString();
// data - 'Now drawing from \'Battery Power\'\n -InternalBattery-0\t95%; discharging; 4:11 remaining\n'
var percent = data.split('\t')[1].split(';')[0];
console.log(percent);
percent = parseInt(percent, 10);
callback(this.RESULT_SUCCESS, new Buffer([percent]));

});
} else {
// return hardcoded value
callback(this.RESULT_SUCCESS, new Buffer([98]));
}
};

module.exports = BatteryLevelCharacteristic;
17 changes: 17 additions & 0 deletions examples/battery-service/battery-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var util = require('util'),
bleno = require('bleno'),
BlenoPrimaryService = bleno.PrimaryService,
BatteryLevelCharacteristic = require('./battery-level-characteristic');

function BatteryService() {
BatteryService.super_.call(this, {
uuid: '180F',
characteristics: [
new BatteryLevelCharacteristic()
]
});
}

util.inherits(BatteryService, BlenoPrimaryService);

module.exports = BatteryService;
22 changes: 22 additions & 0 deletions examples/battery-service/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var bleno = require('bleno'),
BatteryService = require('./battery-service');

var primaryService = new BatteryService();

bleno.on('stateChange', function(state) {
console.log('on -> stateChange: ' + state);

if (state === 'poweredOn') {
bleno.startAdvertising('Battery', [primaryService.uuid]);
} else {
bleno.stopAdvertising();
}
});

bleno.on('advertisingStart', function(error) {
console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success'));

if (!error) {
bleno.setServices([primaryService]);
}
});
20 changes: 20 additions & 0 deletions examples/battery-service/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "battery-service",
"version": "0.1.0",
"description": "BLE (Bluetooth Low Energy) Battery Service",
"main": "index.js",
"engines": {
"node": ">=0.10"
},
"os": [
"darwin",
"linux"
],
"author": {
"name": "Don Coleman",
"url" : "https://github.com/don"
},
"dependencies": {
"bleno": ">0.1.4"
}
}