4.2.3 BLE ANCS Application

Getting Started

Getting Started with Peripheral Building Blocks

Introduction

This section helps users to enable BLE ANCS (Apple Notification Center Service) profile on the WBZ351 Curiosity Board using MCC. In this application example ANCS client is implemented.

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

These examples are incrementally structured upon one another. Recommendation is to follow the examples in order,by learning the basic concepts first and then progressing to the more advanced topics.

Hardware Required

Table 4-25. Hardware Required
Tool Qty
WBZ351 Curiosity Board1
Micro USB cable1
iPhone1

SDK Setup

Software

Smartphone App

None

Programming the Precompiled Hex File or Application Example

Programming the .hex File using MPLAB X IPE

  1. Precompiled .hex file is located in "<Harmony Content Path>\wireless_apps_pic32cxbz3_wbz35\apps\ble\advanced_applications\ble_ancs_app\hex" folder
  2. Follow the steps mentioned here

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. Open and program the application example "ble_ancs_app.X" located in "<Harmony Content Path>\wireless_apps_pic32cxbz3_wbz35\apps\ble\advanced_applications\ble_ancs_app\firmware" using MPLAB X IDE

For more details on finding the Harmony content path, refer to Installing the MCC Plugin

Demo Description

This application demonstrates on how to simulate an ANCS client on the curiosity board . The on-board user Button 2 (USR-BTN1/SW2), LEDs and UART are used as user interface during the demo.

  • UART Settings
    • Baud Rate/Speed – 115200 bps
    • Parity – None
    • Data Bits – 8
    • Stop Bits – 1
    • Flow Control – None
    • Character Encoding: UTF-8
  • Button 2 (USR-BTN1/SW2) Behavior
    • Press: Press the button for less than 500 ms
    • Long press: Press the button for more than 500 ms
    • Double-click: Click the button twice in 500 ms
  • LED Behavior
    • All LEDs are OFF
    • Green LED flashes one time every 3 seconds
    • Green LED flashes two times every 3 seconds
    • Green LED flashes two times every 1.5 seconds

Testing

  1. Connect the WBZ351 Curiosity Board to PC, program the precompiled .hex file or application example as mentioned.
  2. Open TeraTerm and configure as mentioned below:
    Terminal Settings
    • Baud Rate/Speed – 115200 bps
    • Parity – None
    • Data Bits – 8
    • Stop Bits – 1
    • Flow Control – None
  3. Reset the board. After reset, the device will be advertising device name "M-dev".
  4. On the iPhone, remove “M-dev” if it was previously paired
  5. Clear all notifications on the iPhone and it will display "No Older Nofitifcation"
  6. Connect USB cable to the curiosity board from the computer
  7. On the iPhone, open Settings>Bluetooth and click M-dev to connect to and pair with curiosity board.
  8. Open Calendar App on phone and set an event that will alert 2 minutes later.
  9. The notification of the event pops out when time is up
    Figure 4-187. Notification Event
  10. The below texts will be printed on Tera Term
    • BLE_ANCS_EVT_NTFY_ADDED_IND.
    • connHandle: 0x0071
    • NotificationUID: 0x00000000
    • EventFlags:
    • -silent: 0
    • -important: 0
    • -preExisting: 0
    • -positiveAction: 0
    • -negativeAction: 1
    • CategoryID: 0x05
    • CategoryCount: 0x01
    • -> Press Button 2 (USR-BTN1/SW2) to Get Notification Attributes:
  11. On the curiosity board, press Button 2 (USR-BTN1/SW2) as the printed texts instruct
  12. The below texts will be printed on Tera Term
    • BLE_ANCS_EVT_NTFY_ATTR_IND.
    • connHandle: 0x0071
    • NotificationUID: 0x00000000
    • bitmask:
    • appId: com.apple.mobilecal
    • title: Gggg
    • subtitle:
    • msg: Today at 17:04
    • msgSize: 14
    • date: 20230721T170400
    • positiveAction:
    • negativeAction: Clear
    • -> Press Button 2 (USR-BTN1/SW2) to Get App Attributes:
  13. On the curiosity board, press Button 2 (USR-BTN1/SW2) as the printed texts instruct
  14. The below texts will be printed on Tera Term
    • BLE_ANCS_EVT_APP_ATTR_IND.
    • connHandle: 0x0071
    • appId: com.apple.mobilecal
    • displayName: Calendar
    • -> Double-click USR Button 2: Clear :
  15. On the curiosity board, double-click Button 2 (USR-BTN1/SW2) as the printed texts instruct
  16. The below texts will be printed on Tera Term
    • BLE_ANCS_EVT_NTFY_REMOVED_IND.
    • connHandle: 0x0071
    • NotificationUID: 0x00000001
    • EventFlags:
    • -silent: 0
    • -important: 0
    • -preExisting: 0
    • -positiveAction: 0
    • -negativeAction: 1
    • CategoryID: 0x05
    • CategoryCount: 0x00
  17. Reset the board and the iphone will automatically link back

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_pic32cxbz3_wbz35\apps\ble\advanced_applications\ble_ancs_app\firmware\ble_ancs_app.X".

    For more details on importing the component configuration , refer to Importing Existing App Example 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 4-188. Project Graph

Verify MCC Component Settings

  1. Select Apple Notification Center Service Profile component in project graph
    1. Enable ANCS Client Role.

      Figure 4-189. ANCS Configuration
  2. Select BLE Stack component in project graph, and configure as mentioned below
    1. GAP(Generic Access Profile) Settings
      1. Set Device Name to M-dev
      2. Include <<Flag>> type of Advertising Data (AD)
      3. Include <<Local Name>> AD
      4. Include 128bit UUID of the ANCS in advertising Data so that iphone will list the M-dev in its Bluetooth page of Setting App. The ANCS UUID is 7905F431-B5CE-4E99-A40F-4B1E122D00D0 that will be encoded into <<Service UUID>>/0x15 type of AD

        0x11: the AD length indicating how many bytes are appendixed

        0x15: the value of <<Service UUID>> type

        D0002D121E4B0FA4994ECEB531F40579: the ANCS UUID in little endian

        1115D0002D121E4B0FA4994ECEB531F40579
      5. Enable "Peripheral"
        Figure 4-190. 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 4-191. 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 4-192. 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-193. app.c

Autogenerated, advertisement Data Format

Table 4-26. 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.cSource Code for the BLE stack related component configurations, code related to function calls from app.c
app_ble_handler.cAll GAP, GATT, SMP and L2CAP Event handlers
src\app_ble\app_ancs_handler.cHandle ANCS Client Event from Application level
src\config\default\ble\profile_ble\ble_ancs\ble_ancs.cHandle ANCS Client Event from Profile level
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

Configure the Advertisement

  • APP_BleConfigBasic( )

The below code snippet is to set ADs (Advertising Data) including <<Flag>> AD, <<Local Name>>> AD, and the <<Service UUID>> AD for ANCS UUID which will enable the device appearing in device list of Bluetooth page of iphones Setting APP.

static void APP_BleConfigBasic(void)
{
    int8_t                          connTxPower;
    int8_t                          advTxPower;
    BLE_GAP_AdvParams_T             advParam;
    uint8_t advData[]={
            //bit1: LE General Discoverable Mode, bit 2: BR/EDR Not Supported.
            0x02, 0x01, 0x06,
            //local name: "M-dev"
            0x06, 0x09, 0x4D, 0x2D, 0x64, 0x65, 0x76,
            //Length(0x11: 17), 0x15:<<List of 128-bit Service Solicitation UUIDs>>, 128-bit ANCS UUID
            0x11, 0x15, 0xD0, 0x00, 0x2D, 0x12, 0x1E, 0x4B, 0x0F, 0xA4, 0x99, 0x4E, 0xCE, 0xB5, 0x31, 0xF4, 0x05, 0x79};
Check the pairing status and apply different advertising filter policy. Windows, Android and iOS support the reconnection using ADV_IND. Thus ADV_IND is used for pairing and reconnection.
//Windows/ Android/ iOS support the reconnection using ADV_IND. So using ADV_IND for pairing and reconnection.
    advParam.type = BLE_GAP_ADV_TYPE_ADV_IND;        /* Advertising Type */
    advParam.advChannelMap = BLE_GAP_ADV_CHANNEL_ALL;        /* Advertising Channel Map */
    if (g_pairedDevInfo.bPaired)//Paired already
    {
        advParam.filterPolicy = BLE_GAP_ADV_FILTER_SCAN_CONNECT;     /* Advertising Filter Policy */
    }
    else
    {
        advParam.filterPolicy = BLE_GAP_ADV_FILTER_DEFAULT;     /* Advertising Filter Policy */
    }
    BLE_GAP_SetAdvParams(&advParam);
Start Advertisement
  • APP_EnableAdvDirect(g_pairedDevInfo.bPaired);

Custom function EnableAdvDirect() is called during the Applications initialization - APP_STATE_INIT in app.c.

Figure 4-194. app.c

This custom function will call API BLE_GAP_SetAdvEnable( ) to start or stop advertising. Users can exercise various other BLE Advertisement functionalities by using BLE Stack API

Initialize ANCS Client

ANCS Client functionality initialization is done in APP_BleStackInitAdvance( )

BLE_ANCS_Init( ) requests the Device Discovery (DD) middleware to explore the ANCS service of the connecting peer device.

APP_AncsEvtHandler( ) is registered to handle the ANCS event from application level that will not handle the ANCS profile.

    BLE_ANCS_Init();
    BLE_ANCS_EventRegister(APP_AncsEvtHandler);

Implement ANCS Profile

ANCS Profile is implemented in ble_ancs.c. BLE_ANCS_BleEventHandler( ) will handle all ANCS events.
void BLE_ANCS_BleEventHandler(STACK_Event_T *p_stackEvent)
{
    switch (p_stackEvent->groupId)
    {
        case STACK_GRP_BLE_GAP:
            ble_ancs_GapEventProcess((BLE_GAP_Event_T *)p_stackEvent->p_event);
        break;

        case STACK_GRP_GATT:
            ble_ancs_GattEventProcess((GATT_Event_T *)p_stackEvent->p_event);
        break;

        default:
        break;
    }
}

Firmware FSM (Finite State Machine)

This application firmware implements an ANCS client on the curiosity board.

  • APP_ANCS_STATE_IDLE: This application stays in the Idle mode.
  • APP_ANCS_STATE_ADV: The application is waiting for pairing for timeout_adv seconds.
  • APP_ANCS_STATE_ADV_DIR: The application is waiting for reconnection for timeout_adv_dir seconds.
  • APP_ANCS_STATE_CONN: The application is ready for sending commands/receiving notifications.
    Figure 4-195. Finite State Machine
    Table 4-27. Time Out Parameters
    VariableValueDescription
    timeout_adv60sThe timeout of advertising
    timeout_adv_dir30sThe timeout of directed advertising
    Table 4-28. LED vs. State
    Firmware StateLED behavior
    APP_ANCS_STATE_IDLEAll LEDs are OFF
    APP_ANCS_STATE_ADVBlue LED flashes one time every 3 seconds. (ON: 50 ms, OFF: 2950 ms)
    APP_ANCS_STATE_ADV_DIRBlue LED flashes two time every 3 seconds. (ON: 50 ms, OFF: 50 ms)
    APP_ANCS_STATE_CONNBlue LED flashes two times every 1.5 seconds. (ON: 50 ms, OFF: 150 ms)

FSM of the Connection State

State machine when connected:

  • APP_ANCS_IND_INIT: The application enters this state when connected.
  • APP_ANCS_IND_NTFY_ADDED: The application received the event "Notification Added" and is waiting for a button-pressing to send the command "Get Notification Attributes".
  • APP_ANCS_IND_NTFY_ATTR: The application received the data related to "Notification Attributes" and is waiting for a button-pressing to send the command "Get App Attributes".
  • APP_ANCS_IND_APP_ATTR: The application received the data related to "App Attributes" and is waiting for a button action to send the command "Perform Notification Action-Positive"/"Perform Notification Action-Negative".
  • APP_ANCS_IND_NTFY_REMOVED: The application received the event "Notification Removed" and is waiting for another event "Notification Added".
Figure 4-196. Connection State FSM

References