wonder gadget

ガジェットはインターネットの夢をみるか? Intel edisonを中心に IoT, MAKE, Physical Computingしていきます。

BLEのアドバタイズを受信する

EdisonやRaspberry PiでBLEのセントラルになる場合、Node.jsのモジュールnobleが便利です。

sandeepmistry/noble · GitHub

以下のコマンドでnobleをインストールします。

npm install noble

次にGetting startedを参考にコードを書きます。

https://github.com/sandeepmistry/noble/wiki/Getting-started

var noble = require('noble');

noble.on('stateChange', function(state) {
  if (state === 'poweredOn') {
    noble.startScanning();
  } else {
    noble.stopScanning();
  }
});

noble.on('discover', function(peripheral) {
    console.log('Found device with local name: ' + peripheral.advertisement.localName);
    console.log('advertising the following service uuid\'s: ' + peripheral.advertisement.serviceUuids);
    console.log();
});

stateがpoweredOnのときにstartScanning()を呼んでいます。 アドバタイズを受信するとnoble.on()のdiscoverに来ますので、受信したペリフェラルの内容を表示します。 ここではlocalNameとサービスのUUIDを表示しています。

BLEのペリフェラルデバイスを持っていなくてもNode.jsのモジュールblenoを利用することでアドバタイズを発信することができます。

sandeepmistry/bleno · GitHub

MacやiPhoneにもBLEのアドバタイズ(iBeaconなど)を発信することができるアプリがあるので探してみてください。

参考

mqtt - WICED SenseをIntel Edisonから使う - Qiita