Skip to content

Instantly share code, notes, and snippets.

@jweinberg
Created July 31, 2016 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jweinberg/d3d43c886bec167ba42ba72ab38ac78e to your computer and use it in GitHub Desktop.
Save jweinberg/d3d43c886bec167ba42ba72ab38ac78e to your computer and use it in GitHub Desktop.
var noble = require('noble');
var serviceUuids = ['6e400001b5a3f393e0a9e50e24dcca9e']
var characteristicUuids = ['6e400002b5a3f393e0a9e50e24dcca9e']
noble.on('stateChange', function(state) {
if (state === 'poweredOn') {
noble.startScanning(serviceUuids, false);
} else {
noble.stopScanning();
}
});
noble.on('discover', function(peripheral) {
console.log('peripheral with ID ' + peripheral.id + ' found');
peripheral.once('connect', function() {
console.log("Connected");
peripheral.on('servicesDiscover', function(services) {
var service = services[0];
console.log('Found service ' + service)
service.on('characteristicsDiscover', function(characteristics) {
var characteristic = characteristics[0];
console.log("characteristi found " + characteristics);
var i = 0;
setInterval(function() {
const buf = Buffer([i, 0xFF, 0xFF]);
characteristic.write(buf, true);
i = (i + 1) % 255
}, 100);
});
console.log('Discovering characteristics');
service.discoverCharacteristics(characteristicUuids)
});
peripheral.discoverServices(serviceUuids);
});
peripheral.connect();
console.log();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment