Skip to content

Instantly share code, notes, and snippets.

@marufeuille
Last active July 19, 2016 04:03
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 marufeuille/af6a85e95e98ae5ae3c82d8fed00a508 to your computer and use it in GitHub Desktop.
Save marufeuille/af6a85e95e98ae5ae3c82d8fed00a508 to your computer and use it in GitHub Desktop.
BLESerial2を使って、Arduinoからセンサーデータを読みだしてみる ref: http://qiita.com/marufeuille/items/7da0ee1eb726847e6aea
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.write("hello");
delay(1000);
}
var noble = require("noble");
noble.on("stateChange", function(state) {
console.log("on -> stateChange" + state);
if (state == "poweredOn") {
noble.startScanning([],false, function (err) { console.log(err); });
}
else {
noble.stopScanning();
}
});
noble.on("scanStart", function() {
console.log("on -> scanStart");
});
noble.on("scanStop", function() {
console.log("on -> scanStop");
});
noble.on("discover", function(peripheral) {
console.log("on -> discover: " + peripheral);
noble.stopScanning();
peripheral.on("connect", function () {
console.log("on -> connect");
this.discoverServices();
});
peripheral.on("disconnect", function () {
console.log("on -> disconnect");
});
peripheral.on("servicesDiscover", function(services) {
console.log("on -> services discover " + services);
for (var i = 0; i < services.length; i ++) {
services[i].on("includedServicesDiscover", function (includedServiceUUIDs) {
console.log("on -> service included services discovered " + includedServiceUUIDs);
this.discoverCharacteristics();
});
services[i].on("characteristicsDiscover", function (characteristics) {
console.log("on -> characteristics discover " + characteristics);
for (var i = 0; i < characteristics.length; i ++) {
characteristics[i].on("data", function (data, isNotification) {
console.log("data: " + data);
});
characteristics[i].subscribe();
}
});
services[i].discoverIncludedServices();
}
});
peripheral.connect();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment