From the examples of SDK 17.1.0 (I'm Not using Zephyr), I'm trying to connect to a beacon's Generic Access Service, so I can read the characteristic 0x2A00 and then read it's name. But I'm not able to even connect to it yet.
I scanned the beacon and the service of this access service is: 00001800-0000-1000-8000-00805f9b34fb.
How can I achieve that?
Below is my code.
static ble_uuid_t const m_nus_uuid =
{
.uuid = 0x2A00,
.type = BLE_UUID_TYPE_BLE
};
static ble_gap_scan_params_t const m_scan_param =
{
.active = 0x01,
.interval = NRF_BLE_SCAN_SCAN_INTERVAL,
.window = NRF_BLE_SCAN_SCAN_WINDOW,
.filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL,
.timeout = SCAN_DURATION,
.scan_phys = BLE_GAP_PHY_1MBPS,
};
static void db_discovery_init(void)
{
ble_db_discovery_init_t db_init;
memset(&db_init, 0, sizeof(ble_db_discovery_init_t));
db_init.evt_handler = db_disc_handler;
db_init.p_gatt_queue = &m_ble_gatt_queue;
ret_code_t err_code = ble_db_discovery_init(&db_init);
APP_ERROR_CHECK(err_code);
}
void ble_evt_handler(ble_evt_t const * p_ble_evt)
{
ret_code_t err_code;
ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt;
ble_nus_c_evt_t nus_c_evt;
if(p_ble_evt == NULL)
{
return;
}
if((m_ble_nus_c.conn_handle != BLE_CONN_HANDLE_INVALID)
&& (m_ble_nus_c.conn_handle != p_ble_evt->evt.gap_evt.conn_handle))
{
return;
}
switch(p_ble_evt->header.evt_id)
{
case BLE_GAP_EVT_CONNECTED:
{
err_code = ble_db_discovery_start(&m_db_disc, p_ble_evt->evt.gap_evt.conn_handle);
APP_ERROR_CHECK(err_code);
err_code = ble_nus_c_handles_assign(&m_ble_nus_c, p_ble_evt->evt.gap_evt.conn_handle, NULL);
APP_ERROR_CHECK(err_code);
break;
}
case BLE_GAP_EVT_DISCONNECTED:
{
if(p_ble_evt->evt.gap_evt.conn_handle == m_ble_nus_c.conn_handle
&& m_ble_nus_c.evt_handler != NULL)
{
nus_c_evt.evt_type = BLE_NUS_C_EVT_DISCONNECTED;
m_ble_nus_c.conn_handle = BLE_CONN_HANDLE_INVALID;
m_ble_nus_c.evt_handler(&m_ble_nus_c, &nus_c_evt);
}
break;
}
default:
break;
}
}
static void scan_evt_handler(scan_evt_t const * p_scan_evt)
{
ret_code_t err_code;
switch(p_scan_evt->scan_evt_id)
{
case NRF_BLE_SCAN_EVT_CONNECTING_ERROR:
err_code = p_scan_evt->params.connecting_err.err_code;
APP_ERROR_CHECK(err_code);
break;
case NRF_BLE_SCAN_EVT_CONNECTED:
myFlags.connected = true; //NEVER GOES TRUE
break;
case NRF_BLE_SCAN_EVT_SCAN_TIMEOUT:
myFlags.scanning = false; //used to restart scanning if timeout
break;
default:
break;
}
}
static void scan_init(void)
{
ret_code_t err_code = 0;
nrf_ble_scan_init_t init_scan;
memset(&init_scan, 0, sizeof(init_scan));
init_scan.p_scan_param = &m_scan_param;
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);
APP_ERROR_CHECK(err_code);
err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_UUID_FILTER, &m_nus_uuid);
APP_ERROR_CHECK(err_code);
err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_UUID_FILTER, false);
APP_ERROR_CHECK(err_code);
}
void scan_start(void)
{
ret_code_t err_code;
(void) sd_ble_gap_scan_stop();
//set params
err_code = nrf_ble_scan_params_set(&m_scan, &m_scan_param);
APP_ERROR_CHECK(err_code);
//start scan
err_code = nrf_ble_scan_start(&m_scan);
APP_ERROR_CHECK(err_code);
}
int main(void)
{
// Initialize.
ble_stack_init();
scan_init();
gatt_init();
db_discovery_init();
scan_start();
while (true)
{
// REST OF MY CODE
// ....
// ....
}
}