3.1.1.4 BLE Connection
Getting Started with Central Building Blocks
Introduction
- Advertiser (Transmitting Connectable Adv)
- Scanner
Users can choose to just run the precompiled Application Example hex file on the WBZ451 Curiosity Board and experience the demo or can go through the steps involved in developing this Application from scratch
It is recommend to follow the examples in order, by learning the basic concepts first and then progressing to the more advanced topics.
Recommended Reading
Hardware Requirement
Tool | Qty |
---|---|
WBZ451 Curiosity Board | 2 |
Micro USB cable | 2 |
SDK Setup
Software Requirement
Smartphone App
None
Programming the Precompiled Hex File or Application Example
Programming the hex file using MPLABX IPE
-
Precompiled Hex file is located in
"<Harmony Content Path>\wireless_apps_pic32cxbz2_wbz45\apps\ble\building_blocks\central\central_conn\hex"
folder -
For more details on the steps, go to Programming A Device
Programming the Application using MPLABX IDE
-
Follow steps mentioned in of Running a Precompiled Example document
-
Open and program the Application Example "central_conn.x" located in
"<Harmony Content Path>\wireless_apps_pic32cxbz2_wbz45\apps\ble\building_blocks\central\central_conn\firmware"
using MPLABX IDE
For more details on how to find the Harmony Content Path, refer to Installing the MCC Plugin
Demo Description
To experience this demo users are required to use central_connection and peripheral_connection application examples. The 2 Application Example mentioned above will demonstrate a BLE Connection Establishment Process. An advertiser (Peripheral) will start connectable, scannable, undirected BLE advertisement and the scanner (Central) will receive the advertisements and initiate a BLE connection. enables users to transmit Connectable and Scannable Undirected BLE Advertisements. Demo will print start of the scanning "Scanning", connected "Connected!" and disconnected "Disconnected" "Disconnected" state on a terminal emulator like TeraTerm@ (Speed: 115200, Data: 8-bit, Parity: none, stop bits: 1 bit, Flow control: none)
Testing
Users should use another WBZ451 Curiosity Board configured as peripheral connection
This section assumes that a user has already programmed the peripheral_conn and central_conn application on 2 WBZ451 Curiosity Boards.
Board1: WBZ451 Curiosity Board with peripheral_conn application Programmed
Board2: WBZ451 Curiosity Board with central_conn applicaton Programmed
Board1: Open TeraTerm @ (Speed: 115200, Data: 8-bit, Parity: none, stop bits: 1 bit, Flow control: none). Reset the board. Upon reset, "Advertising" message is displayed on the TeraTerm.
Board2: Open TeraTerm @ (Speed: 115200, Data: 8-bit, Parity: none, stop bits: 1 bit, Flow control: none). Reset the board. Upon reset, "Scanning" message is displayed on the TeraTerm. Upon finding peripheral device with public address {0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6} message "Found Peer Node" will be displayed and a connection request will be initiated "Initiating connection".
After connection establishment, both the peripheral device (Board1) and central device(Board2) will display "Connected!" message on respective terminal windows
Developing the Application from Scratch using MPLAB Code Configurator
-
Create a new MCC Harmony Project. For more details, refer to 2.5 Creating a New MCC Harmony Project.
-
Import component configuration -- This step helps users setup the basic components and configuration required to develop this application.The imported file is of format .mc3 and is located in the path
"<Harmony Content Path>\wireless_apps_pic32cxbz2_wbz45\apps\ble\building_blocks\central\central_conn\firmware\central_conn.X".
For more details on how to import the component configuration , refer toNote: Import and Export functionality of Harmony component configuration will help users to start from a known working setup of MCC configuration -
Accept Dependencies or satisfiers, select Yes
-
Verify if the Project Graph window has all the expected configuration
Verifying Scan and Connection Configuration
-
Select BLE_Stack component in project graph
Generating a Code
For more details on code generation, refer to 14.2 MPLAB Code Configurator(MCC) Code Generation
Files and Routines Automatically generated by the MCC
Source Files | Usage |
---|---|
app.c | Application State machine, includes calls for Initialization of all BLE stack (GAP,GATT, SMP, L2CAP) related component configurations |
app_ble\app_ble.c | Source Code for the BLE stack related component configurations, code related to function calls from app.c |
app_ble\app_ble_handler.c | All GAP, GATT, SMP and L2CAP Event handlers |
Note: app.c is autogenerated and has a state machine based Application code sample. Users can use this template to develop their application. |
Header Files
-
ble_gap.h
: This header file contains BLE GAP functions and is automatically included in the app.c file
Function Calls
MPLAB Code Configurator generates and adds the code to initialize the BLE Stack GAP, GATT, L2CAP and SMP in APP_BleStackInit() function
-
APP_BleStackInit() is the API that will be called inside the Applications Initial State -- APP_STATE_INIT in app.c\
User Application Development
Include
-
user action is required as mentioned here
-
definitions.h in all the files where UART will be used to print debug information
Tip: definitions.h is not specific to just UART peripheral, instead it should be included in all application source files where peripheral functionality will be exercised
Start Scanning
BLE_GAP_SetScanningEnable(true, BLE_GAP_SCAN_FD_ENABLE, BLE_GAP_SCAN_MODE_OBSERVER, 1000);
Scan Results and initiating a BLE connection
-
BLE_GAP_EVT_ADV_REPORT event is generated upon finding Adverstisements on legacy channels
-
BLE connection can be initiated by using the API BLE_GAP_CreateConnection(&createConnParam_t);
// code snippet to filter scan results and initiate connection
if (p_event->eventField.evtAdvReport.addr.addr[0] == 0xA1 && p_event->eventField.evtAdvReport.addr.addr[1] == 0xA2)
{
SERCOM0_USART_Write((uint8_t *)"Found Peer Node\r\n", 17);
BLE_GAP_CreateConnParams_T createConnParam_t;
createConnParam_t.scanInterval = 0x3C; // 37.5 ms
createConnParam_t.scanWindow = 0x1E; // 18.75 ms
createConnParam_t.filterPolicy = BLE_GAP_SCAN_FP_ACCEPT_ALL;
createConnParam_t.peerAddr.addrType = p_event->eventField.evtAdvReport.addr.addrType;
memcpy(createConnParam_t.peerAddr.addr, p_event->eventField.evtAdvReport.addr.addr, GAP_MAX_BD_ADDRESS_LEN);
createConnParam_t.connParams.intervalMin = 0x10; // 20ms
createConnParam_t.connParams.intervalMax = 0x10; // 20ms
createConnParam_t.connParams.latency = 0;
createConnParam_t.connParams.supervisionTimeout = 0x48; // 720ms
SERCOM0_USART_Write((uint8_t *)"Initiating Connection\r\n", 23);
BLE_GAP_CreateConnection(&createConnParam_t);
}
Connected and Disconnected Events
-
All the possible GAP, GATT, SMP and L2CAP Event handlers are available in file app_ble_handler.c, users can implement application code to denote Connection State here. BLE_GAP_EVT_CONNECTED event is generated after a successful BLE Connection with peripheral device
Scan Timeout Event
-
In app_ble_handler.c BLE_GAP_EVT_SCAN_TIMEOUT event is generated when BLE Scan duration expires.
Users can exercise various other BLE Advertisement functionalities by using BLE Stack API
For more details, refer to BLE Transparent UART.