Bluezのアプリケーションはユーザー空間から BlueZ の設定や制御に使うもので、hciconfig 、hcidump 、hcitoolなどがあります。hciconfig は Ethernet や WiFi で言うところの ifconfig や iwconfig に近く、Bluetooth アダプタの起動や停止、動作設定などを行います。hcidump は、ターミナル上の Bluetooth パケットの内容の表示および Bluetooth デバイス間を流れるトラフィックをキャプチャするのに使えます。hcitool には様々な機能が入っていますが、多用するのは周辺デバイスを検索する inq と scan で、これは WiFi で言うところの iwlist に近い機能です。

次にhcidumpとhcitoolの使用法について説明します。

hcidumpによるBluetooth packetの解析

Bluezのhcidump ユーティリティは tcpdump と似ています。 これはターミナル上の Bluetooth パケットの内容の表示および Bluetooth パケットのHCI データをファイルにダンプするために使えます。hcidump -Rを起動したまま、SensorTagに対してgatttoolコマンドを実行すると、Bluetooth パケットが得られます。

$  sudo hcidump -R &
[1] 25307
$ HCI sniffer - Bluetooth packet analyzer ver 5.21
device: hci0 snap_len: 1500 filter: 0xffffffff
$ sudo gatttool -b  B4:99:4C:64:CD:DF --interactive
[B4:99:4C:64:CD:DF][LE]> connect
Attempting to connect to B4:99:4C:64:CD:DF
< 01 0D 20 19 60 00 30 00 00 00 DF CD 64 4C 99 B4 00 28 00 38 00 00 00 2A 00 00 00 00 00 04 0F 04 00 01 0D 20 04 3E 13 01 00 47 00 00 00 DF CD 64 4C 99 B4 38 00 00 00 2A 00 00
 
[B4:99:4C:64:CD:DF][LE]> >
Connection successful
[B4:99:4C:64:CD:DF][LE]> char-read-hnd 0x25
[B4:99:4C:64:CD:DF][LE]> < 02 47 00 07 00 03 00 04 00 0A 25 00 04 13 05 01 47 00 01 00 02 47 20 09 00 05 00 04 00 0B 00 00 00 00

Characteristic value/descriptor: 00 00 00 00
[B4:99:4C:64:CD:DF][LE]> char-write-cmd 0x29 01
[B4:99:4C:64:CD:DF][LE]> < 02 47 00 08 00 04 00 04 00 52 29 00 01 04 13 05 01 47 00 01 00

char-read-hnd 0x25
[B4:99:4C:64:CD:DF][LE]> < 02 47 00 07 00 03 00 04 00 0A 25 00 04 13 05 01 47 00 01 00 02 47 20 09 00 05 00 04 00 0B 94 FE B8 0C

Characteristic value/descriptor: 94 fe b8 0c
[B4:99:4C:64:CD:DF][LE]> exit
$ < 01 06 04 03 47 00 13 04 0F 04 00 01 06 04 04 05 04 00 47 00 16

注:hcidumpからのデータは、見やすいようにまとめています。

Bluetooth パケットのフォーマット

フォーマットの仕様書はこちらに示されています。
BLUETOOTH SPECIFICATION Version 4.0 [Vol 0]

ただの16進のダンプだと解析するのに難しそうです。

GATT構造

  • GATTは複数のグループで構成され、サービス宣言アトリビュート 「0x2800」となります。コマンド「gatttool --primary」で一覧が表示できます。
  • 各グループに含まれるアトリビュートは、表示された一覧の「attr handle」と「end grp handle」をパラメータにして、コマンド「gatttool --char-desc」で表示できます。
  • アトリビュートにはサービス宣言「0x2800」、キャラクタリスティク宣言「0x2803」-読み書き可能か等を示すproperty, キャラクタリスティクvalue の uuid などを含む)、キャラクタリスティクの値(Value)、キャラクタリスティクのディスクリプタ(Descriptor「0x2902」- notify や indicate が可能なキャラクタリスティク)等の種類があります。
  • キャラクタリスティク宣言「0x2803」があれば、後に必ずキャラクタリスティクのvalueが1つはつき、コマンド「gatttool --characteristics」 で把握できます。

gatttoolコマンドでSensorTagに対してprimaryの要求

SensorTagに対して、gatttoolコマンドでprimaryを要求してみると次のようになります。

$ sudo gatttool -b B4:99:4C:64:CD:DF --interactive
[B4:99:4C:64:CD:DF][LE]> connect
Attempting to connect to B4:99:4C:64:CD:DF
Connection successful
[B4:99:4C:64:CD:DF][LE]> primary
attr handle: 0x0001, end grp handle: 0x000b uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle: 0x000c, end grp handle: 0x000f uuid: 00001801-0000-1000-8000-00805f9b34fb
attr handle: 0x0010, end grp handle: 0x0022 uuid: 0000180a-0000-1000-8000-00805f9b34fb
attr handle: 0x0023, end grp handle: 0x002a uuid: f000aa00-0451-4000-b000-000000000000
attr handle: 0x002b, end grp handle: 0x0035 uuid: f000aa10-0451-4000-b000-000000000000
attr handle: 0x0036, end grp handle: 0x003d uuid: f000aa20-0451-4000-b000-000000000000
attr handle: 0x003e, end grp handle: 0x0048 uuid: f000aa30-0451-4000-b000-000000000000
attr handle: 0x0049, end grp handle: 0x0054 uuid: f000aa40-0451-4000-b000-000000000000
attr handle: 0x0055, end grp handle: 0x005c uuid: f000aa50-0451-4000-b000-000000000000
attr handle: 0x005d, end grp handle: 0x0061 uuid: 0000ffe0-0000-1000-8000-00805f9b34fb
attr handle: 0x0062, end grp handle: 0x0068 uuid: f000aa60-0451-4000-b000-000000000000
attr handle: 0x0069, end grp handle: 0x0073 uuid: f000ccc0-0451-4000-b000-000000000000
attr handle: 0x0074, end grp handle: 0xffff uuid: f000ffc0-0451-4000-b000-000000000000

uuid 最下位の 16bit 値は GATT の仕様で定められている規定値で、

  • 1800=Generic Access
  • 1803=Link Loss
  • 1802=Immediate Alert
  • 1804=Tx Power
  • 180F=Battery Service

を示しており、このデバイスが持っているプライマリサービス一覧がわかります。GATT にはもう一つ「キャラクタリスティックス」という情報があり、これは SNMP における MIB walk に近い動作をします。

gatttoolコマンドでSensorTagに対してchar-descの要求

SensorTagに対して、gatttoolコマンドでchar-descを要求してみると次のようになります。

[B4:99:4C:64:CD:DF][LE]>  char-desc
handle: 0x0001, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0002, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0003, uuid: 00002a00-0000-1000-8000-00805f9b34fb
handle: 0x0004, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0005, uuid: 00002a01-0000-1000-8000-00805f9b34fb
handle: 0x0006, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0007, uuid: 00002a02-0000-1000-8000-00805f9b34fb
handle: 0x0008, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0009, uuid: 00002a03-0000-1000-8000-00805f9b34fb
handle: 0x000a, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x000b, uuid: 00002a04-0000-1000-8000-00805f9b34fb
handle: 0x000c, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x000d, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x000e, uuid: 00002a05-0000-1000-8000-00805f9b34fb
handle: 0x000f, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x0010, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0011, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0012, uuid: 00002a23-0000-1000-8000-00805f9b34fb
handle: 0x0013, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0014, uuid: 00002a24-0000-1000-8000-00805f9b34fb
handle: 0x0015, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0016, uuid: 00002a25-0000-1000-8000-00805f9b34fb
handle: 0x0017, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0018, uuid: 00002a26-0000-1000-8000-00805f9b34fb
handle: 0x0019, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x001a, uuid: 00002a27-0000-1000-8000-00805f9b34fb
handle: 0x001b, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x001c, uuid: 00002a28-0000-1000-8000-00805f9b34fb
handle: 0x001d, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x001e, uuid: 00002a29-0000-1000-8000-00805f9b34fb
handle: 0x001f, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0020, uuid: 00002a2a-0000-1000-8000-00805f9b34fb
handle: 0x0021, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0022, uuid: 00002a50-0000-1000-8000-00805f9b34fb
handle: 0x0023, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0024, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0025, uuid: f000aa01-0451-4000-b000-000000000000
handle: 0x0026, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x0027, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0028, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0029, uuid: f000aa02-0451-4000-b000-000000000000
handle: 0x002a, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x002b, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x002c, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x002d, uuid: f000aa11-0451-4000-b000-000000000000
handle: 0x002e, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x002f, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0030, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0031, uuid: f000aa12-0451-4000-b000-000000000000
handle: 0x0032, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0033, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0034, uuid: f000aa13-0451-4000-b000-000000000000
handle: 0x0035, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0036, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0037, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0038, uuid: f000aa21-0451-4000-b000-000000000000
handle: 0x0039, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x003a, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x003b, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x003c, uuid: f000aa22-0451-4000-b000-000000000000
handle: 0x003d, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x003e, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x003f, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0040, uuid: f000aa31-0451-4000-b000-000000000000
handle: 0x0041, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x0042, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0043, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0044, uuid: f000aa32-0451-4000-b000-000000000000
handle: 0x0045, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0046, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0047, uuid: f000aa33-0451-4000-b000-000000000000
handle: 0x0048, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0049, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x004a, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x004b, uuid: f000aa41-0451-4000-b000-000000000000
handle: 0x004c, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x004d, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x004e, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x004f, uuid: f000aa42-0451-4000-b000-000000000000
handle: 0x0050, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0051, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0052, uuid: f000aa43-0451-4000-b000-000000000000
handle: 0x0053, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x0054, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0055, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0056, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0057, uuid: f000aa51-0451-4000-b000-000000000000
handle: 0x0058, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x0059, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x005a, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x005b, uuid: f000aa52-0451-4000-b000-000000000000
handle: 0x005c, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x005d, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x005e, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x005f, uuid: 0000ffe1-0000-1000-8000-00805f9b34fb
handle: 0x0060, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x0061, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0062, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0063, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0064, uuid: f000aa61-0451-4000-b000-000000000000
handle: 0x0065, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0066, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0067, uuid: f000aa62-0451-4000-b000-000000000000
handle: 0x0068, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0069, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x006a, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x006b, uuid: f000ccc1-0451-4000-b000-000000000000
handle: 0x006c, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x006d, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x006e, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x006f, uuid: f000ccc2-0451-4000-b000-000000000000
handle: 0x0070, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0071, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0072, uuid: f000ccc3-0451-4000-b000-000000000000
handle: 0x0073, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0074, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0075, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0076, uuid: f000ffc1-0451-4000-b000-000000000000
handle: 0x0077, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x0078, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0079, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x007a, uuid: f000ffc2-0451-4000-b000-000000000000
handle: 0x007b, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x007c, uuid: 00002901-0000-1000-8000-00805f9b34fb

gatttoolコマンドでSensorTagに対してcharacteristicsの要求

SensorTagに対して、gatttoolコマンドでcharacteristicsを要求してみると次のようになります。

[B4:99:4C:64:CD:DF][LE]> characteristics
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: 0x0a, char value handle: 0x0007, uuid: 00002a02-0000-1000-8000-00805f9b34fb
handle: 0x0008, char properties: 0x08, char value handle: 0x0009, uuid: 00002a03-0000-1000-8000-00805f9b34fb
handle: 0x000a, char properties: 0x02, char value handle: 0x000b, uuid: 00002a04-0000-1000-8000-00805f9b34fb
handle: 0x000d, char properties: 0x20, char value handle: 0x000e, uuid: 00002a05-0000-1000-8000-00805f9b34fb
handle: 0x0011, char properties: 0x02, char value handle: 0x0012, uuid: 00002a23-0000-1000-8000-00805f9b34fb
handle: 0x0013, char properties: 0x02, char value handle: 0x0014, uuid: 00002a24-0000-1000-8000-00805f9b34fb
handle: 0x0015, char properties: 0x02, char value handle: 0x0016, uuid: 00002a25-0000-1000-8000-00805f9b34fb
handle: 0x0017, char properties: 0x02, char value handle: 0x0018, uuid: 00002a26-0000-1000-8000-00805f9b34fb
handle: 0x0019, char properties: 0x02, char value handle: 0x001a, uuid: 00002a27-0000-1000-8000-00805f9b34fb
handle: 0x001b, char properties: 0x02, char value handle: 0x001c, uuid: 00002a28-0000-1000-8000-00805f9b34fb
handle: 0x001d, char properties: 0x02, char value handle: 0x001e, uuid: 00002a29-0000-1000-8000-00805f9b34fb
handle: 0x001f, char properties: 0x02, char value handle: 0x0020, uuid: 00002a2a-0000-1000-8000-00805f9b34fb
handle: 0x0021, char properties: 0x02, char value handle: 0x0022, uuid: 00002a50-0000-1000-8000-00805f9b34fb
handle: 0x0024, char properties: 0x12, char value handle: 0x0025, uuid: f000aa01-0451-4000-b000-000000000000
handle: 0x0028, char properties: 0x0a, char value handle: 0x0029, uuid: f000aa02-0451-4000-b000-000000000000
handle: 0x002c, char properties: 0x12, char value handle: 0x002d, uuid: f000aa11-0451-4000-b000-000000000000
handle: 0x0030, char properties: 0x0a, char value handle: 0x0031, uuid: f000aa12-0451-4000-b000-000000000000
handle: 0x0033, char properties: 0x0a, char value handle: 0x0034, uuid: f000aa13-0451-4000-b000-000000000000
handle: 0x0037, char properties: 0x12, char value handle: 0x0038, uuid: f000aa21-0451-4000-b000-000000000000
handle: 0x003b, char properties: 0x0a, char value handle: 0x003c, uuid: f000aa22-0451-4000-b000-000000000000
handle: 0x003f, char properties: 0x12, char value handle: 0x0040, uuid: f000aa31-0451-4000-b000-000000000000
handle: 0x0043, char properties: 0x0a, char value handle: 0x0044, uuid: f000aa32-0451-4000-b000-000000000000
handle: 0x0046, char properties: 0x0a, char value handle: 0x0047, uuid: f000aa33-0451-4000-b000-000000000000
handle: 0x004a, char properties: 0x12, char value handle: 0x004b, uuid: f000aa41-0451-4000-b000-000000000000
handle: 0x004e, char properties: 0x0a, char value handle: 0x004f, uuid: f000aa42-0451-4000-b000-000000000000
handle: 0x0051, char properties: 0x02, char value handle: 0x0052, uuid: f000aa43-0451-4000-b000-000000000000
handle: 0x0056, char properties: 0x12, char value handle: 0x0057, uuid: f000aa51-0451-4000-b000-000000000000
handle: 0x005a, char properties: 0x0a, char value handle: 0x005b, uuid: f000aa52-0451-4000-b000-000000000000
handle: 0x005e, char properties: 0x10, char value handle: 0x005f, uuid: 0000ffe1-0000-1000-8000-00805f9b34fb
handle: 0x0063, char properties: 0x02, char value handle: 0x0064, uuid: f000aa61-0451-4000-b000-000000000000
handle: 0x0066, char properties: 0x0a, char value handle: 0x0067, uuid: f000aa62-0451-4000-b000-000000000000
handle: 0x006a, char properties: 0x12, char value handle: 0x006b, uuid: f000ccc1-0451-4000-b000-000000000000
handle: 0x006e, char properties: 0x08, char value handle: 0x006f, uuid: f000ccc2-0451-4000-b000-000000000000
handle: 0x0071, char properties: 0x08, char value handle: 0x0072, uuid: f000ccc3-0451-4000-b000-000000000000
handle: 0x0075, char properties: 0x1c, char value handle: 0x0076, uuid: f000ffc1-0451-4000-b000-000000000000
handle: 0x0079, char properties: 0x1c, char value handle: 0x007a, uuid: f000ffc2-0451-4000-b000-000000000000

char propertiesの仕様を次に示します。

char properties:
 00000001:
 00000010:read
 00000100:WRITE WITHOUT RESPONSE
 00001000:write
 00010000:notify
 00100000:indicate only
 01000000:
 10000000:

SensorTagのGATT の仕様

GATT の仕様のサービス宣言アトリビュート 「0x2800」の部分が黄色くマーキングされています。この部分がコマンド「gatttool --primary」で一覧が表示されます。すべてのアトリビュートがコマンド「gatttool --char-desc」で表示され、GATT の仕様の「Type」が表示されます。コマンド「gatttool --characteristics」 では、キャラクタリスティク宣言「0x2803」の内容が表示され、GATT の仕様のキャラクタリスティク宣言「0x2803」の部分が水色くマーキングされています。GATT の仕様の灰色の部分はキャラクタリスティクの値(Value)を示します。

プライマリサービス「0x1800=Generic Access」

プライマリサービス「0xAA10=Accelerometer Service」

gatttoolコマンドによるデバイス名の取得

ここで例えばハンドル 0x0003 を読み出すと、16進コードで表示されますが、アスキーコードに変換すると、これが文字列 "TI BLE Sensor Tag" であることがわかります。16bit UUID 値 2a00 は GATT の仕様において DEVICE NAME CHARACTERISTIC と定められているので、このデバイスのデバイス名が「TI BLE Sensor Tag」であることがわかる...という具合です。

$ sudo gatttool -b  B4:99:4C:64:CD:DF --interactive
[B4:99:4C:64:CD:DF][LE]> connect
Attempting to connect to B4:99:4C:64:CD:DF
Connection successful
[B4:99:4C:64:CD:DF][LE]> char-read-hnd 3
Characteristic value/descriptor: 54 49 20 42 4c 45 20 53 65 6e 73 6f 72 20 54 61 67