Skip to content

Instantly share code, notes, and snippets.

@libbymiller
Last active February 13, 2016 14:17
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 libbymiller/de44a4a56114b8ea9230 to your computer and use it in GitHub Desktop.
Save libbymiller/de44a4a56114b8ea9230 to your computer and use it in GitHub Desktop.
// by @dirkx
// Apache 2 license http://www.apache.org/licenses/LICENSE-2.0
// see http://www.byteworks.us/Byte_Works/Blog/Entries/2012/8/20_Controlling_Bluetooth_LE_Devices_with_techBASIC.html
// you may need to fiddle with lines 20 and 25 - alternatives are given!
"use strict";
var noble = require('noble');
noble.on('discover', function(peripheral) {
if (peripheral.advertisement.localName == "MLE-15") {
console.log("Got an MLE-15 "+ peripheral.id);
peripheral.connect(function(error) {
console.log('connected to peripheral: ' + peripheral.uuid);
peripheral.discoverServices('ffe0', function(error, services) {
// peripheral.discoverServices(['ffe0'], function(error, services) {
var buttons = services[0];
buttons.discoverCharacteristics('ffe1', function(error, characteristics) {
// buttons.discoverCharacteristics(['ffe1'], function(error, characteristics) {
var buttonState = characteristics[0];
console.log("buttonState");
console.log(buttonState);
console.log("Registing to be notified");
buttonState.notify(true, function(error) {
console.trace('Failed to get notify going: ', error);
});
buttonState.on('read', function(data, isNotification) {
console.log('Button was pressed: ', data.readUInt8(0), " of device ", peripheral.uuid, " (",isNotification,")");
});
}); // discoverd charactersitics.
}); // discover services
}); // while we're connected.
} else {
console.log("Ignoring ", peripheral.id);
};
});
noble.on('stateChange', function(state) {
if (state === 'poweredOn') {
console.log("Started scanning..");
noble.startScanning();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment