Hello everyone,
I am modifying the ble_central_uart example to connect directly with a device using its MAC address. For some reason I don't understand, it doesn't find the device.
As suggested by the docs and the Q/A I could find, I have the code:
/**@brief NUS UUID. */
static ble_uuid_t const m_nus_uuid = {
.uuid = BLE_UUID_NUS_SERVICE,
.type = NUS_SERVICE_UUID_TYPE};
/**
* @brief Starts scanning for the given MAC address.
* @details Scans for the given MAC address and connects to it.
*/
ret_code_t connect_to_mac(uint8_t mac[]) {
ret_code_t err_code;
nrf_ble_scan_stop();
nrf_ble_scan_all_filter_remove(&m_scan);
nrf_ble_scan_init_t init_scan;
ble_gap_addr_t m_mac_filter;
err_code = nrf_ble_scan_copy_addr_to_sd_gap_addr(&m_mac_filter, mac);
if (err_code == NRF_SUCCESS) {
m_mac_filter.addr_type = BLE_GAP_ADDR_TYPE_PUBLIC;
memset(&init_scan, 0, sizeof(init_scan));
init_scan.connect_if_match = true;
init_scan.conn_cfg_tag = APP_BLE_CONN_CFG_TAG;
err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);
if (err_code == NRF_SUCCESS) {
err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_UUID_FILTER, &m_nus_uuid);
if (err_code == NRF_SUCCESS) {
err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_ADDR_FILTER, &m_mac_filter);
if (err_code == NRF_SUCCESS) {
err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_UUID_FILTER | NRF_BLE_SCAN_ADDR_FILTER, true);
if (err_code == NRF_SUCCESS) {
err_code = scan_start();
if (err_code == NRF_SUCCESS) {
err_code = sendStatusToUART(STATUS_SCANNING);
}
}
}
}
}
BLE_ERROR_CHECK(err_code);
} else {
sendErrorToUART(ERROR_INVALID_MAC);
}
return err_code;
}
Even if I remove the UUID filter and just look for the MAC address, the devices are never found.
What am I doing wrong? can you please point me to an explanation/solution?
Thanks,
Juan