4.2.3 BLE ANCS Application
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.
Recommended Reads
Hardware Required
Tool | Qty |
---|---|
WBZ351 Curiosity Board | 1 |
Micro USB cable | 1 |
iPhone | 1 |
SDK Setup
Software
Smartphone App
None
Programming the Precompiled Hex File or Application Example
Programming the .hex
File using MPLAB X IPE
- Precompiled
.hex
file is located in"<Harmony Content Path>\wireless_apps_pic32cxbz3_wbz35\apps\ble\advanced_applications\ble_ancs_app\hex"
folder -
Follow the steps mentioned here
Programming the Application using MPLAB X IDE
-
Follow steps mentioned in the Running a Precompiled Example section.
- 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
- Connect the WBZ351 Curiosity Board to PC, program the
precompiled
.hex
file or application example as mentioned. - 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
- Reset the board. After reset, the device will be advertising device name "M-dev".
- On the iPhone, remove “M-dev” if it was previously paired
- Clear all notifications on the iPhone and it will display "No Older Nofitifcation"
- Connect USB cable to the curiosity board from the computer
- On the iPhone, open Settings>Bluetooth and click M-dev to connect to and pair with curiosity board.
- Open Calendar App on phone and set an event that will alert 2 minutes later.
- The notification of the event pops out when time is up
- 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:
- On the curiosity board, press Button 2 (USR-BTN1/SW2) as the printed texts instruct
- 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:
- On the curiosity board, press Button 2 (USR-BTN1/SW2) as the printed texts instruct
- 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 :
- On the curiosity board, double-click Button 2 (USR-BTN1/SW2) as the printed texts instruct
- 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
- Reset the board and the iphone will automatically link back
Developing the Application from Scratch using MCC
- Create a new MCC Harmony Project. For more details, refer to Creating a New MCC Harmony Project.
- 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
- Accept Dependencies or satisfiers, select Yes
- Verify if the project graph window has all the expected configuration. as illustrated in the following figure:
Verify MCC Component Settings
- Select Apple Notification
Center Service Profile component in project graph
-
Enable ANCS Client Role.
-
- Select BLE Stack component
in project graph, and configure as mentioned below
- GAP(Generic Access
Profile) Settings
- Set Device Name to M-dev
- Include <<Flag>> type of Advertising Data (AD)
- Include <<Local Name>> AD
- 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
- Enable "Peripheral"
- GAP(Generic Access
Profile) Settings
Generate Code
For more details on code generation, refer to MPLAB Code Configurator(MCC) Code Generation
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.
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 |
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 theapp.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
inapp.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
definitions.h
is not specific to just UART peripheral, instead it must be included in all
application source files where peripheral functionality will be
exercisedConfigure 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};
//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);
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
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 fortimeout_adv
seconds.APP_ANCS_STATE_ADV_DIR
: The application is waiting for reconnection fortimeout_adv_dir
seconds.APP_ANCS_STATE_CONN
: The application is ready for sending commands/receiving notifications.Table 4-27. Time Out Parameters Variable Value Description timeout_adv
60s The timeout of advertising timeout_adv_dir
30s The timeout of directed advertising Table 4-28. 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