Skip to content

Instantly share code, notes, and snippets.

@inderpreet
Created July 1, 2017 17:46
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 inderpreet/c87ed94b7bd57d64b261bd7091a173e9 to your computer and use it in GitHub Desktop.
Save inderpreet/c87ed94b7bd57d64b261bd7091a173e9 to your computer and use it in GitHub Desktop.
var noble = require('noble');
var ble_UUID =['171a3182907143bfb53a1689b132a533', '9d0bfe96c7bb46c59b35d9abc398c587']; // add your UUIDs to this list...
var ble_serices = 'ffb0';
var red_char = 'ffb1';
var green_char = 'ffb2';
var blue_char = 'ffb3';
var white_char = 'ffb4';
var R=0;
var G=0;
var B=0;
var W=0;
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();
}
else {
noble.stopScanning();
}
})
setInterval( function(){
if(noble.state==='poweredOn'){
noble.startScanning();
console.log('Starting Scan...');
setTimeout(function(){
noble.stopScanning();
console.log('Stopping Scan...');
}, 2500)
}
}, 3000);
setInterval(function(){W= W + 5; if(R>100) R=0;}, 3000);
noble.on('discover', function(peripheral) {
// we found a peripheral, stop scanning
//noble.stopScanning();
//R = 00;
//G = 00;
//B = 00;
//W = 00;
//console.log('found peripheral:', peripheral.advertisement);
var advertisement = peripheral.advertisement;
var localName = advertisement.localName;
var txPowerLevel = advertisement.txPowerLevel;
var manufacturerData = advertisement.manufacturerData;
var serviceData = advertisement.serviceData;
var serviceUuids = advertisement.serviceUuids;
ble_UUID.forEach(function(temp){
if(peripheral.id === temp){
peripheral.connect(function(err){
setTimeout(function(){peripheral.disconnect(function(err){});}, 1000);
// lets connect...
peripheral.discoverServices([ble_serices], function(err, service) {
// ask about the services and then...
service.forEach(function(service){
console.log('found service:', service.uuid);
//discover characteristics
service.discoverCharacteristics([], function(err, characteristics) {
characteristics.forEach(function(characteristic) {
var mybuff = new Buffer(1);
switch(characteristic.uuid){
case red_char:
mybuff.writeUInt8(R,0); //write 0 at 0 offset
break;
case green_char:
mybuff.writeUInt8(G,0); //write 0 at 0 offset
break;
case blue_char:
mybuff.writeUInt8(B,0); //write 0 at 0 offset
break;
case white_char:
mybuff.writeUInt8(W,0); //write 0 at 0 offset
break;
default:
mybuff.writeUInt8(0,0); //write 0 at 0 offset
break;
} // end switch
console.log('found characteristic:', characteristic.uuid);
characteristic.write( mybuff, true, function(err){ });
}); // End for each characteristic
}); // End Service discovery
}); //End Service for each
}); // End discovery listed services
}); // End Connect
} // End IF Peripheral ID select
}); // End For each
}); // End on Noble Discover!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment