Last active
August 29, 2015 14:11
-
-
Save ma2shita/c5daa68069837639c374 to your computer and use it in GitHub Desktop.
TI SensorTag (CC2541) SimpleKey(button) Service sample Program on node.js
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
/* $ 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TI_UUID=UUID node ti_sensortag_simplekey.js