BeagleBone Blackで、BLE Beaconを追試る

BeagleBone Blackに入れたUbuntu12.04環境で、ともさんのBLE Beaconを追試る。
https://github.com/tomovwgti/BLEbeacon
USBドングルは、LBT-UAN04C1BKを使った。
前後してしまうが、BTドングルとTI SensorTagの認識方法については、また別途。


まず、git入ってないので、入れないと。

ubuntu@bbby:~$ sudo apt-get install git-core
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  git git-man libasn1-8-heimdal libclass-isa-perl libcurl3-gnutls
  liberror-perl libgcrypt11 libgdbm3 libgnutls26 libgpg-error0
  libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal
  libheimntlm0-heimdal libhx509-5-heimdal libkrb5-26-heimdal libldap-2.4-2
  libp11-kit0 libroken18-heimdal librtmp0 libsasl2-2 libsasl2-modules
  libswitch-perl libtasn1-3 libwind0-heimdal patch perl perl-modules rsync
Suggested packages:
  git-daemon-run git-daemon-sysvinit git-doc git-el git-arch git-cvs git-svn
  git-email git-gui gitk gitweb rng-tools gnutls-bin libsasl2-modules-otp
  libsasl2-modules-ldap libsasl2-modules-sql libsasl2-modules-gssapi-mit
  libsasl2-modules-gssapi-heimdal ed diffutils-doc perl-doc
  libterm-readline-gnu-perl libterm-readline-perl-perl make
  libpod-plainer-perl
The following NEW packages will be installed:
  git git-core git-man libasn1-8-heimdal libclass-isa-perl libcurl3-gnutls
  liberror-perl libgcrypt11 libgdbm3 libgnutls26 libgpg-error0
  libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal
  libheimntlm0-heimdal libhx509-5-heimdal libkrb5-26-heimdal libldap-2.4-2
  libp11-kit0 libroken18-heimdal librtmp0 libsasl2-2 libsasl2-modules
  libswitch-perl libtasn1-3 libwind0-heimdal patch perl perl-modules rsync
0 upgraded, 30 newly installed, 0 to remove and 0 not upgraded.
Need to get 7951 kB/15.0 MB of archives.
After this operation, 42.2 MB of additional disk space will be used.
(snip)


BLE Beaconをcloneしてインストールする。

ubuntu@bbby:~$ git clone https://github.com/tomovwgti/BLEbeacon.git
Cloning into 'BLEbeacon'...
remote: Reusing existing pack: 216, done.
remote: Total 216 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (216/216), 142.99 KiB | 67 KiB/s, done.
Resolving deltas: 100% (65/65), done.

ubuntu@bbby:~$ cd BLEbeacon/
ubuntu@bbby:~/BLEbeacon$ ls
README.md  beacon.js  ble-discovery.js  index.js  lib  package.json
ubuntu@bbby:~/BLEbeacon$ npm install
npm WARN package.json BLEbeacon@0.0.0 No description
npm http GET https://registry.npmjs.org/debug
npm http GET https://registry.npmjs.org/simplesets
npm http GET https://registry.npmjs.org/noble
npm http 200 https://registry.npmjs.org/debug
npm http GET https://registry.npmjs.org/debug/-/debug-0.7.4.tgz
npm http 200 https://registry.npmjs.org/simplesets
npm http GET https://registry.npmjs.org/simplesets/-/simplesets-1.2.0.tgz
npm http 200 https://registry.npmjs.org/noble
npm http GET https://registry.npmjs.org/noble/-/noble-0.2.8.tgz
npm http 200 https://registry.npmjs.org/debug/-/debug-0.7.4.tgz
npm http 200 https://registry.npmjs.org/simplesets/-/simplesets-1.2.0.tgz
npm http 200 https://registry.npmjs.org/noble/-/noble-0.2.8.tgz

> noble@0.2.8 install /home/ubuntu/BLEbeacon/node_modules/noble
> node install.js

noble install: platform is "linux'
noble install: running node-gyp ...
gyp http GET http://nodejs.org/dist/v0.10.21/node-v0.10.21.tar.gz
gyp http 200 http://nodejs.org/dist/v0.10.21/node-v0.10.21.tar.gz
gyp ERR! build error
gyp ERR! stack Error: not found: make
gyp ERR! stack     at F (/usr/local/lib/node_modules/npm/node_modules/which/which.js:43:28)
gyp ERR! stack     at E (/usr/local/lib/node_modules/npm/node_modules/which/which.js:46:29)
gyp ERR! stack     at /usr/local/lib/node_modules/npm/node_modules/which/which.js:57:16
gyp ERR! stack     at Object.oncomplete (fs.js:107:15)
gyp ERR! System Linux 3.8.13-bone30
gyp ERR! command "node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "build"
gyp ERR! cwd /home/ubuntu/BLEbeacon/node_modules/noble
gyp ERR! node -v v0.10.21
gyp ERR! node-gyp -v v0.10.10
gyp ERR! not ok
noble install: done
npm ERR! weird error 1
npm ERR! not ok code 0

エラーだ。node.jsのバージョンアップ中に、makeがないって。
確かにない。
ってことは、gccとかもいるってことか。遠いな。
ビルドには、g++もいるのね。


nodejsパッケージのビルド依存しているパッケージをインストールする。

ubuntu@bbby:~/BLEbeacon$ sudo apt-get build-dep nodejs
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  autoconf automake autotools-dev bash-completion binutils bsdmainutils
  build-essential cdbs cpp cpp-4.6 curl debhelper devscripts dh-apparmor
  dh-buildinfo dh-translations dpkg-dev g++ g++-4.6 gcc gcc-4.6 gettext
  gettext-base groff-base html2text intltool intltool-debian libc-ares-dev
  libc-ares2 libc-dev-bin libc6-dev libcroco3 libcurl3 libdpkg-perl
  libencode-locale-perl libev-dev libev4 libfile-listing-perl libgettextpo0
  libgmp10 libgomp1 libhtml-parser-perl libhtml-tagset-perl libhtml-tree-perl
  libhttp-cookies-perl libhttp-date-perl libhttp-message-perl
  libhttp-negotiate-perl libicu48 libio-socket-ssl-perl liblwp-mediatypes-perl
  liblwp-protocol-https-perl libmpc2 libmpfr4 libnet-http-perl
  libnet-ssleay-perl libpipeline1 libssl-dev libstdc++6-4.6-dev
  libtimedate-perl libunistring0 liburi-perl libv8-3.7.12.22 libv8-dev
  libwww-perl libwww-robotrules-perl libxml-parser-perl linux-libc-dev m4 make
  man-db pkg-config po-debconf python-scour zlib1g-dev
0 upgraded, 75 newly installed, 0 to remove and 0 not upgraded.
Need to get 40.0 MB/41.0 MB of archives.
After this operation, 108 MB of additional disk space will be used.
(snip)

さて、再度やるにはどうしたらいいのかな。
nodeの流儀がよくわかんないな。
もっかいやればいいのかな?

ubuntu@bbby:~/BLEbeacon$ npm install
npm WARN package.json BLEbeacon@0.0.0 No description
npm http GET https://registry.npmjs.org/noble
npm http 304 https://registry.npmjs.org/noble

> noble@0.2.8 install /home/ubuntu/BLEbeacon/node_modules/noble
> node install.js

noble install: platform is "linux'
noble install: running node-gyp ...
make: Entering directory `/home/ubuntu/BLEbeacon/node_modules/noble/build'
  CC(target) Release/obj.target/hci-ble/src/hci-ble.o
../src/hci-ble.c:9:33: fatal error: bluetooth/bluetooth.h: No such file or directory
compilation terminated.
make: *** [Release/obj.target/hci-ble/src/hci-ble.o] Error 1
make: Leaving directory `/home/ubuntu/BLEbeacon/node_modules/noble/build'
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:267:23)
gyp ERR! stack     at ChildProcess.EventEmitter.emit (events.js:98:17)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (child_process.js:789:12)
gyp ERR! System Linux 3.8.13-bone30
gyp ERR! command "node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "build"
gyp ERR! cwd /home/ubuntu/BLEbeacon/node_modules/noble
gyp ERR! node -v v0.10.21
gyp ERR! node-gyp -v v0.10.10
gyp ERR! not ok
noble install: done
npm ERR! weird error 1
npm ERR! not ok code 0

こんどは、nobleビルドするのに、bluetooth/bluetooth.hがないと言う。
libbluetooth-devを入れる。

ubuntu@bbby:~/BLEbeacon$ sudo apt-get install libbluetooth-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  libbluetooth-dev
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 73.8 kB of archives.
After this operation, 318 kB of additional disk space will be used.

再度。

ubuntu@bbby:~/BLEbeacon$ npm install
npm WARN package.json BLEbeacon@0.0.0 No description
npm http GET https://registry.npmjs.org/noble
npm http 304 https://registry.npmjs.org/noble

> noble@0.2.8 install /home/ubuntu/BLEbeacon/node_modules/noble
> node install.js

noble install: platform is "linux'
noble install: running node-gyp ...
make: Entering directory `/home/ubuntu/BLEbeacon/node_modules/noble/build'
  CC(target) Release/obj.target/hci-ble/src/hci-ble.o
  LINK(target) Release/hci-ble
  LINK(target) Release/hci-ble: Finished
  CC(target) Release/obj.target/l2cap-ble/src/l2cap-ble.o
  LINK(target) Release/l2cap-ble
  LINK(target) Release/l2cap-ble: Finished
make: Leaving directory `/home/ubuntu/BLEbeacon/node_modules/noble/build'
noble install: done
noble@0.2.8 node_modules/noble

なんか、node.jsそのものは上がらなかったような...
まぁ、いいか。


BLEデバイスのスキャンをやってみる。

ubuntu@bbby:~/BLEbeacon$ sudo node ble-discovery.js
sudo: unable to resolve host bbby

近くに何もないので、何も表示されない。
TIのSensorTagがあるので、スイッチを入れる。

peripheral discovered (34b1f7d53ce5):
	hello my local name is:
		SensorTag
	can I interest you in any of the following advertised services:
		[]
	my TX power level is:
		0

これがえんえんと画面を流れる。
一応認識している。
ちょっと、これは見てられないね。
^Cで終わる。


スキャンと発見・消失・Proximity変化イベントを見る。

ubuntu@bbby:~/BLEbeacon$ sudo node beacon.js

さっきと同様、何も起きない。
SensorTagのボタンを押して10秒待ってみる。
何もおきない。
こっちはiBeacon専用かな?
lib/blebeacon.jsを見ると、iBeaconしか相手にしてない感じだ。
iBeacon持ってないんだよねぇ...


ble-discovery.jsのメッセージを短くしたい。
とりあえず、SensorTagで表示される分だけでも変更。

ubuntu@bbby:~/BLEbeacon$ cp ble-discovery.js ble-discovery_org.js
ubuntu@bbby:~/BLEbeacon$ vi ble-discovery.js

適当に短くする。


ログに日時も入れたい。date-utilsを入れる。

ubuntu@bbby:~/BLEbeacon$ sudo npm install date-utils
npm WARN package.json BLEbeacon@0.0.0 No description
npm http GET https://registry.npmjs.org/date-utils
npm http 200 https://registry.npmjs.org/date-utils
npm http GET https://registry.npmjs.org/date-utils/-/date-utils-1.2.15.tgz
npm http 200 https://registry.npmjs.org/date-utils/-/date-utils-1.2.15.tgz
date-utils@1.2.15 node_modules/date-utils

こんな感じのパーツを追加。

ubuntu@bbby:~/BLEbeacon$ vi ble-discovery.js
require('date-utils');
var dt = new Date();
var formdate = dt.toFormat("YYYY/MM/DD HH24:MI:SS");
console.log(formdate);


動かしてみる。

ubuntu@bbby:~/BLEbeacon$ sudo node ble-discovery.js
2014/03/18 09:12:40	34b1f7d53ce5	SensorTag

2014/03/18 09:12:40	34b1f7d53ce5	SensorTag

2014/03/18 09:12:41	34b1f7d53ce5	SensorTag

2014/03/18 09:12:41	34b1f7d53ce5	SensorTag

2014/03/18 09:12:41	34b1f7d53ce5	SensorTag

毎秒7回くらい出ている。
一行おきがやだな。


console.log()の改行をやめたい。
こういうときには、util.print()か、process.stdout.write()を使うようだ。
node-utilをrequireするのが面倒なので、process.stdout.write()にしてみる。

ubuntu@bbby:~/BLEbeacon$ sudo node ble-discovery.js
2014/03/18 09:32:21	34b1f7d53ce5	SensorTag
2014/03/18 09:32:21	34b1f7d53ce5	SensorTag
2014/03/18 09:32:21	34b1f7d53ce5	SensorTag
(snip)

できた。