2014年1月12日

BluetoothLowEnergy(BLE)の勉強 - その1 - Linux+BlueZでBLE通信を試す

Bluetooth Low Energey (BLE) の個人勉強用に、LinuxでBLE通信を試してみました。
その手順等について以下記載します。

環境
ThinkPad X201上のGentooLinux環境で試しました。Linuxカーネルは3.10.17、BlueZ(Bluetoothスタック)は4.101をインストールしています。
ThinkPad X201はBLE対応(Bluetooth Ver4.0以降)のホストを搭載していないので、USBドングル(Princeton PTM-UBT7)を接続しました。通信相手となるBLEデバイスには、iPhone対応しているふるタグ(Logitec LBT-MPVRU01BK)を使います。

BLEデバイスのスキャン
コマンドラインでhcitoolを使ってスキャンします。
  $ hcitool lescan
以下のような出力が続き、LBT-MPVRU01BKが見つかりました。
  44:13:19:02:F8:A9 LBT-VRU01
  44:13:19:02:F8:A9 (unknown)
LBT-MPVRU01BKのBluetoothアドレスは、44:13:19:02:F8:A9ということがわかります。

デバイスへのアクセス
見つかったアドレスに対して、gatttoolを使ってアクセスしてみます。"-I"オプションをつけることでインタラクティブに通信操作を行うことができます。(太字の部分が実際に入力した箇所です)
  $ gatttool -i hci0 -b 44:13:19:02:F8:A9 -I
  [   ][44:13:19:02:F8:A9][LE]> connect
  [CON][44:13:19:02:F8:A9][LE]> primary
  [CON][44:13:19:02:F8:A9][LE]>
  attr handle: 0x0001, end grp handle: 0x0007 uuid: 00001800-0000-1000-8000-00805f9b34fb
  attr handle: 0x0008, end grp handle: 0x000a uuid: 00001803-0000-1000-8000-00805f9b34fb
  attr handle: 0x000b, end grp handle: 0x000d uuid: 00001802-0000-1000-8000-00805f9b34fb
  attr handle: 0x000e, end grp handle: 0x0010 uuid: 00001804-0000-1000-8000-00805f9b34fb
  attr handle: 0x0011, end grp handle: 0xffff uuid: 0000180f-0000-1000-8000-00805f9b34fb
  [CON][44:13:19:02:F8:A9][LE]> characteristics
  [CON][44:13:19:02:F8:A9][LE]>
handle: 0x0002, char properties: 0x02, char value handle: 0x0003, uuid: 00002a00-0000-1000-8000-00805f9b34fb
handle: 0x0004, char properties: 0x02, char value handle: 0x0005, uuid: 00002a01-0000-1000-8000-00805f9b34fb
handle: 0x0006, char properties: 0x02, char value handle: 0x0007, uuid: 00002a04-0000-1000-8000-00805f9b34fb
handle: 0x0009, char properties: 0x0a, char value handle: 0x000a, uuid: 00002a06-0000-1000-8000-00805f9b34fb
handle: 0x000c, char properties: 0x04, char value handle: 0x000d, uuid: 00002a06-0000-1000-8000-00805f9b34fb
handle: 0x000f, char properties: 0x02, char value handle: 0x0010, uuid: 00002a07-0000-1000-8000-00805f9b34fb
handle: 0x0012, char properties: 0x02, char value handle: 0x0013, uuid: 00002a19-0000-1000-8000-00805f9b34fb
handle: 0x0014, char properties: 0x02, char value handle: 0x0015, uuid: 00002a1a-0000-1000-8000-00805f9b34fb
handle: 0x0016, char properties: 0x12, char value handle: 0x0017, uuid: 00002a1b-0000-1000-8000-00805f9b34fb
handle: 0x0019, char properties: 0x02, char value handle: 0x001a, uuid: 00002a3a-0000-1000-8000-00805f9b34fb
  [CON][44:13:19:02:F8:A9][LE]> char-write-cmd 0x000d 02
  [CON][44:13:19:02:F8:A9][LE]> char-write-cmd 0x000d 01

最後の2つのコマンドで、実際にBLEデバイス(LBT-MPVRU01BK)に対する通信操作を行うことができました。
"char-write-cmd 0x000d 02"でLBT-MPVRU01BKからメロディが流れます。
また、"char-write-cmd 0x000d 01"でLBT-MPVRU01BKのLEDが点滅します。

gatttoolでの操作および出力がBLEの肝になるような雰囲気ですが、これらについては後日記載したいと思います。

参考ページ
スキャンや通信に関しては、以下のページを参考にしました。(ほぼそのままです…)
http://i-miss-erin.blogspot.jp/2010/12/gatttool-in-bluez-over-bredr.html


0 件のコメント: