The goal of this example is to demonstrate automatic role switching between Central and Peripheral of BLE QN9080 SIP and indicate the proximity of another BLE module using RSSI value.
The automatic Role Switching feature can be used for continuously scan the presence of other BLE device and also to advertise so that other BLE device can scan it. The use case is to maintain social distancing and trigger a warning if the two devices are closer than a threshold distance.
RSSI stands for Received Signal Strength Indicator which shows the power of received radio signal.
Bare metal ‘Wireless_UART’ example is used from ‘SDK_2.x_QN908xCDK’ version 2.2.2
As the device needs to switch its role after every particular time interval, so a timer is required to be initialized as it can be seen in below screenshot.
Next step is to allocate Timer ID to the declared variable and start the timer. In this case, the timer shall go to callback function after the time(seconds) defined by the macro 'gSwitchTime'. This is done in 'BleApp_Config' function.
After the specified time interval, timer stops and enters the callback function where switching of roles takes place. The main point that needs to be highlighted here is that while going into scanning mode, advertising mode should be stopped and vice versa.
In advertising, the LED will be turned off. In scanning, the LED glows based on the RSSI.
While in Central mode, device scans the presence of other bluetooth devices. Here, we need to check the RSSI value of received signals from those devices.
There is a register available in QN9080 where the RSSI can be read after a received signal. RSSI is always negative, so the register stores the 2's compliment of the actual value. Below formula can be used to get the actual value of RSSI:-
Actual RSSI = NOT(RSSI) + 1;
This formula will give the positive value which is inversely proportional to Signal strength.
In the callback function of scanning 'BleApp_ScanningCallback', filtering is applied and following decisions are taken based on filtered value:-
As there is no need to make connections with the available devices, so the function requesting to make connection with the scanned device will be deleted.
Advertising interval can be changed as per requirement by making changes in the following macros:-
To advertise at a fixed interval, value of minimum and maximum interval should be same.
Note:- RSSI varies with environment, surrounding etc., so the threshold value of distance may vary with variation in testing condition.
Demo code is attached for out of the box testing.