A useful experiment to learn how Bluetooth connects using nRF52840-DK kit
nRF52840-DK Example: BLE Interactive Command Line Interface
This example tutorial from Nordic is a sample program to help engineer learns about Bluetooth practise.
The following are some documentation about this example to practise.
What you need?
You will need at least two development board to play around with the advertise, scanning for Bluetooth devices, and connection.
The source code and *.hex provided by Nordic are for nRF52840-DK (PCA10056) and nRF52832-DK (PCA10040) boards. In this example, I am using two sets of nRF52840-DK board.
The example used is
nRF5 (SDK) -> Examples -> BLE Central and Peripheral -> Experimental -> BLE App Interactive -> PCA10056
The *.hex file can be found from this directory. Source codes are here as well.
Flash both boards with the same firmware.
Playing with the Bluetooth connectivity through console
Once the firmware is flash, the USB connection is also the virtual com port. Connect them to the PC and open the port using a serial console like MobaXterm.
Serial port settings: Baudrate is 115200bps, 8 bits data, 1 stop bit, no parity, no hardware control.
Once connected through com port, you can start to issue commands
advertise -Turn advertising on or off.
bonded_devices -List bonded_devices.
<address/peer_id>Establish a connection with a device.
connected_devices -Display connected devices and security information on each connection.
<name>Set device name.
devices -List available (advertising) devices.
<address>Disconnect from a device.
gatt -GATT Client procedures.
<key>Enter passkey displayed by another device (for pairing mode: “Passkey Entry”).
<subcmd>Turn on NFC reader (requires additional hardware) <on/off>.
numeric -Confirm or reject a numerical value (for pairing mode: “Numerical Comparison”).
<subcmd> <address> <option>Initiate pairing with a connected device.
<subcmd>Change or request new Link Layer or GATT parameters.
privacy -Set privacy settings.
<subcmd>Remove a bonded device.
scan -Turn scanning on or off.
LED indicators to help us see the state that the Bluetooth is in.
- LED 1: Means board is scanning for Bluetooth devices
- LED 2: Means board is connected to a BLE Peripheral device.
- LED 3: Means board is advertising itself. (boardcast)
- LED 4: Means board is connected to a BLE Central device.
You can set the name for the board using the command “device_name“. This name is volatile and not permanent. The name will be gone after a reset. Key in this command to set a name “Board 1” & “Board 2” to differential this two board.
eg. “device_name Board 1”
Key in the command “scan on“, you will notice that LED 1 will be lighted up. After this key in the command “devices“, you will get to see a list of nearby Bluetooth devices advertising themselves (broadcasting their ID). The command “devices” will only display the list when the board has its scanning activated.
You will not be able to see the other Bluetooth board that you have because that board has not yet started its advertising.
For the other board, key in the command “advertise on“. You will notice the LED 3 on this board lighted up.
On the first board which is still on the scanning mode, key in the command “devices”. You will get to see a list of Bluetooth devices. This time round you should be able to see the other board, its Bluetooth address ID and its device name.
On the first board, key in the command “connect” follow by the board ID of the other board that I want to connect. When connecting, both board will display messages about the connection. This first board will have LED 2 lighted up, indicating that it is connected to a peripheral board, while the other board has its LED 4 lighted up indicating that it is connected to a central board.
You can check the connected device using the command “connected_devices“
In the process of connection, I tried to make another connection from the Board 1 (central) to Board 2 (peripheral). It is known that a central device can make multiple connection to various peripheral devices. In this experiment I notice that it is possible for the Bluetooth peripheral to receive multiple connection. Each connection is reference by a handler ID.
Pairing is a temporary exchange of security features. Security key are exchanged. Bonding is a permanent exchange of security features. It just means the the security key that is exchanged are saved and be used for any future connection. Bonding can only be executed after pairing is done. It is simply saving the security keys on both side.
After the Connection
After the connection you can proceed to use the other commands “pair“, “parameters” and “gatt“.
Technical Name Recap
The GAP (Generic Access Profile) defines the discovery, connection and link management part of Bluetooth. Central (usually connected to multiple peripheral) and peripheral (usually a single connection to the central) are both related to GAP.
The GATT (Generic Attribute Profile) is one of the data communication method after the Bluetooth devices are connected. The Bluetooth device is the server serving the data, while the one accessing it is the client.