This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Whitelisting in Gatt Server mode (SDK 15.2 /nrf52840)

Im using sdk 15.2 with 2 nrf52840 boards, i would like the server to connect to a particular client. How to add the devices to whitelist in sdk 15.2 ?. I have gone through various blogs and  I have used the following snippet, but the server connects to any client available in the vicinity. Please do correct me if im wrong .

ble_gap_addr_t whitelist_addr = {1, BLE_GAP_ADDR_TYPE_PUBLIC,{0x27,0x23,0xBC,0x70,0xF2,0xDC}};
ble_gap_addr_t const *p_whitelist_addr[] = {&whitelist_addr};

ret_code_t err_code;
err_code = sd_ble_gap_whitelist_set(p_whitelist_addr, 1);
if (err_code==NRF_SUCCESS)
{
NRF_LOG_INFO("wHITELIST SET SUCCESSFULLY ");
}
APP_ERROR_CHECK(err_code);

thanks,

Preethi

  • I managed to make it work using respectively \nRF5_SDK_15.2.0\examples\ble_peripheral\ble_app_uart and nRF5_SDK_15.2.0\examples\ble_central\ble_app_uart_c. 

    • In ble_app_uart_c/main.c I figured out the address and address type by using sd_ble_gap_addr_get():

    ret_code_t err_code;
        ble_gap_addr_t mac_addr;
        err_code = sd_ble_gap_addr_get(&mac_addr); 
        APP_ERROR_CHECK(err_code);
    
        if(BLE_GAP_ADDR_TYPE_PUBLIC == mac_addr.addr_type){
            NRF_LOG_INFO("Address type is BLE_GAP_ADDR_TYPE_PUBLIC");
        }else if(BLE_GAP_ADDR_TYPE_RANDOM_STATIC == mac_addr.addr_type){
            NRF_LOG_INFO("Address type is BLE_GAP_ADDR_TYPE_RANDOM_STATIC");
        } else if(BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE == mac_addr.addr_type){
            NRF_LOG_INFO("Address type is BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE");
        } else if(BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE == mac_addr.addr_type){
            NRF_LOG_INFO("Address type is BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE");
        } else if(BLE_GAP_ADDR_TYPE_ANONYMOUS == mac_addr.addr_type){
            NRF_LOG_INFO("Address type is BLE_GAP_ADDR_TYPE_ANONYMOUS");
        }
        
        NRF_LOG_INFO("Address: [%X, %X, %X, %X, %X, %X]", mac_addr.addr[0], mac_addr.addr[1], mac_addr.addr[2], mac_addr.addr[3], mac_addr.addr[4], mac_addr.addr[5]);
    

    • I got the following log:

    Address type is BLE_GAP_ADDR_TYPE_RANDOM_STATIC
    <info> app: Address: [D3, EA, 17, FD, E8, FB]

    In ble_app_uart/main.c I made the following changes:

    • In main.c --> advertising_init() I added the following:

    init.config.ble_adv_whitelist_enabled = true;

    • In the top of main.c -->on_adv_evt() I added the following:

    ble_gap_addr_t whitelist_addr = {1, BLE_GAP_ADDR_TYPE_RANDOM_STATIC, {0xD3, 0xEA, 0x17, 0xFD, 0xE8, 0xFB}};
    ble_gap_addr_t const     *p_whitelist_addr[] = {&whitelist_addr};

    • In  main.c -->on_adv_evt() I added a new case:

            case BLE_ADV_EVT_WHITELIST_REQUEST:
                err_code = sd_ble_gap_whitelist_set(p_whitelist_addr, 1);
                APP_ERROR_CHECK(err_code);
                err_code = ble_advertising_whitelist_reply(&m_advertising, &whitelist_addr, 1, NULL, 0);
                APP_ERROR_CHECK(err_code);
                break;


    You can also use scan filtering, which filters on a higher level of abstraction than the SoftDevice, read more about the difference here.

    Best regards,

    Simon

  • Hi Simon ,

    Thanks for the detailed reply. I am trying on ble_peripheral where im whitelisting while using a scanner. It would be very nice of you if you can show how to whitelist in scanning in a peripheral/gatt server . I have done similar steps in the ble scanning evt handler.

    Thanks,

    Preethi.

  • I will be gone until monday (4th of may) and I'm not able to look at
    your issue until then

    Best regards,
    Simon

  • I have not tested this, but I think I figured out how to do this with the scanner (all changes are done to ble_app_uart_c/main.c):

    • In scan_init(), after memset(&init_scan, 0, sizeof(init_scan)), set init_scan.scan_param->filter_policy=BLE_GAP_SCAN_FP_WHITELIST.
    • In ble_app_uart_c/main.c, under scan_evt_handler()-->case 
      NRF_BLE_SCAN_EVT_WHITELIST_REQUEST you should set the whitelist using 
      sd_ble_gap_whitelist_set()

    Could you report back to me if this works or not.

    Best regards,

    Simon

  • Hi Simon ,

    I tired compiling with your changes and it throw me an error reporting the filter_policy is just read only. I have attached the screen post along with this message.

    Thanks,

    Preethi,

Related