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

NCS/Zephyr: how to enable whitelist

My device is peripheral. I added a few lines before the advertisement start in main(). The whole project is in main and simply advertise and get connected and bonded.

    printk("Clear whitelist... ... ...\n", err);
	bt_le_whitelist_clear();
	bt_foreach_bond(BT_ID_DEFAULT, update_whitelist, NULL);
	
    printk("Start advertising... ... ...\n", err);
	err = bt_le_adv_start(BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE|BT_LE_ADV_OPT_FILTER_CONN,
						160, 1600, NULL),
						ad, ARRAY_SIZE(ad),
						sd, ARRAY_SIZE(sd));

update_whitelist() is like this:

static void update_whitelist(const struct bt_bond_info *info, void *user_data){
	char addr[BT_ADDR_LE_STR_LEN];
	bt_addr_le_to_str(&info->addr, addr, sizeof(addr));

	int err = bt_le_whitelist_add(&info->addr);
	if (err){
		printk("whitelist add: %s FAILED!\n", addr);
		error(99);
	}else{
		printk("whitelist add: %s\n", addr);
	}
}

Obviously this doesn't work. After bonded with another device, I reset my device. It seems bt_foreach_bond() doesn't find any bonded device because it doesn't print out anything. I verified bonding is successful. By looking at the serial debug output, I suspect these whitelist calls are too early because the two line "Clear whitelist" and "Start advertising" appeared ahead of board initialization. Is it something to worry? But the advertising works fine. Adding delay also doesn't solve the problem. So I'm not sure what is the problem. Maybe I didn't use the whitelist API correctly?

Any idea? Thanks!

  • Hi Bluebeam,

    Could test and make sure that the bond information is stored properly. The bt_foreach_bond() should return the for each of the peers you bonded. Regardless if you call bt_le_whitelist_clear() or not. 

    Please make sure you turned on CONFIG_BT_SETTINGS and the flash setting:


    CONFIG_BT_SETTINGS=y
    CONFIG_FLASH=y
    CONFIG_FLASH_PAGE_LAYOUT=y
    CONFIG_FLASH_MAP=y
    CONFIG_NVS=y
    CONFIG_SETTINGS=y

  • Aha... I missed CONFIG_SETTINGS. I must somehow mistaken that same as CONFIG_BT_SETTINGS. There're so many CONFIGs. How can I ever know which one is needed for a particular feature... it's frustrating.

    Back to whitelist... ok I think it's working now. bt_foreach_bond() does find all the existing bondings now.

    New question: it seems if the whitelist is empty and filtering is enabled, it will block any incoming connection. Is that expected? If so, I'll need to manually change the advertising options based on whether whitelist is empty or not? Is there any API can tell you how many bonded devices or how many devices in the whitelist?

    Further, how to delete the Settings? I can't find any information regarding how BLE module makes use of storage system and how bondings are stored, and how to delete the bonding. I do notice there's a bt_unpair() which works for a single bonding.Further more, is there something like Device manager/Peer manager in NCS? Or no need for them any more?

    Thanks for help!

  • Hi BlueBeam. 
    I would suggest to have a look at my blog here. It has some information about delete bond information and using filter. 
    You may want to catch scan_filter_no_match() and then manually/directly establish a connection when you acquired the address of the advertiser.

  • Hi Hung,

    Thanks for the info. The blog helps in overall understanding. But my device is peripheral. The main goal is to use whitelist to block unknown central to connect. But we have to allow the first central to connect and bond and then put it into the whitelist. It seems if advertise with an empty whitelist it will block any central device to connect. So I'll need to figure out if there's existing bonded device and adjust the white list.

    Another question, I don;t quite understand why there're so many CONFIG_xxxx. Are they completely compile time thing like macro? Can you change it at run time? For example I figured out I need these CONFIG for fixed passkey pairing:

    CONFIG_BT_FIXED_PASSKEY=y
    CONFIG_BT_SMP_SC_ONLY=y
    CONFIG_BT_TINYCRYPT_ECC=y

    Can these be changed during runtime? Is it possible to switch back and forth between simple pairing (just works) and fixed postcode pairing during run time? That's one of our application's requirements...

    Thanks!

  • Hi Bluebeam, 
    Could you show how you advertise with an empty whitelist ? 
    My understanding is that if bt_le_whitelist_add() is not called the empty whitelist will not be used to filter out connection. If you can provide a simple example that show the issue I can try to test here and figure out a solution. 

    The CONFIG_XXX is macro configuration so it's not possible to change at runtime. You can read more about the configuration here: https://devzone.nordicsemi.com/nordic/nrf-connect-sdk-guides/b/getting-started/posts/nrf-connect-sdk-tutorial---part-2-ncs-v1-4-0#h88sjwm54ukvkhdiku1qtdjdb1685yli

Related