Bluetooth Low Energy (BLE) or Bluetooth Smart is a wireless personal area network technology that is not backwards compatible with Bluetooth Classic while using the same radio frequencies. Compared to Bluetooth Classic, it provides reduced power consumption while maintaining a similar communication range (typically between 10m and 100m). It was incorporated in the Bluetooth Core Specification Version 4.0.
Bluetooth Low Energy has two core protocols:
-
Generic Access Profile (GAP): Controls connections and advertisements between devices. GAP defines two core device roles:
-
Peripheral: Small, low-power, and resource-constrained devices that connect to a central device. They are usually sensors. A peripheral can only connect to one central device.
-
Central: This is the usual role for the ConnectCore 93 device, processing the connection from peripheral devices.
-
-
Generic Attribute Profile (GATT): Used to transfer data between BLE devices. GATT defines services and characteristics, and defines two roles:
-
Server or slave, which holds the service and characteristic definitions and is typically the peripherals above.
-
Client or master, typically the central device above which sends request to the server.
-
GATT transactions are started by the master device by establishing a connection and suggesting a connection interval to the slave. The slave will then attempt to reconnect at the connection interval to recover data.
BLE defines a set of profiles for low energy devices that are based over the services and characteristics of the Generic Attribute Profile (GATT) protocol. A profile is a pre-defined collection of services, which in turn contains a set of characteristics. Each service identifies itself with a UUID, 16 bits for official BLE services or 128 bits for custom services, and each characteristic is identified by a pre-defined 16 bits or 128 bits UUID.
User space usage
|
By default, ConnectCore Cloud services demo seizes the Bluetooth interface. To use Bluetooth for your own application stop the demo with the following commands:
|
Digi Embedded Yocto includes bluetoothctl, gattool, and other standard command line applications that can be used to test the Bluetooth functionality.
Scan and discover devices
See #bluetoothctl,bluetoothctl command line utility to scan for Bluetooth devices, including BLE devices.
Connect
Use the gatttool command to connect to a device, by passing its MAC address.
If the connection succeeds, the prompt changes to the MAC of the remote device.
You can then connect:
# gatttool -i hci0 -b 00:18:31:85:28:3E -I
[00:18:31:85:28:3E][LE]> connect
Attempting to connect to 00:18:31:85:28:3E
Connection successful
Discover services
You can request to discover the services offered by the remote device.
[CON][00:18:31:85:28:3E][LE]> primary
attr handle: 0x0001, end grp handle: 0x000b uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle: 0x000c, end grp handle: 0x000f uuid: 00001801-0000-1000-8000-00805f9b34fb
attr handle: 0x0010, end grp handle: 0x0022 uuid: 0000180a-0000-1000-8000-00805f9b34fb
attr handle: 0x0023, end grp handle: 0x0025 uuid: 00001803-0000-1000-8000-00805f9b34fb
attr handle: 0x0026, end grp handle: 0x0028 uuid: 00001802-0000-1000-8000-00805f9b34fb
attr handle: 0x0029, end grp handle: 0x002c uuid: 00001804-0000-1000-8000-00805f9b34fb
attr handle: 0x002d, end grp handle: 0x0031 uuid: 0000180f-0000-1000-8000-00805f9b34fb
attr handle: 0x0032, end grp handle: 0x0044 uuid: 0000ffa0-0000-1000-8000-00805f9b34fb
attr handle: 0x0045, end grp handle: 0xffff uuid: 0000ffe0-0000-1000-8000-00805f9b34fb
Access characteristics
You can then read/write the characteristics with char-read-hnd and char-write-req, for instance:
[CON][00:18:31:85:28:3E][LE]> char-read-hnd 34
Characteristic value/descriptor: 00
[CON][00:18:31:85:28:3E][LE]> char-write-req 34 01
[CON][00:18:31:85:28:3E][LE]> Characteristic value was written successfully
Act as a BLE server
You can set up your device to act as a BLE server.
For that, you can use a custom application to provide your own services and characteristics or use the sample application btgatt-server to simulate a heart rate device.
Heart rate service
Start the BLE advertisement:
# btmgmt -i hci0 le on
hci0 Set Low Energy complete, settings: powered bondable ssp br/edr le secure-conn
# btmgmt -i hci0 connectable on
hci0 Set Connectable complete, settings: powered connectable bondable ssp br/edr le secure-conn
# btmgmt -i hci0 discov on
hci0 Set Discoverable complete, settings: powered connectable discoverable bondable ssp br/edr le secure-conn
# btmgmt -i hci0 name "BLE-server"
# btmgmt -i hci0 advertising on
hci0 Set Advertising complete, settings: powered connectable discoverable bondable ssp br/edr le advertising secure-conn
| The name may be truncated due to length limitations of BLE payload. |
Start the GATT server with your desired functionality (-r, --heart-rate: Enable heart rate service):
# btgatt-server -i hci0 -r -v
Started listening on ATT channel. Waiting for connections
At this point you can connect to the BLE server using either gatttool or btgatt-client:
# btgatt-client -d 00:40:9D:8B:8E:20 -i hci0 -v
btgatt-client: Opening L2CAP LE connection on ATT channel:
src: 00:40:9D:98:99:BD
dest: 00:40:9D:8B:8E:20
Connecting to device... Done
On the server you see the connection:
Connect from 00:40:9D:98:99:BD
Running GATT server
att: > 10 01 00 ff ff 00 28 ......(
att: ATT PDU received: 0x10
[...]
[GATT server]#
On the client, run services to check the offered services:
[GATT client]# services
service - start: 0x0001, end: 0x0006, type: primary, uuid: 00001800-0000-1000-8000-0
0805f9b34fb
[...]
Sample application
An example application called ble-gatt-server-example is included in the dey-examples package of the meta-digi layer.
This application demonstrates how to enable a BLE GATT server on the ConnectCore 93 platform with custom services and characteristics using the Bluetooth SIG standards.
The application creates a service called "device information" that contains the following characteristics:
-
Current time (read/write characteristic)
-
Read: returns the current system time.
-
Write: prints the received value to the console.
-
-
Temperature measurement (read/notify characteristic)
-
Read: returns the current CPU temperature
-
Notify: Upon subscription, sends a notification when the temperature exceeds a defined threshold passed as a command line argument
-
Each characteristic has two descriptors, the "user description" with a custom description for the characteristic and the "presentation format" that contains the format information (format, exponent, unit, namespace and description) of the characteristic value.
Start the BLE advertisement:
# btmgmt -i hci0 le on
hci0 Set Low Energy complete, settings: powered connectable bondable ssp br/edr le advertising secure-conn
# btmgmt -i hci0 connectable on
hci0 Set Connectable complete, settings: powered connectable bondable ssp br/edr le advertising secure-conn
# btmgmt -i hci0 pairable on
hci0 Set Bondable complete, settings: powered connectable bondable ssp br/edr le advertising secure-conn
# btmgmt -i hci0 discov on
hci0 Set Discoverable complete, settings: powered connectable discoverable bondable ssp br/edr le advertising secure-conn
# btmgmt -i hci0 advertising on
hci0 Set Advertising complete, settings: powered connectable discoverable bondable ssp br/edr le advertising secure-conn
After you initialize the BLE interface, you can start the sample application.
See the syntax of the GATT server sample application (use -h for showing the help):
# /usr/bin/ble-gatt-server-example -h
ble-gatt-server-example
Usage:
ble-gatt-server-example [options]
Options:
-i, --index <id> Specify adapter index, e.g. hci0
-t, --threshold <temp> The temperature threshold to send notification
-m, --mtu <mtu> The ATT MTU to use
-v, --verbose Enable extra logging
-h, --help Display help
Run the GATT server with a temperature threshold of 50C:
# /usr/bin/ble-gatt-server-example --index hci0 --threshold 50
Running GATT server
Started listening on ATT channel. Waiting for connections
You can now connect to the GATT server from a client (for example using any standard app on a mobile phone), to read and interact with it.
Go to GitHub to see the application instructions and source code.