Last edited 2 months ago

How to scan BLE devices

Template:ArticleMainWriter Template:ArticleApprovedVersion


This page lists the different operations needed to scan, connect and display BLE device information. BLE stands for Bluetooth Low Energy

1. Packages needed[edit | edit source]

  • bluez5

2. Configuration[edit | edit source]

Init file modification for automatic Bluetooth start
/etc/udev/rules.d/10-local.rules:

# Set bluetooth power up
ACTION=="add", KERNEL=="hci0", RUN+="/usr/bin/hciconfig hci0 up"

3. Existing tool selection[edit | edit source]

Bluez provides some tools, by default, to analyze Bluetooth networks.
hciconfig to configure hci connections
hcitool to scan, find a device, connect to a device, manage a device list.. deviceS may be normal or low energy
gatttool for BLE device management

3.1. BLE device connection step-by-step[edit | edit source]

Command to scan all low-energy Bluetooth hardware:

Template:Board$ hciconfig hci0 up
Template:Board$ hcitool lescan

To scan available BLE devices:

Template:Board$ hcitool lewladd <BLE_MAC_ADDRRESS>

To add BLE device in the white list (optional):

Template:Board$ hcitool lecc <BLE_MAC_ADDRESS>

To connect a BLE device:

Once a BLE device is identified, its characteristics (attibutes) can be discovered, read and modified using GATTTOOL.
GATTTOOL offers two working modes: interractive and non-interractive

3.1.1. Gatttool Interractive mode[edit | edit source]

Template:Board$ gatttool -b <MAC Address> --interactive

In interractive mode, a new prompt is available to perform BLE commands.
connect: to connect to a specified device
primary: to disable all primary attributes
char-read-hnd <handle> to read specified handle/attribute values
char-write-cmd <handle> <value> to modify handle values

3.1.2. Gatttool Non-interractive mode:[edit | edit source]

In non-interractive mode, commands are issued one by one. At each command, GATTTOOL performs device connection, action and disconnection.
Few a few examples are given below:

Template:Board$ gatttool -b <Mac Address> --primary 
Template:Board$ gatttool -b <MAC Address> --characteristics
Template:Board$ gatttool -b <MAC Address> --char-read
Template:Board$ gatttool -b <MAC Address> --char-desc