4.1.2.3 Bluetooth® LE Connection
This section explains how to enable advertisements and connection on the PIC32WM-BW1 Curiosity board using the MCC. The peripheral device will be the PIC32WM-BW1 Curiosity Board and the central device can either be a smart phone with a Light Blue app or another PIC32WM-BW1 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 PIC32WM-BW1 Curiosity Board or follow the steps to develop the application from scratch.
Hardware Requirement
| S. No. | Tool | Quantity |
|---|---|---|
| 1 | PIC32WM-BW1 Curiosity Board | 1 |
| 2 | USB Type-C™ cable | 1 |
Software Requirement
- To install Tera Term tool, refer to the Tera Term web page listed in the Resference Documentation from Related Links.
Smart Phone 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:
<Harmony Content Path>\wireless_apps_pic32_bw1\apps\ble\peripheral\peripheral_Connect\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:
<Harmony Content Path>\wireless_apps_pic32_bw1\apps\ble\peripheral\peripheral_Connect\BLE_Connect.X
Testing
- Program PIC32WM-BW1 Curiosity Board with “
BLE_Connect” application and open Tera Term configured with following settings through Terminal Settings:- Baud rate/ Speed – 115200
- Parity – None
- Data bits – 8
- Stop bits – 1
- Flow Control – None
- Reset the board. Upon reset, “BW1-Advertising” will be displayed on Tera Term indicating that the device has started advertising.
- Launch the Light Blue mobile app that will act as central device scanning for advertisement. The device name “pic32wm-bw1” will appear in the list of devices in the app and then press Connect. The mobile app issues a connection request to the Curiosity Board.
- After a successful connection,
users can view the advertisement data in the app and see “Connected” on Tera Term.
Figure 4-66. Terminal Window Figure 4-67. LightBlue Application Note: Users can use another PIC32WM-BW1 Curiosity board configured as BLE Connection (central) instead of using a mobile app.
Project Graph
- Verify if the Project Graph window has all the expected configuration as illustrated in the following figure.
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 4-69. 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
The OSAL, RF System and BLE System initialization routine executed during program initialization can be found in the project file. This initialization routine is automatically generated by the MCC.
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.
Note:definitions.his 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,APP_BleConfigBasic()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);Figure 4-74. app_ble.c -
Starting Advertisement in
app.cInAPP_Tasks(), caseAPP_STATE_INIT:// Start Advertisement BLE_GAP_SetAdvEnable(0x01, 0x00); SERCOM0_USART_Write((uint8_t *)"BW1-Advertising\r\n",17);
Figure 4-75. app.c -
Connected and Disconnected event in
app_ble_handler.c-
In
APP_BleGapEvtHandler(), caseBLE_GAP_EVT_CONNECTED/* TODO: implement your application code.*/ SERCOM0_USART_Write((uint8_t *)"Connected\r\n",11);
-
In
APP_BleGapEvtHandler(), caseBLE_GAP_EVT_DISCONNECTED/* TODO: implement your application code.*/ SERCOM0_USART_Write((uint8_t *)"Disconnected\r\n",14);
-
Figure 4-76. app_ble_handler.c
-
