Skip to content

Instantly share code, notes, and snippets.

@ma2shita
Last active August 29, 2015 14:11
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 ma2shita/c5daa68069837639c374 to your computer and use it in GitHub Desktop.
Save ma2shita/c5daa68069837639c374 to your computer and use it in GitHub Desktop.
TI SensorTag (CC2541) SimpleKey(button) Service sample Program on node.js
/* $ npm install sensortag (require `libbluetooth-dev`) */
var SensorTag = require('sensortag');
var net = require('net');
var myUUID = process.env["TI_UUID"] || "YOUR_TI_sensor_tag_UUID";
console.log("start");
console.log("waiting for connect from " + myUUID);
SensorTag.discover(function(sensorTag) {
console.log(">> STOP: Ctrl+C or SensorTag power off");
sensorTag.connect(function() {
console.log("found: discovery service ... (very slow)");
sensorTag.discoverServicesAndCharacteristics(function() {
sensorTag.readDeviceName(function(deviceName) {
console.log("connect: " + deviceName);
sensorTag.notifySimpleKey(function() {
console.log("ready: notifySimpleKey");
console.log("// left right (x = pushed, o = opened) //");
sensorTag.on("simpleKeyChange", function(left, right) { /* run per pushed button */
var btn_st = 0x00; /* ボタンが押された時のみ送信するようにするためのフィルタ変数 */
var send_str = "";
if (left) {
btn_st += 0x10;
send_str += " x";
} else {
send_str += " o";
}
if (right) {
btn_st += 0x01;
send_str += " x";
} else {
send_str += " o";
}
if (0x00 != (btn_st & 0x11)) {
console.log(send_str);
}
});
});
});
});
});
/* In case of SensorTag PowerOff or out of range when fired `onDisconnect` */
sensorTag.on("disconnect", function() {
console.log("disconnect");
process.exit(0);
});
}, myUUID);
@ma2shita
Copy link
Author

$ sudo apt-get install libbluetooth-dev
$ npm install sensortag
$ sudo node ./ti_sensortag_simplekey.js 
>> STOP: Ctrl+C or SensorTag power off
found: discovery service ...
connect: TI BLE Sensor Tag
start: notifySimpleKey
// left right (x = pushed, o = opened) //
    o     x
    x     o
    x     o

@ma2shita
Copy link
Author

ma2shita commented Apr 3, 2015

TI_UUID=UUID node ti_sensortag_simplekey.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment