Last active
August 29, 2015 14:28
-
-
Save virgilvox/1c66badba421ef672903 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bleno = require('bleno'); | |
var SERVICE_UUID = 'b1a6752152eb4d36e13e357d7c225466'; | |
var CHAR_UUID = '4b842c60ddd611e38b680800200c9a66'; | |
bleno.on('stateChange', function(state) { | |
console.log('on -> stateChange: ' + state); | |
if (state === 'poweredOn') { | |
bleno.startAdvertising('BaleCounter',[SERVICE_UUID]); | |
} else { | |
bleno.stopAdvertising(); | |
} | |
}); | |
var value = 0; | |
var prev; | |
// Changing value here | |
setInterval(function() { | |
// test = value + " Bales"; | |
console.log(value); | |
prev = value; | |
value++; | |
}, 1000); | |
// changing value | |
bleno.on('advertisingStart', function(error) { | |
console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success')); | |
if (!error) { | |
console.log("set services"); | |
bleno.setServices([ | |
new bleno.PrimaryService({ | |
uuid : SERVICE_UUID, | |
characteristics : [ | |
new bleno.Characteristic({ | |
uuid : CHAR_UUID, | |
properties : ['notify'], | |
onSubscribe : function(maxValueSize, updateValueCallback) { | |
// check on interval if the value changed | |
setInterval(function() { | |
if(value != prev){ | |
console.log("doing it"); | |
prev = value; | |
// Sending a buffer with the value and a string attached | |
// to anyone listening to broadcast | |
updateValueCallback(new Buffer(value + " bales")); | |
} | |
}, 900); | |
// close interval | |
} | |
}) | |
] | |
}) | |
]); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment