5.1.1.2 BLE Legacy Scan
This section explains how to enable the BLE Scanning on the PIC32-BZ6 Curiosity board using MCC. BLE scanning is utilized to detect devices that are in advertising mode. Within BLE protocols, the process is initiated by either a central device or an observer engaging in scanning activities.
Users can choose to either run the precompiled Application Example hex file provided on the PIC32-BZ6 Curiosity Board or follow the steps to develop the application from scratch.
It is recommended to follow the examples in sequence to understand the basic concepts before progressing to the advanced topics.
Recommended Readings
-
Getting Started with Application Building Blocks – See Building Block Examples from Related Links.
-
Getting Started with Peripheral Building Blocks – See Central Devices from Related Links.
-
FreeRTOS and BLE Stack Setup – See Central - FreeRTOS BLE Stack and App Initialize from Related Links.
- See BLE Legacy Scan from Related Links.
- See BLE Connection from Related Links.
-
BLE Software Specification – See MPLAB® Harmony Wireless BLE in Reference Documentation from Related Links.
Hardware Requirement
S. No. | Tool | Quantity |
---|---|---|
1 | PIC32-BZ6 Curiosity Board | 1 |
2 | Micro USB cable | 1 |
SDK Setup
Refer to Getting Started with Software Development from Related Links.
Software
-
To install Tera Term tool, refer to the Tera Term web page in Reference Documentation from Related Links.
Smartphone App
None
Programming the Precompiled Hex File or Application Example
Using MPLAB® X IPE:
-
Import and program the precompiled hex file:
<Harmony Content Path>\wireless_apps_pic32_bz6\apps\ble\building_blocks\central\legacy_scan\precompiled_hex
. - For detailed steps, refer to
Programming a Device in MPLAB® IPE in
Reference Documentation from Related Links.Note: Ensure to choose the correct Device and Tool information.
Using MPLAB® X IDE:
- Perform the following the steps mentioned in Running a Precompiled Example. For more information, refer to Running a Precompiled Application Example from Related Links.
-
Open and program the application:
legacy_scan.X
located in<Harmony Content Path>\wireless_apps_pic32_bz6\apps\ble\building_blocks\central\legacy_scan\firmware
. -
For more details on how to find the Harmony Content Path, refer to Installing the MCC Plugin from Related Links.
Demo Description
This application example enables users to do passive scanning. After programming the application example, on Reset user will be able to see the beginning of scan operation, The Bluetooth addresses of devices will be scanned for the next 10 seconds. After 10 seconds the scanning operation will complete.
Testing
- Using a micro USB cable, connect the Debug USB on the PIC32-BZ6 Curiosity board to a PC.
- Program the precompiled hex file or application example as mentioned.
- 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
- Reset the board, upon Reset, “Scanning” message is displayed on the TeraTerm.
- Once scan operation has begun user will be able to display all the Bluetooth
addresses that are advertising on channel 37, 38 and 39.
Figure 5-11. BLE Scanning TeraTerm - After 10 secs “Scan Completed” message is sent out.
Users can use another PIC32-BZ6 Curiosity Board configured as BLE Legacy Advertisements set the address and scan. For more information, refer to BLE Legacy Advertisements from Related Links.
Developing this Application from Scratch Using MCC
-
Create a new harmony project. For more details, see Creating a New MCC Harmony Project from Related Links.
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_pic32_bz6\apps\ble\building_blocks\central\legacy_scan\firmware\legacy_scan.X.
.- Accept dependencies or satisfiers when prompted.
Verify if the project graph window has all the expected configuration.
Figure 5-12. Project Graph
Verifying the Scan Configuration
- Click on the BLE Stack component in project graph, to open component configuration and configure as illustrated in the following figure.
Figure 5-13. BLE Stack Configuration
Generating a Code
For more details on code generation, refer to MPLAB Code Configurator (MCC) Code Generation from Related Links.
Files and Routines Automatically generated by the MCC
Initialization.c
app_ble.c
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\app_ble.c | Source code for the BLE stack related component configurations, code related to function
calls from app.c |
app_ble\app_ble_handler.c | 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 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 initialization stateAPP_STATE_INIT
inapp.c
User Application Development
Include
- Include the user action. For more information, refer to User Action from Related Links.
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, 100);
// Output the status string to UART
SERCOM0_USART_Write((uint8_t *)"Scanning \r\n", 11);
APP_STATE_INIT
in
app.c
. Scan duration is 10 seconds.app.c
Scan Results
BLE_GAP_EVT_ADV_REPORT
event is generated upon finding advertisements on legacy channels.
// code snippet to print scan results
uint8_t scanAddr[12]; //var to store ASCII address
BLE_GAP_EvtAdvReport_T scanResults;
scanResults.addr = p_event->eventField.evtAdvReport.addr;
APP_HexToAscii(6, scanResults.addr.addr, scanAddr);
SERCOM0_USART_Write((uint8_t *)"0x", 2);
SERCOM0_USART_Write((uint8_t *)scanAddr, 12);
SERCOM0_USART_Write((uint8_t *)"\r\n", 2);
// function to convert HEX to ASCII
static void APP_HexToAscii(uint8_t byteNum, uint8_t *p_hex, uint8_t *p_ascii)
{
uint8_t i, j, c;
uint8_t digitNum = byteNum * 2;
if (p_hex == NULL || p_ascii == NULL)
return;
for (i = 0; i < digitNum; i++)
{
j = i / 2;
c = p_hex[j] & 0x0F;
if (c >= 0x00 && c <= 0x09)
{
p_ascii[digitNum - i - 1] = c + 0x30;
}
else if (c >= 0x0A && c <= 0x0F)
{
p_ascii[digitNum - i - 1] = c - 0x0A + 'A';
}
p_hex[j] /= 16;
}
}
Add the above code to the appropriate sections within the
app_ble_handler.c
.
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.- Add the following code, inside
BLE_GAP_EVT_SCAN_TIMEOUT
case.SERCOM0_USART_Write((uint8_t *)”Scan Completed \r\n”, 17);
Figure 5-19. app_ble_handler.c
- Add the following code, inside
Where to Go from Here
See BLE Connection from Related Links.