@@ -108,7 +108,7 @@ void NimbleController::Init() {
108108}
109109
110110void NimbleController::StartAdvertising () {
111- if (ble_gap_adv_active ()) return ;
111+ if (bleController. IsConnected () || ble_gap_conn_active () || ble_gap_adv_active ()) return ;
112112
113113 ble_svc_gap_device_name_set (deviceName);
114114
@@ -197,6 +197,8 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) {
197197 NRF_LOG_INFO (" disconnect; reason=%d" , event->disconnect .reason );
198198
199199 /* Connection terminated; resume advertising. */
200+ currentTimeClient.Reset ();
201+ alertNotificationClient.Reset ();
200202 connectionHandle = BLE_HS_CONN_HANDLE_NONE;
201203 bleController.Disconnect ();
202204 StartAdvertising ();
@@ -289,10 +291,10 @@ int NimbleController::OnDiscoveryEvent(uint16_t i, const ble_gatt_error *error,
289291
290292int NimbleController::OnCTSCharacteristicDiscoveryEvent (uint16_t connectionHandle, const ble_gatt_error *error,
291293 const ble_gatt_chr *characteristic) {
292- if (characteristic == nullptr && error->status == BLE_HS_EDONE) {
294+ if (characteristic == nullptr && error->status == BLE_HS_EDONE && currentTimeClient. IsCharacteristicDiscovered () ) {
293295 NRF_LOG_INFO (" CTS characteristic Discovery complete" );
294- ble_gattc_read (connectionHandle, currentTimeClient.CurrentTimeHandle (), CurrentTimeReadCallback, this );
295- return 0 ;
296+ auto res = ble_gattc_read (connectionHandle, currentTimeClient.CurrentTimeHandle (), CurrentTimeReadCallback, this );
297+ return res ;
296298 }
297299 return currentTimeClient.OnCharacteristicDiscoveryEvent (connectionHandle, error, characteristic);
298300}
0 commit comments