Bluetooth Low Energy Handshake Sample Code
This topic walks you through sample code that encodes and decodes packets that an Echo device and a gadget exchange during a handshake over Bluetooth Low Energy (BLE). Here we use a C language implementation, but you can extend the concepts to other languages such as Python or Java.
- Step 1: Get the resources
- Step 2: Copy the utility files
- Step 3: Compile the .proto files
- Step 4: Compile the sample
- Step 5: Run the sample
- (Optional) Step 6: Configure the sample code parameters
Step 1: Get the resources
The first step is to get the following resources:
- Download the nanopb .proto compiler – To use protocol buffers with C, we recommend that you use nanopb. Nanopb is a small code-size protocol buffer implementation in ANSI C. It is especially suitable for use in microcontrollers, but fits any memory-restricted system. To install nanopb, do the following:
- Download the latest nanopb version at Nanopb Downloads.
- Extract the contents of the nanopb download. The extracted nanopb directory will contain an executable to compile .proto files (that is, get *.c and *.h files). It will also contain helper files that contain functions and data structures to encode and decode any protobuf-encoded data.
- Download the Alexa Gadgets sample code repository – Get the Alexa Gadgets Sample Code GitHub repository. This repository contains Alexa Gadget .proto files and sample code.
Step 2: Copy the utility files
Copy the following files from your nanopb directory into the /ConnectionHelpers/BLE/Handshake directory of the Alexa Gadgets repository that you downloaded in the previous step. These files are the utility files that define the methods and the data structures required to encode and decode the protobuf-encoded messages.
Step 3: Compile the .proto files
You now need to compile the Alexa Gadgets .proto files into classes that you can reference from your gadget code, as follows:
- Navigate to the /ConnectionHelpers/BLE/Handshake directory of the Alexa Gadgets sample code repository that you downloaded.
- Open compile_nanos.sh (on Mac or Linux) or compile_nanos.bat (on Windows) and make sure that your nanopb .proto compiler is located at the specified
- From the /ConnectionHelpers/BLE/Handshake directory, run compile_nanos.sh (on Mac or Linux) or compile_nanos.bat (on Windows). This compiles all of the .proto files in the Alexa Gadgets sample code repository and puts the resulting C and header source files into the /ConnectionHelpers/BLE/Handshake directory.
Step 4: Compile the sample
From the /ConnectionHelpers/BLE/Handshake directory, compile the code by using the following command:
gcc -I. -DPB_FIELD_16BIT *.c -o sample
Step 5: Run the sample
Execute the sample by typing
./sample (on Mac or Linux) or
sample.exe (on Windows). This encodes and decodes BLE handshake commands and responses to and from binary format, and prints out the results. The following table shows the functions that the sample code contains.
||Creates a sample advertisement packet.|
||Creates a sample protocol version packet.|
||Creates a DeviceInformation command; emulates a gadget receiving the command and generating a response; emulates an Echo device processing the response.|
||Creates a DeviceFeatures command; emulates a gadget receiving the command and generating a response; emulates an Echo device processing the response.|
||Creates an UpdateComponentSegment command; emulates a gadget receiving the command and generating a response; emulates an Echo device processing the response.|
||Creates an ApplyFirmware command; emulates a gadget receiving the command and generating a response; emulates an Echo device processing the response.|
||Decodes BLE packets that the gadget received from the Echo device.|
When you run the sample, you will get the following output:
(Optional) Step 6: Configure the sample code parameters
The sample code uses the configuration declared in config.h, which sets the maximum transaction size and the maximum transmission unit (MTU) size as follows:
#define SAMPLE_MAX_TRANSACTION_SIZE (5000U) #define SAMPLE_NEGOTIATED_MTU (128U)
You can modify these configurations and rebuild the sample as needed.
Was this page helpful?
Last updated: Feb 14, 2022