NRF52 ble_db_discovery failed to initialize correctly when repeats the service discovery

Hi

We are using NRF52 and SDK 11 and S132.

The nrf52 acts as a central and selectively connects to the closest phones (ios) that are acting as a pheripheral. For this purpose we have 4 characteristics (TX, RX, RSSI and heartbeat-HB )

The application works in the following way, first we scan for advertisement reports for 1 sec storing peer_addr and rssi for each device. Then we check if we wan't to connect/disconnect before we start scanning again for 1 sec.

In case of a disconnect we call

err_code = sd_ble_gap_disconnect(
	    m_ble_ba_c[idx].conn_handle, 
	    BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION
); 

and wait for the BLE_BA_C_EVT_DISCONNECTED event to set the connection handle to BLE_CONN_HANDLE_INVALID and start scanning.

In case of a Connect we call

err_code = sd_ble_gap_connect(
    peer_addr,
    &m_scan_params,
    &m_connection_param
);

and wait for the BLE_GAP_EVT_CONNECTED event to call

err_code = ble_db_discovery_start(  
    &m_ble_db_discovery[p_gap_evt->conn_handle], 
    p_gap_evt->conn_handle
);

to discover characteristics, then we enable notification on the RX characteristic and indication on the heartbeat characteristic with

sd_ble_gattc_write(conn_handle, &write_params);

waiting for BLE_GATTC_EVT_WRITE_RSP inbetween before we start scanning.

In most cases this works as intended and the nordic chip connects to the phone and the phone receives two didSubscribeToCharacteristic events and we can send data between the two devices.

However in some cases the didSubscribeToCharacteristic event is followed by a didUnsubscribeFromCharacteristic event and no data can be sent between them, if this happens it doesn't matter how many times we disconnect/connect from the nordic chip to that phone, it never receives another didSubscribeToCharacteristic and the only solution seems to be either to disable/enable bluetooth on the phone or to restart the nordic chip.

In case of this error we never see anything strange in the connection routine on the nordic chip, we establish a connection (bluetooth logo on iphone turns black), discover the characteristics and enable notification/indication with out any error messages.

Is it possible that the nordic chip is unsubscribing somewhere in the background and we are not picking up the correct event type for the callback?

Do we need to do something more than call sd_ble_gap_disconnect() when we are disconnecting?

Is the nordic chip possibly storing old connections and/or not connecting properly if we connect again within a certain time?

I would suspect that this is a problem with the ios app, however we do have an old implementation that works the same on an older ble chip not from Nordic and there we don't see the same behaviour.

thanks, Unnar