Created
December 2, 2020 12:45
-
-
Save pixma/3cab61902dbe9b0e430db06c6286cbe7 to your computer and use it in GitHub Desktop.
NRF52 BLE NUS UART Template: Scanning without Filters and NRFLog removed/disabled.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Only edited portions are mentioned here for main.c from template project to make/customize | |
// for scanning without filter, also NRFLog removed to pack it within 32K limit of Keil for now. | |
// | |
/**@brief Function for initializing the scanning and setting the filters. | |
*/ | |
static void scan_init(void) | |
{ | |
ret_code_t err_code; | |
nrf_ble_scan_init_t init_scan; | |
memset(&init_scan, 0, sizeof(init_scan)); | |
init_scan.connect_if_match = true; | |
init_scan.conn_cfg_tag = APP_BLE_CONN_CFG_TAG; | |
err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler); | |
APP_ERROR_CHECK(err_code); | |
// Omitted section to remove filters while scanning. | |
//err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_UUID_FILTER, &m_nus_uuid); | |
//APP_ERROR_CHECK(err_code); | |
//err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_UUID_FILTER, false); | |
//APP_ERROR_CHECK(err_code); | |
} | |
/////////////// | |
//////////// | |
/**@brief Function for handling BLE events. | |
* | |
* @param[in] p_ble_evt Bluetooth stack event. | |
* @param[in] p_context Unused. | |
*/ | |
static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) | |
{ | |
ret_code_t err_code; | |
ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt; | |
switch (p_ble_evt->header.evt_id) | |
{ | |
// this below event ID will help you to access MAC ID, Complete Name and RSSI of available BLE Device if they are | |
// broadcasting. | |
case BLE_GAP_EVT_ADV_REPORT:// Add this case if not present in template or in example projects | |
{ | |
//Edited By Annim Banerjee | |
const ble_gap_evt_adv_report_t *p_adv_report = &p_gap_evt->params.adv_report;// get a report of the scan | |
compute_is_object_close( p_adv_report ); // Added this method which returns void. | |
/////////// | |
} | |
break; | |
case BLE_GAP_EVT_CONNECTED: | |
err_code = ble_nus_c_handles_assign(&m_ble_nus_c, p_ble_evt->evt.gap_evt.conn_handle, NULL); | |
APP_ERROR_CHECK(err_code); | |
err_code = bsp_indication_set(BSP_INDICATE_CONNECTED); | |
APP_ERROR_CHECK(err_code); | |
// start discovery of services. The NUS Client waits for a discovery result | |
err_code = ble_db_discovery_start(&m_db_disc, p_ble_evt->evt.gap_evt.conn_handle); | |
APP_ERROR_CHECK(err_code); | |
break; | |
case BLE_GAP_EVT_DISCONNECTED: | |
printf("Disconnected. conn_handle: 0x%x, reason: 0x%x", | |
p_gap_evt->conn_handle, | |
p_gap_evt->params.disconnected.reason); | |
break; | |
case BLE_GAP_EVT_TIMEOUT: | |
if (p_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_CONN) | |
{ | |
printf("Connection Request timed out."); | |
} | |
break; | |
case BLE_GAP_EVT_SEC_PARAMS_REQUEST: | |
// Pairing not supported. | |
err_code = sd_ble_gap_sec_params_reply(p_ble_evt->evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL); | |
APP_ERROR_CHECK(err_code); | |
break; | |
case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST: | |
// Accepting parameters requested by peer. | |
err_code = sd_ble_gap_conn_param_update(p_gap_evt->conn_handle, | |
&p_gap_evt->params.conn_param_update_request.conn_params); | |
APP_ERROR_CHECK(err_code); | |
break; | |
case BLE_GAP_EVT_PHY_UPDATE_REQUEST: | |
{ | |
printf("PHY update request."); | |
ble_gap_phys_t const phys = | |
{ | |
.rx_phys = BLE_GAP_PHY_AUTO, | |
.tx_phys = BLE_GAP_PHY_AUTO, | |
}; | |
err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys); | |
APP_ERROR_CHECK(err_code); | |
} break; | |
case BLE_GATTC_EVT_TIMEOUT: | |
// Disconnect on GATT Client timeout event. | |
printf("GATT Client Timeout."); | |
err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle, | |
BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); | |
APP_ERROR_CHECK(err_code); | |
break; | |
case BLE_GATTS_EVT_TIMEOUT: | |
// Disconnect on GATT Server timeout event. | |
printf("GATT Server Timeout."); | |
err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle, | |
BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); | |
APP_ERROR_CHECK(err_code); | |
break; | |
default: | |
break; | |
} | |
} | |
/////////// | |
//////////// | |
// Author: Annim Banerjee | |
#include "math.h" // added which comes along with SDK bundle so provided by Keil for ARM. | |
//If you are using GCC, you may have to provide flag options in compilation phase. | |
//////////////////////// | |
static void compute_is_object_close( const ble_gap_evt_adv_report_t *p_adv_report_data){ | |
// computing Range approximation based on RSSI | |
// Parameters | |
// Distance, Measured Power, RSSI and N as a Contant depends on the Env factor from 2-4 | |
// Formulae : Distance = 10 ^ ((Measured Power — RSSI)/(10 * N)) | |
// Using math lib... | |
const int nConstant = 2; // Considering low strength from surrounding BLE devices if they are adv-ing. | |
const int nMeasuredPower = -69; // BLE uses Measured Power is also known as the 1 Meter RSSI. So consider value of Measured Power = -69 | |
static double approxDistancef = 0.00; | |
// Formulae : Distance = 10 ^ ((Measured Power — RSSI)/(10 * N)) | |
approxDistancef = pow((double)10, (double)( (double)(nMeasuredPower - ((int)p_adv_report_data->rssi))/(double)(10*nConstant) )); | |
if(approxDistancef >= 0.025f ){ | |
// If anything more than 0.05 m approx. range then do not display for now. | |
// printf("Target : %02x%02x%02x%02x%02x%02x\r\n", | |
// p_adv_report_data->peer_addr.addr[0], | |
// p_adv_report_data->peer_addr.addr[1], | |
// p_adv_report_data->peer_addr.addr[2], | |
// p_adv_report_data->peer_addr.addr[3], | |
// p_adv_report_data->peer_addr.addr[4], | |
// p_adv_report_data->peer_addr.addr[5] | |
// ); | |
// printf("This Device RSSI db:%d.\r\n", p_adv_report_data->rssi); | |
// printf("This Device approx. distance from scanner: %fm . \n\r", approxDistancef ); | |
// printf("\r\n"); | |
}else{ | |
// Normal. Other Devices within Approx 1m range. | |
// else, show that this is under 1m range. | |
printf("Target : %02x%02x%02x%02x%02x%02x\r\n", | |
p_adv_report_data->peer_addr.addr[0], | |
p_adv_report_data->peer_addr.addr[1], | |
p_adv_report_data->peer_addr.addr[2], | |
p_adv_report_data->peer_addr.addr[3], | |
p_adv_report_data->peer_addr.addr[4], | |
p_adv_report_data->peer_addr.addr[5] | |
); | |
printf("This Device RSSI db:%d.\r\n", p_adv_report_data->rssi); | |
printf("This Device approx. distance from scanner: %fm . \n\r", approxDistancef ); | |
printf("\r\n"); | |
} | |
} | |
//End of compute_is_object_close() . | |
///////////////// | |
// To remove NRFLog | |
// Disable them from sdk_config.h via help of wizard from keil IDE, which will help you to pack the output within 32K limit. | |
// replace NRF_LOG_XXX by printf or remove them if you wish to. | |
// Clean and then built the project. | |
int main(void) | |
{ | |
// Initialize. | |
//log_init(); | |
timer_init(); | |
uart_init(); | |
buttons_leds_init(); | |
db_discovery_init(); | |
power_management_init(); | |
ble_stack_init(); | |
gatt_init(); | |
nus_c_init(); | |
scan_init(); | |
// Start execution. | |
printf("BLE UART central example started.\r\n"); | |
//NRF_LOG_INFO("BLE UART central example started."); | |
scan_start(); | |
// Enter main loop. | |
for (;;) | |
{ | |
idle_state_handle(); | |
} | |
} | |
// | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment