ガジェットひとつで、 いつでもどこでも 英語を勉 …...iPhoneやAndroidスマホは?バッテリーに意すれば OK! • 勉強でバッテリーを使い果たして、肝なときに電話できな
BlueZで遊んでみる - BLE大阪勉強会
-
Upload
shinji-kobayashi -
Category
Technology
-
view
3.411 -
download
0
description
Transcript of BlueZで遊んでみる - BLE大阪勉強会
BlueZ で遊んでみる
@kobashinG
今日のお題
BlueZとは?
コマンドラインでCentral
Advertiseを投げてみる
BlueZとは?OSSのBluetoothスタック
Linux/Android等で利用されているBluetoothを制御する為のもの
kernel-module/utilities/daemonから成る
BlueZとは?大雑把に図にすると、、
Kernel Driver
Utilities bluetoothd
GUI Applicationd-busd/D-bus経由
HCIソケット経由
BlueZ(厳密にはDriverも)
この辺を使ってBLEを紐解いてみる。
BlueZとは?hciconfig - ifconfigに近い。 デバイスを設定する為のコマンド。
hcitool - connectionを制御する為のコマンド。 scan/cmd/leccなど。
gatttool - tいっぱい。 GATTをコントロールする為のコマンド
Utilities 概要
用意したものUbuntu環境
BeagleBoneBlack + ubuntu13.04 BLE対応のUSBドングル
Peripheral対応端末
StickNFind / ぶるタグ
iPhone / Android(検証用)
BLExplr / 自作アプリ。
BlueZをビルドUbuntu標準のBlueZはBLE非対応。
最新のBlueZをインストール$ wget https://www.kernel.org/pub/linux/bluetooth/bluez-5.16.tar.gz $ tar zxvf bluez-5.16.tar.gz $ sudo apt-get install libglib2.0-dev libdbus-1-dev libudev-dev libical-dev libreadline-dev $ cd bluez-5.16 $ ./configure —disable-systemd # make && make install
近くのデバイスを 検索してみる
$ hciconfig -a hci0: Type: BR/EDR Bus: USB BD Address: 00:1B:DC:06:2C:AD ACL MTU: 310:10 SCO MTU: 64:8 UP RUNNING PSCAN … 省略
$ sudo hcitool lescan LE Scan ... 44:13:19:02:E1:D9 LBT-VRU01 … 省略
◀ DOWNだったら hciconfig up する
◀ なんか見えた!!
接続してみる$ sudo hcitool lecc 44:13:19:02:E1:D9 Connection handle 71 $ sudo gatttool -i hci0 -b 44:13:19:02:E1:D9 -t random -I [44:13:19:02:E1:D9][LE]> connect Attempting to connect to 44:13:19:02:E1:D9 Connection successful [44:13:19:02:E1:D9][LE]> primary attr handle: 0x0001, end grp handle: 0x0007 uuid: 00001800-…. attr handle: 0x0008, end grp handle: 0x000a uuid: 00001803-…. attr handle: 0x000b, end grp handle: 0x000d uuid: 00001802-…. attr handle: 0x000e, end grp handle: 0x0010 uuid: 00001804-…. attr handle: 0x0011, end grp handle: 0xffff uuid: 0000180f-….
◀ なにこれ??
BLEの仕様を覗くBLEのService(機能)はそれぞれUUIDが決まってる。
Developer Portalを見てみる。https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
Serviceの中を覗いてみる
◀ Device Nameが取れるらしい。
Serviceの中を覗いてみる
◀ 0x2A00がDevice Name。
Serviceの中を覗いてみる
Generic AccessBattery Service
Immediate AlertLink LossTx Power
Proximty Profile
Device Name
AppearancePeripheral Privacy Flag
Reconnection Address
Peripheral Preferred Connection Parameters
Services Characteristics
Device Nameを見てみる。[44:13:19:02:E1:D9][LE]> primary attr handle: 0x0001, end grp handle: 0x0007 uuid: 00001800-…. …省略
[44:13:19:02:E1:D9][LE]> char-desc 0x0001 0x0007 handle: 0x0001, uuid: 2800 handle: 0x0002, uuid: 2803 handle: 0x0003, uuid: 2a00 handle: 0x0004, uuid: 2803 handle: 0x0005, uuid: 2a01 handle: 0x0006, uuid: 2803 handle: 0x0007, uuid: 2a04 [44:13:19:02:E1:D9][LE]> char-read-hnd 0x0003 Characteristic value/descriptor: 4c 42 54 2d 56 52 55 30 31
◀ これがDevice Name.
ASCII表記「LBT-VRU01」
ぶるタグを鳴らしてみる
Generic AccessBattery Service
Immediate AlertLink LossTx Power
Proximty Profile
Alert Level
Services
Characteristics
UUID : 0x1802
UUID : 0x2A06
ぶるタグを鳴らしてみる
◀ 0x02入れたら鳴りそう!
ぶるタグを鳴らしてみる
[44:13:19:02:E1:D9][LE]> primary attr handle: 0x000b, end grp handle: 0x000d uuid: 00001802-…. [44:13:19:02:E1:D9][LE]> char-desc 0x000b 0x000d handle: 0x000b, uuid: 2800 handle: 0x000c, uuid: 2803 handle: 0x000d, uuid: 2a06 [44:13:19:02:E1:D9][LE]> char-write-cmd 0x000d 02
◀ これがAlert Level.
Advertizeを投げてみる受け手を作る。(Android)
android.bluetooth.BluetoothAdapter.LeScanCallback
◀ ここにAdvertizeのデータが入ってるぽい.
Advertiseを投げてみる拾ったデータをダンプする。
@Override public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { // send from NOT-UI THREAD // ! StringBuffer sb = new StringBuffer(); for (byte b : scanRecord) { sb.append("0x"); sb.append(Integer.toHexString(b & 0xff)); sb.append(" "); } Log.i(TAG,sb.toString()); }
Advertizeを投げてみる$ sudo hciconfig hci0 leadv 3
0x02 0x01 0x0a 0x02 0x0a 0x08 0x0c 0x09 0x43 0x53 0x52 0x38 0x35 0x31 0x30 0x20 0x41 0x31 0x30 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00 0x00 0x00
◀ [CSR8510 A10] Androidがデバイス名として認識してるのはこの辺りぽいけど?
AdvertizeパケットPreamble
1 octet 4 octet
Access Address Protocol Data Unit
2 ~ 39 octet
CRC
3 octet
▼先のAPIが取得できてたのはココ。
Header
2 octet
Payload
6 ~ 37 octet
AdvA Adv Data
Length AD Type Data
6 octet 0 ~ 31 octet
1 octet 1 octet N octet
解析してみる
Advertizeを投げてみる
0x02 0x01 0x0a
0x02 0x0a 0x08
0x0c 0x09 0x43 0x53 0x52 0x38 0x35 0x31 0x30 0x20 0x41 0x31 0x30
Length AD Type Data
Advertizeを投げてみるAD Type?
Dataに何が入っているか定義
0x02 0x01 0x0a
0x02 0x0a 0x08
0x0c 0x09 0x43 0x53 0x52 0x38 0x35 0x31 0x30 0x20 0x41 0x31 0x30
Length AD Type Data
▼0x09 Local Name(Completed). デバイス名!
Advertizeを投げてみる好きなデータを投げてみたい。以下で設定できるらしい?
$ hcitool -i hci0 cmd 0x08 0x0008 48 45 4c 4c 4f 57 $ sudo hciconfig hci0 leadv 3
ASCII表記「HELLOBLE」
えっ!?全然変わんない。。。
Advertizeを投げてみる
仕方無いので、Cプログラムからbluezを叩いてみる。(参考)bluez-ibeacon https://github.com/carsonmcdonald/bluez-ibeacon
Advertizeを投げてみるhci_send_req()を使うとdeviceに要求を出せる。
要求はstruct hci_requestにまとめる。
PARAM設定(Adv間隔),ENABLE設定,DATA設定の順に書き込めば、受信データが変わったことを確認できた。
0x0A 0x09 0x48 0x45 0x4a 0x4a 0x4f 0x57 0x42 0x4a 0x45 0x00 0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00 …省略◀ [HELLOBLE]が取れた!
!
!
Thank you !!