Linux BLE: Difference between revisions

From Variscite Wiki
(Initial version)
 
m (Nate moved page IMX BLE to Linux BLE)
 
(29 intermediate revisions by 6 users not shown)
Line 1: Line 1:
= Scanning for BLE devices =
{{PageHeader|Bluetooth Low Energy}}
{{DocImage|category1=Yocto|category2=Debian}}[[Category:VAR-SOM-MX6]][[Category: DART-6UL]][[Category: VAR-SOM-MX7]][[Category:DART-MX8M]][[Category:DART-MX8M-MINI]][[Category:DART-MX8M-PLUS]] __toc__
<br>{{note| Please make sure your Bluetooth device is powered on and ready for use. Follow the [[IMX Bluetooth#Device_identification|Bluetooth wiki]] page if required.
|info}}


Use btmgmt tool to scan for BLE devices and report their MAC address type (random vs public). btmgmt has many other options, use "btmgmt --help" to find out.
= Simulating BLE devices =
The BLE peripheral can be simulated installing and running the following Android App:<br>
https://play.google.com/store/apps/details?id=io.github.webbluetoothcg.bletestperipheral&hl=en_US&gl=US<br>
The BLE Peripheral Simulator is an Android app that allows developers to try out new features<br>
of Web Bluetooth without the need for a BLE Peripheral Device.<br>
You can check the code at: https://github.com/WebBluetoothCG/ble-test-peripheral-android<br>
[[File:BLE_Peripheral_Simulator.jpeg|200px]]
 
= Finding BLE devices =
 
Use '''btmgmt''' tool to find BLE devices and detect their MAC address type (random vs public).


<pre>
<pre>
root@var-som-mx6:~# btmgmt find
# btmgmt find -l
Discovery started
Discovery started
hci0 type 7 discovering on
hci0 type 7 discovering on
'''hci0 dev_found: 74:B9:AB:CF:13:A9 type LE Random rssi -90 flags 0x0000'''
hci0 dev_found: 74:B9:AB:CF:13:A9 type LE Random rssi -90 flags 0x0000
AD flags 0x1a  
AD flags 0x1a  
name Galaxy S5
name Galaxy S5
hci0 dev_found: 00:1A:7D:DA:71:0B type BR/EDR rssi -79 flags 0x0000
name SMTBT
hci0 dev_found: 00:1A:7D:DA:71:11 type BR/EDR rssi -96 flags 0x0001
eir_len 5
confirm_name succeeded for 00:1A:7D:DA:71:11
hci0 type 7 discovering off
hci0 type 7 discovering off
</pre>
</pre>


In this example a single BLE device was detected, having MAC address 74:B9:AB:CF:13:A9 and random MAC address type


= Connecting to BLE devices =
= Connecting to BLE devices =
Use '''gatttool''' to connect to BLE devices. Pass "-t random" parameter if scan has reported a random MAC address.
<pre>
# gatttool -t random -b 74:B9:AB:CF:13:A9 -I
[74:B9:AB:CF:13:A9][LE]> connect
Attempting to connect to 74:B9:AB:CF:13:A9
[74:B9:AB:CF:13:A9][LE]>
Connection successful
Indication handle = 0x0003 value: 01 00 ff ff
[74:B9:AB:CF:13:A9][LE]>
</pre>
= Accessing BLE devices =


Use gatttool to connect to BLE devices. Pass "-t random" parameter if scan reported random MAC address.
After successful connection BLE device characteristics can be accessed via '''gatttool'''. Continuing the example above:
<pre>
[74:B9:AB:CF:13:A9][LE]> characteristics
handle: 0x0002, char properties: 0x20, char value handle: 0x0003, uuid: 00002a05-0000-1000-8000-00805f9b34fb
handle: 0x0015, char properties: 0x02, char value handle: 0x0016, uuid: 00002a00-0000-1000-8000-00805f9b34fb
handle: 0x0017, char properties: 0x02, char value handle: 0x0018, uuid: 00002a01-0000-1000-8000-00805f9b34fb
handle: 0x0019, char properties: 0x02, char value handle: 0x001a, uuid: 00002aa6-0000-1000-8000-00805f9b34fb
handle: 0x0029, char properties: 0x12, char value handle: 0x002a, uuid: 00002a19-0000-1000-8000-00805f9b34fb
[74:B9:AB:CF:13:A9][LE]> char-read-hnd 0x0002
Characteristic value/descriptor: 20 03 00 05 2a
[74:B9:AB:CF:13:A9][LE]> char-read-hnd 0x0015
Characteristic value/descriptor: 02 16 00 00 2a
[74:B9:AB:CF:13:A9][LE]> char-read-hnd 0x0017
Characteristic value/descriptor: 02 18 00 01 2a
[74:B9:AB:CF:13:A9][LE]> char-read-hnd 0x0019
Characteristic value/descriptor: 02 1a 00 a6 2a
[74:B9:AB:CF:13:A9][LE]> char-read-hnd 0x0029
Characteristic value/descriptor: 12 2a 00 19 2a
</pre>


$ gatttool -b <MAC> [-t random] -I
= Receiving notifications from BLE devices =


Notifications sent from connected BLE devices can be seen in '''gatttool'''. The example below shows heart rate notification sent by Android "BLE Peripheral Simulator"<br>


For example when connecting to "BLE Peripheral Simulator" on Android phone the following is done:</br>
[[File:BLE_Peripheral_HRS.jpeg|200px]]
<pre>


root@var-som-mx6:~# gatttool -t random -b 74:B9:AB:CF:13:A9 -I
<pre>
[74:B9:AB:CF:13:A9][LE]> connect
Attempting to connect to 74:B9:AB:CF:13:A9
[74:B9:AB:CF:13:A9][LE]>
[74:B9:AB:CF:13:A9][LE]>
Connection successful
Notification handle = 0x002a value: 08 3c 00 00
Indication  handle = 0x0003 value: 01 00 ff ff
[74:B9:AB:CF:13:A9][LE]>
[74:B9:AB:CF:13:A9][LE]>
</pre>
</pre>

Latest revision as of 16:41, 15 March 2023

Bluetooth Low Energy


Please make sure your Bluetooth device is powered on and ready for use. Follow the Bluetooth wiki page if required.

Simulating BLE devices

The BLE peripheral can be simulated installing and running the following Android App:
https://play.google.com/store/apps/details?id=io.github.webbluetoothcg.bletestperipheral&hl=en_US&gl=US
The BLE Peripheral Simulator is an Android app that allows developers to try out new features
of Web Bluetooth without the need for a BLE Peripheral Device.
You can check the code at: https://github.com/WebBluetoothCG/ble-test-peripheral-android
BLE Peripheral Simulator.jpeg

Finding BLE devices

Use btmgmt tool to find BLE devices and detect their MAC address type (random vs public).

# btmgmt find -l
Discovery started
hci0 type 7 discovering on
hci0 dev_found: 74:B9:AB:CF:13:A9 type LE Random rssi -90 flags 0x0000
AD flags 0x1a 
name Galaxy S5
hci0 type 7 discovering off

In this example a single BLE device was detected, having MAC address 74:B9:AB:CF:13:A9 and random MAC address type

Connecting to BLE devices

Use gatttool to connect to BLE devices. Pass "-t random" parameter if scan has reported a random MAC address.

# gatttool -t random -b 74:B9:AB:CF:13:A9 -I
[74:B9:AB:CF:13:A9][LE]> connect
Attempting to connect to 74:B9:AB:CF:13:A9
[74:B9:AB:CF:13:A9][LE]>
Connection successful
Indication handle = 0x0003 value: 01 00 ff ff
[74:B9:AB:CF:13:A9][LE]>

Accessing BLE devices

After successful connection BLE device characteristics can be accessed via gatttool. Continuing the example above:

[74:B9:AB:CF:13:A9][LE]> characteristics 
handle: 0x0002, char properties: 0x20, char value handle: 0x0003, uuid: 00002a05-0000-1000-8000-00805f9b34fb
handle: 0x0015, char properties: 0x02, char value handle: 0x0016, uuid: 00002a00-0000-1000-8000-00805f9b34fb
handle: 0x0017, char properties: 0x02, char value handle: 0x0018, uuid: 00002a01-0000-1000-8000-00805f9b34fb
handle: 0x0019, char properties: 0x02, char value handle: 0x001a, uuid: 00002aa6-0000-1000-8000-00805f9b34fb
handle: 0x0029, char properties: 0x12, char value handle: 0x002a, uuid: 00002a19-0000-1000-8000-00805f9b34fb
[74:B9:AB:CF:13:A9][LE]> char-read-hnd 0x0002
Characteristic value/descriptor: 20 03 00 05 2a 
[74:B9:AB:CF:13:A9][LE]> char-read-hnd 0x0015
Characteristic value/descriptor: 02 16 00 00 2a 
[74:B9:AB:CF:13:A9][LE]> char-read-hnd 0x0017
Characteristic value/descriptor: 02 18 00 01 2a 
[74:B9:AB:CF:13:A9][LE]> char-read-hnd 0x0019
Characteristic value/descriptor: 02 1a 00 a6 2a 
[74:B9:AB:CF:13:A9][LE]> char-read-hnd 0x0029
Characteristic value/descriptor: 12 2a 00 19 2a 

Receiving notifications from BLE devices

Notifications sent from connected BLE devices can be seen in gatttool. The example below shows heart rate notification sent by Android "BLE Peripheral Simulator"

BLE Peripheral HRS.jpeg

[74:B9:AB:CF:13:A9][LE]>
Notification handle = 0x002a value: 08 3c 00 00
[74:B9:AB:CF:13:A9][LE]>