4.2.6 BLE ANPC Application
Getting Started with Peripheral Building Blocks
Introduction
This section helps users to enable the BLE Alert Notification Profile Client (ANPC) on the WBZ351 Curiosity board using the MCC. In this application example, BLE ANPC functions in the Peripheral role and is paired with the BLE ANPS application which functions in the Central role.
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 Requirement
Tool |
Quantity |
---|---|
WBZ351 Curiosity Board |
3 |
Micro USB cable |
2 |
SDK Setup
Software Requirement
Smart phone App
-
None
Programming the Precompiled Hex File or Application Example
Programming the .hex
File using MPLAB X IPE
- Import and program the precompiled
.hex
file located in"<Harmony Content Path>\wireless_apps_pic32cxbz3_wbz35
\apps\ble\advanced_applications\ble_anpc_app\hex\ble_anpc_app.X.production.hex
- Import and program the precompiled
.hex
file located in"<Harmony Content Path>\wireless_apps_pic32cxbz3_wbz35
\apps\ble\advanced_applications\ble_anps_app\hex\ble_anps_app.X.production.hex
- 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
- Follow steps mentioned in the Running a Precompiled Example.
- Open and program the application example
"
ble_anpc_app.X
" located in:<Harmony Content Path>\wireless_apps_pic32cxbz3_wbz35\apps\ble\advanced_applications\ble_anpc_app\firmware\ble_anpc_app.X
using MPLAB X IDE. - Open and program the application example
"
ble_anps_app.X
" located in: :<Harmony Content Path>\wireless_apps_pic32cxbz3_wbz35\apps\ble\advanced_applications\ble_anps_app\firmware\ble_anps_app.X
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 simulating an ANPC on the Curiosity board. The on-board User button, LED and UART are used as user interface during the demo.
- Type of Pressing on User Button
- Press: Press the button for less than 500 ms
- Long press: Press the button for more than 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
- UART Settings
- Baud Rate/Speed: 115200
- Parity: None
- Data Bits: 8
- Stop Bits: 1
- Flow Control: None
- Character Encoding: UTF-8
Testing
Experience the Demo using two WBZ351 Curiosity boards configured as Peripheral and Central device
This section assumes that a user has already programmed the
“ble_anps_app.X
” and “ble_anpc_app.X
” application on two
WBZ351 Curiosity boards.
- Board 1 – WBZ351 Curiosity Board Programmed with “
ble_anpc_app.X
”. Once programmed, the board’s LED will Flash once every 3 seconds and wait for a new connection within 60 seconds.- 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
Note: For more details on setting the “Serial Port” and “Speed”, refer to COM Port Setup. - Reset the board (on-board Reset), Upon Reset, “ANPC init” will be displayed in Tera Term.
- Open TeraTerm and configure as
mentioned below:
- Board 2 – WBZ351 Curiosity Board Programmed with
“
ble_anps_app.X
”. Once programmed, the ANPS device will establish a new connection with the ANPC device. - Once connection is established, the ANPC device stores the pairing data with the ANPS device and the ANPC device’s LED will Flash twice every 1.5 seconds. The following logs will also be displayed in Tera Term.
- Press the user button on the ANPS device to send a new alert notification to the ANPC device and the following will be displayed in Tera Term.
- Press the User button on the ANPS device again to send an unread alert notification to the ANPC device and the following will be displayed in Tera Term.
- Press the User button on the ANPC device to disable the Client Characteristic Configuration Descriptor (CCCD) for new/unread alerts on the ANPS device and the following will be displayed in Tera Term.
- Press the User button on the ANPS device to try to send a new/unread alert notification to the ANPC device. As evidenced in Tera Term, the new/unread alert notification is not received by the ANPC device.
- Unplug the USB cable of the ANPC device then plug it in again. The ANPC device will establish a reconnection with the ANPS device within 30 seconds and the ANPC device’s LED will again blink twice every 1.5 seconds.
- Now, unplug the USB cable of the ANPS
device and program the ANPS precompiled
.hex
file or application example in another Curiosity board.Note: Since the ANPC device stored pairing data with the ANPS device, it will not yet attempt a connection with the ANPS Device 2. - Long press the User button on the ANPC device so that it accepts a new connection within 60 seconds. Once connection is established with the ANPS Device 2, the ANPC device’s LED will blink twice every 1.5 seconds.
Developing this Application from Scratch using MCC
This section explains the steps required by a user to develop this application example from scratch using MCC
-
Create a new MPLAB 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_anpc_app\firmware\ble_anpc_app.X\ble_anpc_app.mc3".
For more details on importing the component configuration , refer to Importing Existing App Example ConfigurationNote: Import and export functionality of Harmony component configuration will help users to start from a known working setup of MCC 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 Alert Notification Profile component in project graph, to open component configuration and configure as illustrated in the following figure.
-
Select BLE Stack component in project graph, to open component configuration and configure as illustrated in the following figure.
Generating a Code
For more details on code generation, refer to MPLAB Code Configurator (MCC) Code Generation.
Files and Routines Automatically Generated by the MCC
Source Files |
Usage |
---|---|
|
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_ble_handler.c |
All GAP, GATT, SMP and L2CAP Event handlers |
src\app_ble\app_anpc_handler.c |
Handle ANPC event from application level |
src\config\default\ble\profile_ble\
ble_anpc\ble_anpc.c |
Handle ANPC 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 own
application.Header Files
ble_gap.h
: contains BLE GAP functions and is automatically included inapp.c
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 User Action
- 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 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 sets the Advertising Data and Scan Response Data of the ANPC application that will be used by the ANPS device to connect.
Starting Advertisement:
-
APP_EnableAdv(bPaired ? APP_ADV_TYPE_WITH_BOND_ADV: APP_ADV_TYPE_ADV)
Connected and Disconnected Events:
- All the possible GAP, GATT, SMP and L2CAP
event handlers are available in
app_ble_handler.c
. Users can implement their own application code to denote Connection State here.
Initialize ANPC:
- ANPC functionality initialization is done
in
APP_BleStackInitAdvance()
Implement ANPC:
- ANPC is implemented in
ble_anpc.c
andBLE_ANPC_BleEventHandler()
will handle all ANPC events.
Firmware FSM (Finite State Machine)
This application firmware implements an ANPC on the Curiosity board.
APP_ANPC_STATE_IDLE
: application remains in the Idle modeAPP_ANPC_STATE_ADV
: application is waiting for a new connection within timeout_adv seconds.APP_ANPC_STATE_WITH_BOND_ADV
: application is waiting for a reconnection within timeout_with_bond_adv seconds.APP_ANPC_STATE_CONN
: application is prepared to send commands/receive notifications.
Variable |
Value |
Description |
---|---|---|
|
60 seconds |
The timeout of advertising without bond |
|
30 seconds |
The timeout of advertising with bond |
Firmware State |
LED behavior |
---|---|
APP_ANPC_STATE_IDLE |
All LEDs are OFF |
APP_ANPC_STATE_ADV |
Blue LED flashes once every 3 seconds. (ON: 50 ms, OFF: 2950 ms) |
APP_ANPC_STATE_WITH_BOND_ADV |
Blue LED flashes twice every 3 seconds. (ON: 50 ms, OFF: 50 ms) |
APP_ANPC_STATE_CONN |
Blue LED flashes twice every 1.5 seconds (ON: 50 ms, OFF: 150 ms) |