Upload
lars-alexander-blumberg
View
1.323
Download
9
Embed Size (px)
Citation preview
What you will learn• What is a Bluetooth Low Energy GATT profile?
• How do I make my Raspberry Pi talk to BLE devices?
• Discover and connect to BLE devices
• Read/write values from/to BLE devices
• Talk to BLE devices with Python
Generic ATTribute profile
= the API of your BLE device
BLE GATT
Characteristic 1
Characteristic 2
Characteristic 3
Service 1
Service 2
Service n
NOTIFY ME
READ
WRITE
Generic ATTribute profile
= the API of your BLE device
BLE GATT
Service 1
Heart Rate
Service n
Sensor Location
Heart Rate
Control Point
120,
2 (hand wrist)
NOTIFY ME
READ
118,122 …
WRITEOK
180D
2A38
2A37
2A39
Discovery
In “advertising” mode
In “discovery” (=scan) mode
Advertisement package periodically sent (20ms-10s) size: 22-47 bytes
Interpreted by your phone OS
read
write
Connection, R/W
In “connected” mode (cannot be discovered
during connection)
TerminologyServerPeripheralSlave
TerminologyClientCentralMaster
Requests a connection
Installing BlueZ
BlueZ is the official Linux Bluetooth protocol stack
sudo apt-get install --no-install-recommends bluetooth
(All code examples tested on Raspbian Feb. 2016)
Discovering
$ sudo hcitool lescan LE Scan ... C3:12:3A:53:27:09 (unknown) C3:12:3A:53:27:09 EST EF:84:0D:82:32:51 Nuimo EF:84:0D:82:32:51 (unknown) 54:60:09:E8:A5:5B (unknown) 54:60:09:E8:A5:5B ChromecastAudio3967 C7:59:CD:40:8D:CD BLEGong ...
MAC address of discovered BLE device
Connecting
$ sudo gatttool -b D1:3A:19:C5:79:6D -t random -I[D1:3A:19:C5:79:6D][LE]> connect Attempting to connect to D1:3A:19:C5:79:6DConnection successful [D1:3A:19:C5:79:6D][LE]> characteristics value handle: 0x0003, uuid: 00002a00-0000-1000-...value handle: 0x0005, uuid: 00002a01-0000-1000-...value handle: 0x0007, uuid: 00002a04-0000-1000-...value handle: 0x000b, uuid: 00002a19-0000-1000-......
Battery UUID (RTFM!)
Read
[D1:3A:19:C5:79:6D][LE]> characteristics value handle: 0x0003, uuid: 00002a00-0000-1000-...value handle: 0x0005, uuid: 00002a01-0000-1000-...value handle: 0x0007, uuid: 00002a04-0000-1000-...value handle: 0x000b, uuid: 00002a19-0000-1000-...... Battery handle[D1:3A:19:C5:79:6D][LE]> char-read-hnd 0b Characteristic value/descriptor: 55
Write
[D1:3A:19:C5:79:6D][LE]> characteristics value handle: 0x0003, uuid: 00002a00-0000-1000-...value handle: 0x0005, uuid: 00002a01-0000-1000-...value handle: 0x0007, uuid: 00002a04-0000-1000-...value handle: 0x000b, uuid: 00002a19-0000-1000-...... Battery handle[D1:3A:19:C5:79:6D][LE]> char-write-req 0b 64 Error: Characteristic Write Request failed:Attribute can't be written
gatttool cannot load the battery :(
Notify
[D1:3A:19:C5:79:6D][LE]> characteristics value handle: 0x0003, uuid: 00002a00-0000-1000-...value handle: 0x0005, uuid: 00002a01-0000-1000-...value handle: 0x0007, uuid: 00002a04-0000-1000-...value handle: 0x000b, uuid: 00002a19-0000-1000-...... Battery handle[D1:3A:19:C5:79:6D][LE]> char-write-req 0c 0100 Characteristic value was written successfully
Handle (0b) + 1
Notification handle = 0x000b value: 4d
Value changes automatically notified
Enable notifications
Python librariesbluepy: github.com/IanHarvey/bluepy (synchr.)
pygattlib: bitbucket.org/OscarAcena/pygattlib (asynchr.)
Summary
• BLE GATT is the API of your BLE devices
• It consists of services and characteristics with unique IDs
• Basic operations: Read, Write, Notifications
• bluepy and pygattlib: good libraries to add BLE communication to Python scripts