4.2.3 BLE ANCS Application

Getting Started

Getting Started with Peripheral Building Blocks

Introduction

This document will help users to enable BLE ANCS(The Apple Notification Center Service) profile on WBZ351 Curiosity board using MPLAB Code Configurator(MCC). In this application example ANCS client is implemented.

Users of this document can choose to just run the precompiled Application Example hex file on the WBZ351 Curiosity Board and experience the demo or can go through the steps involved in developing this Application from scratch

These examples each build on top on one and other. We strongly recommend that you follow the examples in order, to learn the basics concepts before progressing to the more advanced topics.

Hardware Required

Tool Qty
WBZ351 Curiosity Board 1
Micro USB cable 1
iPhone 1

SDK Setup

Getting Started with Software Development

Software

TeraTerm

Smartphone App

N/A

Programming the precompiled hex file or Application Example

Programming the hex file using MPLABX 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

Caution: Users should choose the correct Device and Tool information

Programming the Application using MPLABX IDE

  1. Follow steps mentioned in of Running a Precompiled Example document

  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 MPLABX IDE

<Harmony Content Path> how to find what is my Harmony Content Path

Demo Description

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

  • UART Settings
    • Baud Rate: 115200 bps
    • Data Bits: 8-bits
    • Parity: None
    • Stop Bits: 1 bit
    • Flow Control: None
    • Character Encoding: UTF-8
  • Button 2(USR-BTN1/SW2) behaviour
    • 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
    • Blue LED flashes one time every 3 seconds
    • Blue LED flashes two times every 3 seconds
    • Blue LED flashes two times every 1.5 seconds

Testing

Connect the WBZ351 Curiosity board to PC, program the precompiled hex file or application example as mentioned. Open TeraTerm @ (Speed: 115200, Data: 8-bit, Parity: none, stop bits: 1 bit, Flow control: none). Reset the board. After reset, the device will be advertising device name "M-dev".

  1. On the iPhone, remove “M-dev” if it was previously paired
  2. Clear all notifications on the iPhone and it will display "No Older Nofitifcation"

  3. Connect USB cable to the curiosity board from the computer

  4. On the iPhone, open “Settings -> Bluetooth” and click “M-dev” to connect to and pair with curiosity board.
  5. Open Calendar App on iphone and set an event that will alert 2 minutes later.
  6. The notification of the event pops out when time is up

  7. 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 to Get Notification Attributes:
  8. On the curiosity board, press Button 2(USR-BTN1/SW2) as the printed texts instruct

  9. 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 to Get App Attributes:
  10. On the curiosity board, press Button 2 as the printed texts instruct

  11. 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 :
  12. On the curiosity board, double-click Button 2(USR-BTN1/SW2) as the printed texts instruct
  13. 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
  14. Reset the board and the iphone will automatically link back

Developing this Application from scratch using MPLAB Code Configurator

This section explains the steps required by a user to develop this application example from scratch using MPLABx Code Configurator

Tip: New users of MPLAB Code Configurator are recommended to go through the overview.

  1. Create a new MCC Harmony Project -- link for instructions

  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". Users should follow the instructions mentioned here to import the component configuration.

  3. Accept Dependencies or satisfiers, select "Yes"

  4. Verify if the Project Graph window has all the expected configuration

Verify MCC Component Settings

  1. Apple Notification Center Service Profile component settings

    Enable ANCS Client Role.

  2. BLE_Stack component settings
    • 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"

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

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

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

Autogenerated, advertisement Data Format

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

Tip: 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

Tip: definitions.h is not specific to just UART peripheral, instead it should 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.

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.

    Table 4-5. Time out parameters
    Variable Value Description
    timeout_adv 60s The timeout of advertising
    timeout_adv_dir 30s The timeout of directed advertising
    Table 4-6. LED vs. State
    Firmware State LED behavior
    APP_ANCS_STATE_IDLE All LEDs are off
    APP_ANCS_STATE_ADV Blue LED flashes one time every 3 seconds. (On: 50 ms, Off: 2950 ms)
    APP_ANCS_STATE_ADV_DIR Blue LED flashes two time every 3 seconds. (On: 50 ms, Off: 50 ms)
    APP_ANCS_STATE_CONN Blue 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".

References