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 6UL 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
Digi Embedded Yocto includes the
gattool, and other standard command line applications that can be used to test the Bluetooth functionality as follows:
Scan and discover devices
You can use the
bluetoothctl tool to scan for devices as above.
You can also scan only for BLE devices using
hcitool as follows:
# hcitool -i hci0 lescan --duplicates LE Scan ... 00:18:31:85:28:3E (unknown) 00:18:31:85:28:3E Keyfobdemo
Once the remote device is identified, you can connect to it with the
gatttool command line application.
You will see the prompt change to the MAC of the remote device.
# gatttool -i hci0 -b <Remote MAC> -I [00:18:31:85:28:3E][LE]> connect
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
You can then read/write the characteristics. In the current example we are using an accelerometer device which can be enabled as follows:
[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 name "my_ble_server" # btmgmt -i hci0 advertising on hci0 Set Advertising complete, settings: powered connectable bondable ssp br/edr le advertising secure-conn
Start the GATT server with your desired functionality (
--heart-rate: Enable heart rate service):
# btgatt-server -i hci0 -r -v Started listening on ATT channel. Waiting for connections < next output will appear after a client connection > Connect from 00:40:9D:98:99:BD Running GATT server [GATT server]# att: > 10 01 00 ff ff 00 28 ......( [GATT server]# att: ATT PDU received: 0x10
At this point you can connect to the BLE server using either
# hcitool lescan LE Scan ... 00:40:9D:8B:8E:20 my_ble_server # 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 ..... [GATT client]# services service - start: 0x0001, end: 0x0006, type: primary, uuid: 00001800-0000-1000-8000-0 0805f9b34fb ........
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 6UL 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 advertising on hci0 Set Advertising complete, settings: powered connectable 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-app -h ble-gatt-server-app Usage: ble-gatt-server-app [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.
Known issues and limitations
The maximum number of supported simultaneous Bluetooth Low Energy connections is 15.