5.2.2.5 BLE Connection
This section explains how to enable advertisements and connection on the PIC32-BZ6 Curiosity board using the MPLAB Code Configurator (MCC). The peripheral device will be the PIC32-BZ6 Curiosity board and the central device can either be a smartphone with a Light Blue app or another PIC32-BZ6 Curiosity board. BLE Advertising is broadcasting small packets to peer devices. In BLE, a peripheral device always starts with advertisements. Advertisement packets enable a central or observer to discover and connect to a peripheral.
Users can choose to either run the precompiled Application Example hex file provided on the PIC32-BZ6 Curiosity Board or follow the steps to develop the application from scratch.
It is recommended to follow the examples in sequence to understand the basic concepts before progressing to the advanced topics.
Recommended Readings
-
Getting Started with Application Building Blocks – See Building Block Examples from Related Links.
-
Getting Started with Peripheral Building Blocks – See Peripheral Devices from Related Links.
-
FreeRTOS and BLE Stack Setup – See Peripheral - FreeRTOS BLE Stack and App Initialize from Related Links.
- See BLE Legacy Advertisements from Related Links.
- See BLE Connection from Related Links.
-
BLE Software Specification – See MPLAB® Harmony Wireless BLE in Reference Documentation from Related Links.
Hardware Requirement
| S. No. | Tool | Quantity |
|---|---|---|
| 1 | PIC32-BZ6 Curiosity Board | 1 |
| 2 | Micro USB cable | 1 |
| 3 | Android/iOS Smartphone | 1 |
SDK Setup
Refer to Getting Started with Software Development from Related Links.
Software Requirement
To install Tera Term tool, refer to the Tera Term web page in Reference Documentation from Related Links.
Smartphone App
Light Blue iOS/Android app available in stores
Programming the Precompiled Hex File or Application Example
Using MPLAB® X IPE:
- Import and program the
precompiled hex file:
<Discover Path>\wireless_apps_pic32_bz6.\apps\ble\building_blocks\peripheral\peripheral_conn\precompiled_hex\peripheral_conn.X.production.signed.hex - For detailed steps, refer to
Programming a Device in MPLAB® IPE in
Reference Documentation from Related Links.Note: Ensure to choose the correct Device and Tool information.
Using MPLAB® X IDE:
- Perform the following the steps mentioned in Running a Precompiled Example. For more information, refer to Running a Precompiled Application Example from Related Links.
- Open and program the application:
<Discover Path>\wireless_apps_pic32_bz6.\apps\ble\building_blocks\peripheral\peripheral_conn\firmware\peripheral_conn.X - For more details on how to find the Discover path, refer to Download Application Example from Discover in Running a Precompiled Application Example from Related Links.
Demo Description
This application enables users to transmit Connectable and Scannable Undirected BLE Advertisements. On reset, demo will print “Advertising” on a terminal emulator like Tera Term which denotes the start of advertisements. The central device scanning these advertisements can then issue a connection request unto the Curiosity board. Upon connection, “Connected” is printed on the terminal window.
Testing
- Using a micro USB cable, connect the Debug USB on the Curiosity board to a PC.
- Program the precompiled hex file or application example as mentioned.
- Open Tera Term:
- Set the “Serial Port” to USB Serial Device.
- Speed to 115200.
- Press the NMCLR button on the
Curiosity board. “Advertising” must be displayed in Tera Term.
- Launch the Light Blue mobile app
to scan for Advertisements. The device name “pic32cx-bz6” will appear then press
Connect. After a successful connection, you can view the advertisement data and
see “Connected” displayed in Tera Term. Users with an iOS device may see the
device name as “Microchip”.
Figure 5-80. Note: Users can use another PIC32-BZ6 Curiosity board configured as BLE Connection (central) instead of using a mobile app. For more information, refer to BLE Connection from Related Links.
Developing the Application from Scratch using MCC
-
Create a new harmony project. For more details, see Creating a New MCC Harmony Project from Related Links.
- To setup the basic components and
configuration required to develop this application, import component
configuration:
<Discover Path>\wireless_apps_pic32_bz6\apps\ble\building_blocks\peripheral\peripheral_conn\firmware\peripheral_conn.X\peripheral_conn.Note: 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 when prompted.
- Verify if the project graph window has all the expected configuration.
Verifying Advertisement and Connection Configuration
- Select BLE Stack component in project
graph, to open component configuration and configure as illustrated in the
following figure.
Figure 5-82. BLE Stack Configuration
Generating Code
For more details on code generation, refer to MPLAB Code Configurator (MCC) Code Generation from Related Links.
Files and Routines Automatically Generated by the MCC
Initialization routines for OSAL, RF System, and BLE System are auto-generated by the MCC. See OSAL Libraries Help in Reference Documentation from Related Links. Initialization routine executed during program initialization can be found in the project file.
initialization.cThe BLE stack initialization routine executed during Application Initialization can be found in project files. This initialization routine is automatically generated by the MCC. This call initializes and configures the GAP, GATT, SMP, L2CAP and BLE middleware layers.
app_ble.c| 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 | GAP, GATT, SMP and L2CAP event handlers |
app.c is autogenerated and has a state machine based application
code. Users can use this template to develop their own application.Header Files
ble_gap.hcontains BLE GAP functions and is automatically included inapp.cfile.
Function Calls
- MCC generates and adds the code
to initialize the BLE Stack GAP, GATT, SMP and L2CAP in
APP_BleStackInit()
APP_BleStackInit()is the API that will be called inside the Applications Initial StateAPP_STATE_INITinapp.c
User Application Development
Include
- Include the user action. For more information, refer to User Action from Related Links.
-
definitions.hmust be included in all the files where UART will be used to print debug information.
definitions.h is not specific to UART
but instead it must be included in all application source files where any peripheral
functionality will be exercised.Set Public Device Address in
app_ble.c
BLE_GAP_SetDeviceAddr(&devAddr);
BLE_GAP_Addr_T devAddr;
devAddr.addrType = BLE_GAP_ADDR_TYPE_PUBLIC;
devAddr.addr[0] = 0xA1;
devAddr.addr[1] = 0xA2;
devAddr.addr[2] = 0xA3;
devAddr.addr[3] = 0xA4;
devAddr.addr[4] = 0xA5;
devAddr.addr[5] = 0xA6;
// Configure device address
BLE_GAP_SetDeviceAddr(&devAddr);app_ble.cStarting Advertisement in
app.c.
BLE_GAP_SetAdvEnable(0x01, 0x00);app.cConnected and Disconnected Events
- All the possible GAP, GATT, SMP
and L2CAP Event handlers are available in file
app_ble_handler.c. Users can implement their own application code to denote Connection State here.Figure 5-89. app_ble_handler.c
Where to go from Here
See BLE Transparent UART from Related Links.
