Hello,
On a BLE Central, how can I enable connect with auto connect to a peripheral with previously saved bonding information.
Once paired and bonded, I can recall bonding address information on boot. I use this address to configure the scanning filters on the central but I cannot get the central to connect from a directed advertisement. On boot and on disconnect I add the last connected address to the queue and only attempt direct advertising until reconnected. The peripheral is configured to only reconnect via direct advertisement with the following code taken from the SDK examples.
if (!k_msgq_get(&bonds_queue, &addr, K_NO_WAIT)) { char addr_buf[BT_ADDR_LE_STR_LEN]; adv_param = *BT_LE_ADV_CONN_DIR(&addr); adv_param.options |= BT_LE_ADV_OPT_DIR_ADDR_RPA; err = bt_le_adv_start(&adv_param, NULL, 0, NULL, 0); if (err) { LOG_ERR("Directed advertising failed to start"); return; } bt_addr_le_to_str(&addr, addr_buf, BT_ADDR_LE_STR_LEN); LOG_DBG("Direct advertising to %s started", log_strdup(addr_buf)); }
When testing the same peripheral but replacing the central with the nRF Connect app and selecting "Connect with autoconnect" the peripheral works as intended. When attempting to do the same on an embedded central scanning for the address and connecting to a match I cannot connect to a directed advertisement. However, I can get the central scanning in address filtered mode to connect to a peripheral advertising in regular mode that uses the following advertisement:
static const struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN), }; static const struct bt_data sd[] = { BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_CUSTOM_VAL), }; .... adv_param = *BT_LE_ADV_CONN; adv_param.options |= BT_LE_ADV_OPT_ONE_TIME; err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd)); ....
How can I fix this? The code used on central for scanning is the following:
bt_scan_stop(); bt_scan_filter_remove_all(); bt_scan_filter_disable(); err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_ADDR, addr); if (err) { LOG_ERR("Set filter on last device address error: %d", err); return err; } LOG_DBG("Enabling bonded address filters"); err = bt_scan_filter_enable(BT_SCAN_ADDR_FILTER, false); if (err) { LOG_ERR("Scanning filters cannot be set (%d)", err); return err; } err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE); if (err) { LOG_ERR("Scanning failed to start (%d)", err); return err; } LOG_DBG("Scanning started");