3.1.1.4 BLE Connection

Getting Started

Getting Started with Central Building Blocks

Introduction

This section helps users to enable scan and connect functionality on the BLE Central Device (WBZ451). To establish a successful BLE connection, the Advertiser is required to broadcast advertisement packets across the three primary advertisement channels (or a subset of these channels). This allows the devices scanning for advertisers to find and read their advertisement data, the scanner can initiate a connection if advertiser allows it. To demonstrate a BLE connection on the WBZ451 two device types are required.
  1. Advertiser (Transmitting Connectable Adv)
  2. Scanner

Users can choose to run the precompiled application example .hex file on the WBZ451 Curiosity Board and experience the demo or go through the steps involved in developing this application from scratch.

Recommendation is to follow the examples in order, by learning the basic concepts first and then progressing to the more advanced topics.

Hardware Requirement

Table 3-5. Hardware Requirement
Tool Qty
WBZ451 Curiosity Board2
Micro USB cable2

SDK Setup

Software Requirement

Smartphone App

None

Programming the Precompiled Hex File or Application Example

Programming the .hex File using MPLAB X IPE

  1. Central Device – Precompiled .hex file is located in "<Harmony Content Path>\wireless_apps_pic32cxbz2_wbz45\apps\ble\building_blocks\central\central_conn\hex" folder
  2. Peripheral Device – Precompiled .hex file is located in "<Harmony Content Path>\wireless_apps_pic32cxbz2_wbz45\apps\ble\building_blocks\Peripheral\Peripheral_conn\hex" folder
  3. For more details on the steps, go to Programming a Device
    Note: Users must choose the correct device and tool information
Programming the Application using MPLAB X IDE
  1. Follow steps mentioned in the Running a Precompiled Example section

  2. Central Device – 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 MPLAB X IDE
  3. Peripheral Device – Open and program the application example “peripheral_conn.X” located in "<Harmony Content Path>\wireless_apps_pic32cxbz2_wbz45\apps\ble\building_blocks\peripheral\peripheral_conn\firmware" using MPLAB X IDE

For more details on finding 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 two 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 application prints the status messages "Scanning" at the initiation of the scan process, "Connected!" upon successful connection, and "Disconnected" when the connection is lost, within a terminal emulator interface.

Tera Term Configuration
  • Baud Rate/Speed – 115200 (as configured in SERCOM configuration)
  • Parity – None
  • Data Bits – 8
  • Stop Bits – 1
  • Flow Control – None

Testing

Users must 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 two WBZ451 Curiosity Boards.

  1. Board 1 – WBZ451 Curiosity Board Programmed with “peripheral_conn”
    1. Open TeraTerm and configure as mentioned below:
      Terminal Settings
      • Baud Rate/Speed – 115200 (as configured in SERCOM configuration)
      • Parity – None
      • Data Bits – 8
      • Stop Bits – 1
      • Flow Control – None
    2. Reset the board, Upon reset, "Advertising" message is displayed on the TeraTerm.
  2. Board 2 – WBZ451 Curiosity Board Programmed with “central_conn”
    1. Open TeraTerm and configure as mentioned below:
      Terminal Settings
      • Baud Rate/Speed – 115200 (as configured in SERCOM configuration)
      • Parity – None
      • Data Bits – 8
      • Stop Bits – 1
      • Flow Control – None
    1. Reset the board. Upon reset, "Scanning" message is displayed on the TeraTerm.
    2. 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".
Note: Scanner is configured to scan only for 100 seconds, user must ensure the peer device is advertising.

After connection establishment, both the peripheral device (Board1) and central device (Board2) will display "Connected!" message on respective terminal windows

Figure 3-29. Tera Term Logs

Developing the Application from Scratch using MCC

This section explains the steps required by a user to develop this application example from scratch using MCC
Note: It is recommended that new users of MCC to go through the overview.
  1. Create a new MCC Harmony Project. For more details, refer to Creating a New MCC Harmony Project.
  2. 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 importing the component configuration , refer to Importing Existing App Example Configuration
    Note: Import and export functionality of Harmony component configuration will help users to start from a known working setup of MCC configuration
  3. Accept Dependencies or satisfiers, select Yes
  4. Verify if the project graph window has all the expected configuration. as illustrated in the following figure:
    Figure 3-30. Project Graph

Verifying Scan and Connection Configuration

  1. Select BLE Stack component in project graph, to open component configuration and configure as illustrated in the following figure.

    Figure 3-31. 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 can be found in the following project directories
Figure 3-32. Project File
The OSAL, RF System, BLE System initialization routine executed during program initialization can be found in the project files. This initialization routine is automatically generated by the MCC
Figure 3-33. initialization.c
The BLE stack initialization routine excuted during Application Initialization can be found in project files. This intitialization routine is automatically generated by the MCC. This call initializes and configures the GAP, GATT, SMP, L2CAP and BLE middleware layers
Figure 3-34. app_ble.c
Table 3-6. 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.cAll 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

MCC 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

Note: definitions.h is not specific to just UART peripheral, instead it must be included in all application source files where peripheral functionality will be exercised

Start Scanning

// Scanning Enabled
BLE_GAP_SetScanningEnable(true, BLE_GAP_SCAN_FD_ENABLE, BLE_GAP_SCAN_MODE_OBSERVER, 1000);
// Output the state string to UART
SERCOM0_USART_Write((uint8_t *)"Scanning \r\n", 11);
This API is called in the application’s initial state – APP_STATE_INIT in app.c. Scan duration is 100 secs
Figure 3-35. app.c

Scan Results and initiating a BLE connection

  • BLE_GAP_EVT_ADV_REPORT event in app_ble_handler.c is generated upon finding advertisements 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);
  }
Figure 3-36. app_ble_handler.c

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
    // Connected EVT
    SERCOM0_USART_Write((uint8_t *)"Connected!\r\n", 12);
    //Disconnected EVT
    SERCOM0_USART_Write((uint8_t *)"Disconnected\r\n", 15);
    
    Figure 3-37. app_ble_handler.c

Scan Timeout Event

  • In app_ble_handler.c BLE_GAP_EVT_SCAN_TIMEOUT event is generated when BLE Scan duration expires.
    SERCOM0_USART_Write((uint8_t *)"Scan Completed\r\n", 17);
    Figure 3-38. app_ble_handler.c

Users can exercise various other BLE advertisement functionalities by using BLE Stack API.

Where to go from Here