Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var noble = require('noble');
- var bufferpack = require('bufferpack');
- noble.on('stateChange', function(state) {
- if (state === 'poweredOn') {
- //
- // Once the BLE radio has been powered on, it is possible
- // to begin scanning for services. Pass an empty array to
- // scan for all services (uses more time and power).
- //
- console.log('scanning...');
- noble.startScanning([], false);
- }
- else {
- noble.stopScanning();
- }
- })
- var started, resetTimeoutHandle, resetTimeout = 1000,
- clicks = 0;
- started = new Date();
- clicks = 0;
- function clicksPerSecond(started, clicks) {
- return clicks / ((new Date()) - started) * 1000;
- }
- var format = '<h(a_x)h(a_y)h(a_z)h(g_x)h(g_y)h(g_z)';
- var count = 0;
- noble.on('discover', function(peripheral) {
- if (peripheral.uuid == 'c23d48a715f1433d9cd011199ab5f77a') {
- noble.stopScanning();
- peripheral.connect(function(err) {
- //
- // Once the peripheral has been connected, then discover the
- // services and characteristics of interest.
- //
- peripheral.discoverServices(['180d'], function(err, services) {
- services.forEach(function(service) {
- //
- // This must be the service we were looking for.
- //
- console.log('found service:', service.uuid);
- //
- // So, discover its characteristics.
- //
- service.discoverCharacteristics(['2A37'], function(err, characteristics) {
- characteristics.forEach(function(characteristic) {
- //
- // Loop through each characteristic and match them to the
- // UUIDs that we know about.
- //
- console.log('found characteristic:', characteristic.uuid);
- if(characteristic.uuid=='2a37'){
- characteristic.subscribe(function(err) {
- started = new Date();
- clicks = 0;
- console.log('found characteristic subscribe ', err);
- })
- }
- characteristic.on('data', function(data, isNotification){
- clearTimeout(resetTimeoutHandle);
- clicks++;
- console.log('found characteristic subscribe ', data, bufferpack.unpack(format, data.toString('binary'), 0) ,clicksPerSecond(started, clicks),count);
- count++;
- });
- })
- })
- })
- })
- })
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement