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

Table 4-10. Hardware Prerequisites
S. No. Tool Quantity
1PIC32WM-BW1 Curiosity Board1
2USB Type-C cable1

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:

  1. Import and program the precompiled hex file: <Harmony Content Path>\wireless_apps_pic32_bw1\apps\ble\peripheral\peripheral_Connect\hex
  2. 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:

  1. Perform the following the steps mentioned in Running a Precompiled Example. For more information, refer to Running a Precompiled Application Example from Related Links.
  2. Open and program the application: <Harmony Content Path>\wireless_apps_pic32_bw1\apps\ble\peripheral\peripheral_Connect\BLE_Connect.X

Testing

  1. Program PIC32WM-BW1 Curiosity Board with “BLE_Connect” application and open Tera Term configured with following settings through Terminal Settings:
    1. Baud rate/ Speed – 115200
    2. Parity – None
    3. Data bits – 8
    4. Stop bits – 1
    5. Flow Control – None
  2. Reset the board. Upon reset, “BW1-Advertising” will be displayed on Tera Term indicating that the device has started advertising.
  3. 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.
  4. 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

This section explains how MCC Project graph must look like and component configurations.
  1. Verify if the Project Graph window has all the expected configuration as illustrated in the following figure.
Figure 4-68. Project Graph

Verifying Advertisement and Connection Configuration

  1. 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

Files and Routines Automatically Generated by the MCC

After generating the program source from MCC interface by clicking Generate Code, the BLE configuration source and header files can be found in the following project directories.
Figure 4-70. Project File

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.

Figure 4-71. initialization.c

The 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.

Figure 4-72. app_ble.c
Autogenerated, Advertisement Data Format
Figure 4-73. AD Structures and Types
Table 4-11. Source Files
Source Files Usage
app.cApplication State machine, includes calls for Initialization of all BLE stack (GAP,GATT, SMP, L2CAP) related component configurations
app_ble\app_ble.cSource code for the BLE stack related component configurations, code related to function calls from app.c
app_ble\app_ble_handler.cGAP, GATT, SMP and L2CAP event handlers
Note: 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.h contains BLE GAP functions and is automatically included in app.c file.

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 State APP_STATE_INIT in app.c

User Application Development

  1. Include

    • Include the user action. For more information, refer to User Action from Related Links.
    • definitions.h must be included in all the files where UART will be used to print debug information.

    Note: 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.
  2. 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
  3. Starting Advertisement in app.c

    In APP_Tasks(), case APP_STATE_INIT:
    // Start Advertisement
    BLE_GAP_SetAdvEnable(0x01, 0x00);
    SERCOM0_USART_Write((uint8_t *)"BW1-Advertising\r\n",17);
    Figure 4-75. app.c
  4. Connected and Disconnected event in app_ble_handler.c

    • In APP_BleGapEvtHandler(), case BLE_GAP_EVT_CONNECTED
      /* TODO: implement your application code.*/
      SERCOM0_USART_Write((uint8_t *)"Connected\r\n",11);
    • In APP_BleGapEvtHandler(), case BLE_GAP_EVT_DISCONNECTED

      /* TODO: implement your application code.*/
      SERCOM0_USART_Write((uint8_t *)"Disconnected\r\n",14);
    • Figure 4-76. app_ble_handler.c