Last active
October 17, 2018 14:42
-
-
Save gfwilliams/c1a9ff0ae3023a933a2af63f4b7673b4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Node.js | |
var bleno = require('bleno'); | |
var MSG = { | |
LED_COLOR: 1, | |
MSG_NEXT: 2, | |
MSG_NOW: 3, | |
MSG_ALERT: 4, | |
MSG_INFO: 5 | |
} | |
var messages = [ | |
[ MSG.LED_COLOR, "\0\0\0" ], | |
[ MSG.MSG_NEXT, "Mr. Flibbles"], | |
[ MSG.MSG_NOW, "Someone interesting"] | |
]; | |
var msgIndex = 0; | |
function setAdvert(msgType, msgText) { | |
var piData = [4+msgText.length,0xFF,0x90,0x05,msgType]; | |
for (var i=0;i<msgText.length;i++) piData.push(msgText.charCodeAt(i)); | |
var advertisementData = new Buffer(piData.length); | |
for (var i=0;i<piData.length;i++) advertisementData.writeUInt8(piData[i], i); | |
bleno.startAdvertisingWithEIRData(advertisementData, function(err) { | |
if (err) console.log("startAdvertising",err); | |
}); | |
} | |
function swapAdvertising() { | |
msgIndex++; | |
if (msgIndex>=messages.length) msgIndex=0; | |
if (messages[msgIndex]) | |
bleno.stopAdvertising(function() { | |
setAdvert(messages[msgIndex][0], messages[msgIndex][1]); | |
}); | |
} | |
function startAdvertising() { | |
console.log("Starting advertising"); | |
setInterval(swapAdvertising, 110); | |
} | |
bleno.on('stateChange', function(state) { | |
console.log("STATE => "+state); | |
if (state=="poweredOn") | |
startAdvertising(); | |
}); | |
// Espruino | |
var MSG = { | |
RESERVED : 0, | |
LED_COLOR: 1, | |
MSG_NEXT: 2, | |
MSG_NOW: 3, | |
MSG_ALERT: 4, | |
MSG_INFO: 5 | |
}; | |
var whitelist = [ | |
"b8:27:eb:31:6f:66 public", | |
]; | |
var bleData = []; | |
function gotData(msg,v) { | |
console.log(Object.keys(MSG)[msg]+" => "+JSON.stringify(v)); | |
} | |
function doScan() { | |
var d = []; | |
packets = 0; | |
setTimeout(function() { | |
NRF.setScan(); | |
console.log(packets+" received"); | |
for (var k in d) { | |
if (bleData[k]!=d[k]) { | |
bleData[k] = d[k]; | |
gotData(k,d[k]); | |
} | |
} | |
}, 1000); | |
console.log("Scanning"); | |
NRF.setScan(function(device) { | |
var m = device.manufacturerData; | |
if (whitelist.indexOf(device.id)<0 || !m) return; | |
packets++; | |
d[m[0]] = E.toString(new Uint8Array(m,1)); | |
}, { filters: [{ manufacturerData:{0x0590:{}} }] }); | |
} | |
setInterval(doScan, 60000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment