|
19 | 19 | #include <zephyr/logging/log.h>
|
20 | 20 | LOG_MODULE_DECLARE(bt_mgmt_scan);
|
21 | 21 |
|
| 22 | +#define BT_ADDR_LE_STR CONFIG_CONNECT_TO_BT_ADDR_LE_STR /* Replace with desired address */ |
22 | 23 | #define CONNECTION_PARAMETERS \
|
23 | 24 | BT_LE_CONN_PARAM(CONFIG_BLE_ACL_CONN_INTERVAL, CONFIG_BLE_ACL_CONN_INTERVAL, \
|
24 | 25 | CONFIG_BLE_ACL_SLAVE_LATENCY, CONFIG_BLE_ACL_SUP_TIMEOUT)
|
@@ -184,6 +185,73 @@ static bool device_name_check(struct bt_data *data, void *user_data)
|
184 | 185 | return true;
|
185 | 186 | }
|
186 | 187 |
|
| 188 | +/** |
| 189 | + * @brief Check the advertising data for the matching address. |
| 190 | + * |
| 191 | + * @param[in] data The advertising data to be checked. |
| 192 | + * @param[in] user_data Pointer to the address. |
| 193 | + * |
| 194 | + * @retval false Stop going through adv data. |
| 195 | + * @retval true Continue checking the data. |
| 196 | + */ |
| 197 | +static bool addr_check(struct bt_data *data, void *user_data) |
| 198 | +{ |
| 199 | + int ret; |
| 200 | + char addr_string[BT_ADDR_LE_STR_LEN]; |
| 201 | + bt_addr_le_t *addr = user_data; |
| 202 | + struct bt_conn *conn = NULL; |
| 203 | + |
| 204 | + bt_addr_le_to_str(addr, addr_string, BT_ADDR_LE_STR_LEN); |
| 205 | + |
| 206 | + bt_addr_le_t cmp_addr; |
| 207 | + /* BT_ADDR_LE_STR defined at the start of this file */ |
| 208 | + |
| 209 | + ret = bt_addr_le_from_str(BT_ADDR_LE_STR, "public", &cmp_addr); |
| 210 | + if (ret) { |
| 211 | + LOG_ERR("Failed to create bt_addr_le from string: %d", ret); |
| 212 | + return true; |
| 213 | + } |
| 214 | + |
| 215 | + bt_addr_le_to_str(&cmp_addr, addr_string, BT_ADDR_LE_STR_LEN); |
| 216 | + |
| 217 | + if (!bt_addr_le_cmp(addr, &cmp_addr)) { |
| 218 | + /* Check if the device is still connected due to waiting for ACL timeout */ |
| 219 | + if (conn_exist_check(addr)) { |
| 220 | + /* Device is already connected, stop parsing the adv data */ |
| 221 | + return false; |
| 222 | + } |
| 223 | + |
| 224 | + LOG_INF("Device found: %s", srch_name); |
| 225 | + |
| 226 | + bt_le_scan_cb_unregister(&scan_callback); |
| 227 | + cb_registered = false; |
| 228 | + |
| 229 | + ret = bt_le_scan_stop(); |
| 230 | + if (ret) { |
| 231 | + LOG_ERR("Stop scan failed: %d", ret); |
| 232 | + } |
| 233 | + |
| 234 | + bt_addr_le_to_str(addr, addr_string, BT_ADDR_LE_STR_LEN); |
| 235 | + |
| 236 | + LOG_INF("Creating connection to device: %s", addr_string); |
| 237 | + |
| 238 | + ret = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, CONNECTION_PARAMETERS, &conn); |
| 239 | + if (ret) { |
| 240 | + LOG_ERR("Could not init connection: %d", ret); |
| 241 | + |
| 242 | + ret = bt_mgmt_scan_start(0, 0, BT_MGMT_SCAN_TYPE_CONN, NULL, |
| 243 | + BRDCAST_ID_NOT_USED); |
| 244 | + if (ret) { |
| 245 | + LOG_ERR("Failed to restart scanning: %d", ret); |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + return false; |
| 250 | + } |
| 251 | + |
| 252 | + return true; |
| 253 | +} |
| 254 | + |
187 | 255 | /**
|
188 | 256 | * @brief Check the advertising data for the matching 'Set Identity Resolving Key' (SIRK).
|
189 | 257 | *
|
@@ -267,7 +335,11 @@ static void scan_recv_cb(const struct bt_le_scan_recv_info *info, struct net_buf
|
267 | 335 | /* Note: May lead to connection creation */
|
268 | 336 | if (bonded_num < CONFIG_BT_MAX_PAIRED) {
|
269 | 337 | if (server_sirk == NULL) {
|
270 |
| - bt_data_parse(ad, device_name_check, (void *)info->addr); |
| 338 | + if (IS_ENABLED(CONFIG_BT_MGMT_CONNECT_BY_ADDR)) { |
| 339 | + bt_data_parse(ad, addr_check, (void *)info->addr); |
| 340 | + } else { |
| 341 | + bt_data_parse(ad, device_name_check, (void *)info->addr); |
| 342 | + } |
271 | 343 | } else {
|
272 | 344 | bt_data_parse(ad, csip_found, (void *)info->addr);
|
273 | 345 | }
|
|
0 commit comments