Skip to content

Instantly share code, notes, and snippets.

@libbymiller libbymiller/bt_button.js
Last active Feb 13, 2016

Embed
What would you like to do?
// 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
You can’t perform that action at this time.