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!

Related