Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var noble = require('noble');
  2.  
  3. var bufferpack = require('bufferpack');
  4.  
  5.  
  6.  
  7. noble.on('stateChange', function(state) {
  8.   if (state === 'poweredOn') {
  9.     //
  10.     // Once the BLE radio has been powered on, it is possible
  11.     // to begin scanning for services. Pass an empty array to
  12.     // scan for all services (uses more time and power).
  13.     //
  14.     console.log('scanning...');
  15.     noble.startScanning([], false);
  16.   }
  17.   else {
  18.     noble.stopScanning();
  19.   }
  20. })
  21.  
  22. var started, resetTimeoutHandle, resetTimeout = 1000,
  23.         clicks = 0;
  24.  
  25. started = new Date();
  26.     clicks = 0;
  27.  
  28.     function clicksPerSecond(started, clicks) {
  29.     return clicks / ((new Date()) - started) * 1000;
  30. }
  31.  
  32. var format = '<h(a_x)h(a_y)h(a_z)h(g_x)h(g_y)h(g_z)';
  33.  
  34. var count = 0;
  35.  
  36. noble.on('discover', function(peripheral) {
  37.     if (peripheral.uuid == 'c23d48a715f1433d9cd011199ab5f77a') {
  38.         noble.stopScanning();
  39.         peripheral.connect(function(err) {
  40.             //
  41.             // Once the peripheral has been connected, then discover the
  42.             // services and characteristics of interest.
  43.             //
  44.             peripheral.discoverServices(['180d'], function(err, services) {
  45.                 services.forEach(function(service) {
  46.                     //
  47.                     // This must be the service we were looking for.
  48.                     //
  49.                     console.log('found service:', service.uuid);
  50.  
  51.                     //
  52.                     // So, discover its characteristics.
  53.                     //
  54.                     service.discoverCharacteristics(['2A37'], function(err, characteristics) {
  55.  
  56.                         characteristics.forEach(function(characteristic) {
  57.                             //
  58.                             // Loop through each characteristic and match them to the
  59.                             // UUIDs that we know about.
  60.                             //
  61.                             console.log('found characteristic:', characteristic.uuid);
  62.  
  63.                             if(characteristic.uuid=='2a37'){
  64.                                  characteristic.subscribe(function(err) {
  65.                                    started = new Date();
  66.                                   clicks = 0;
  67.                                     console.log('found characteristic subscribe ', err);
  68.  
  69.                                  })
  70.                             }
  71.                               characteristic.on('data', function(data, isNotification){
  72.                                 clearTimeout(resetTimeoutHandle);
  73.                                  clicks++;
  74.  
  75.  
  76.                                   console.log('found characteristic subscribe ', data, bufferpack.unpack(format, data.toString('binary'), 0) ,clicksPerSecond(started, clicks),count);
  77.                                   count++;
  78.                               });
  79.                         })
  80.                     })
  81.                 })
  82.             })
  83.         })
  84.     }
  85.  
  86. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement