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 6 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 bluetoothctl, 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

Connect

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

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. 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 need to start the BLE advertisement and then start the gatt server with your desired functionality (-r, --heart-rate: Enable Heart Rate service). 

~# 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 se
cure-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 ad
vertising secure-conn
~# 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 gatttool or well btgatt-client:

~# 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
........