LoginSignup
138
153

More than 5 years have passed since last update.

Raspberry PiでiBeaconを検知する

Last updated at Posted at 2014-04-04

bluezインストール

Reaspberry Piでbluetoothを扱う場合はbluezを使うらしいです。
インストールは株式会社アイムービックさんのエンジニアブログに書いている通りにやるとインストールできました。有益な情報ありがとうございます。

Raspberry Pi で iBeacon を試してみよう!

現時点(2014/4/4)での最新のライブラリーは5.17でしたので、ダウンロードと展開の所だけ最新のに合わせて行いました。

wget https://www.kernel.org/pub/linux/bluetooth/bluez-5.17.tar.xz
tar xvJf bluez-5.17.tar.xz
cd bluez-5.17

ここまででRaspberry PiがiBeaconとして認識されました。

bluezでは色々コマンドが用意されている様で、hcitoolでもできる様です。

Raspberry PiをiBeacon化してみた

iBeaconを検知

ここからが本題ですが、この逆でCentralとしてデバイスを検知しようとした場合に情報が少なくて困り果てました。が、やっと見つかったので書き留めておきます。

iBeaconはBLEのAdvertisingという機能を使っていてるという事は分りましたが、それを受信するのはどうするの?となるとなかなか情報にたどりつけませんでした。

差し当たってhcitool lescanでデバイスのMACアドレスを取得できる事が分かりました。(unknownって何?)

$ sudo hcitool lescan
LE Scan ...
EE:E3:FB:60:13:E3 (unknown)
EE:E3:FB:60:13:E3 s62
00:1C:4D:40:B7:C3 (unknown)

gatttoolで何かできそうらしいのですが、肝心のgatttoolがなくてと思っていたら、bluezをビルドした時にattribディレクトリーにできていて、それをコピーして使う様でした。

$ sudo cp attrib/gatttool /usr/local/bin

gattoolは-bでMACアドレスを与えて-Iで対話できる様です。
使い方が良く分かってないのでhelpを

$ sudo gatttool -b 00:1C:4D:40:B7:C3 -I
[00:1C:4D:40:B7:C3][LE]> help
help                                           Show this help
exit                                           Exit interactive mode
quit                                           Exit interactive mode
connect         [address [address type]]       Connect to a remote device
disconnect                                     Disconnect from a remote device
primary         [UUID]                         Primary Service Discovery
included        [start hnd [end hnd]]          Find Included Services
characteristics [start hnd [end hnd [UUID]]]   Characteristics Discovery
char-desc       [start hnd] [end hnd]          Characteristics Descriptor Discovery
char-read-hnd   <handle>                       Characteristics Value/Descriptor Read by handle
char-read-uuid  <UUID> [start hnd] [end hnd]   Characteristics Value/Descriptor Read by UUID
char-write-req  <handle> <new value>           Characteristic Value Write (Write Request)
char-write-cmd  <handle> <new value>           Characteristic Value Write (No response)
sec-level       [low | medium | high]          Set security level. Default: low
mtu             <value>                        Exchange MTU for GATT/ATT

connectで接続して

[00:1C:4D:40:B7:C3][LE]> connect
Attempting to connect to 00:1C:4D:40:B7:C3
Connection successful

primaryでGATTとかATTとかの情報が得られる様です。
BluetoothLowEnergy(BLE)の勉強 - その2 - Linux+BlueZでBLE通信内容確認(1)

[00:1C:4D:40:B7:C3][LE]> primary
attr handle: 0x0001, end grp handle: 0x0005 uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle: 0x0006, end grp handle: 0x0006 uuid: 00001801-0000-1000-8000-00805f9b34fb
attr handle: 0x0007, end grp handle: 0x000f uuid: 0000180a-0000-1000-8000-00805f9b34fb
attr handle: 0x0010, end grp handle: 0xffff uuid: 00000001-24f6-4000-a000-000000000000

characteristicsをすると、何やら表示されて、char-read-hndやchar-write-cmdで何かしらできる様ですがAdvertisingの内容を得る方法は分りませんでした。

[00:1C:4D:40:B7:C3][LE]> characteristics
handle: 0x0002, char properties: 0x0a, char value handle: 0x0003, uuid: 00002a00-0000-1000-8000-00805f9b34fb
  .
  .

[00:1C:4D:40:B7:C3][LE]> char-read-hnd 0x0003
Characteristic value/descriptor: 20 

物によっては音をだしたりLEDを点灯させたりできるんですね。
BluetoothLowEnergy(BLE)の勉強 - その1 - Linux+BlueZでBLE通信を試す

やり方

探してみると上記の様な事柄ばかりで、やっとみつけたのがこちらです。

ほぼそのままです。
hcidump -Rを起動したままhcitool lescanをすると得られます。(UUIDは捏造しました)
(出力はhcidumpの方からでてました 4/5追記)

$ sudo hcidump -R &
[1] 2462
$ HCI sniffer - Bluetooth packet analyzer ver 5.9
device: hci0 snap_len: 1500 filter: 0xffffffff

$ sudo hcitool lescan
< 01 0B 20 07 01 10 00 10 00 00 00 
> 04 0E 04 01 0B 20 00 
< 01 0C 20 02 01 01 
> 04 0E 04 01 0C 20 00 
LE Scan ...
  .
  .
00:1C:4D:40:B7:C3 (unknown)
> 04 3E 2A 02 01 00 00 C3 B7 40 4D 1C 00 1E 02 01 06 1A FF 4C 
  00 02 15 E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 00
  00 00 00 C8 00 
  .
  .

フォーマット

iBeaconのAdvertisingパケットのフォーマットはこちらに書かれていて、4C 00からがiBeaconを表している様です。

What is the iBeacon Bluetooth Profile

以下引用

4C00 02 15 585CDE931B0142CC9A1325009BEDC65E 0000 0000 C5

<company identifier (2 bytes)> <type (1 byte)> <data length (1 byte)> <uuid (16 bytes)> <major (2 bytes)> <minor (2 bytes)> <RSSI @ 1m>

その他参照

Bluetooth Low Energy リンクレイヤの解説

138
153
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
138
153