@@ -31,10 +31,14 @@ void ImmediateAlertClient::Init() {
3131
3232bool ImmediateAlertClient::OnDiscoveryEvent (uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_svc* service) {
3333 if (service == nullptr && error->status == BLE_HS_EDONE) {
34- if (isDiscovered ) {
34+ if (iasHandles. has_value () ) {
3535 NRF_LOG_INFO (" [IAS] service found, starting characteristics discovery" );
3636
37- ble_gattc_disc_all_chrs (connectionHandle, iasStartHandle, iasEndHandle, OnImmediateAlertCharacteristicDiscoveredCallback, this );
37+ ble_gattc_disc_all_chrs (connectionHandle,
38+ iasHandles->startHandle ,
39+ iasHandles->endHandle ,
40+ OnImmediateAlertCharacteristicDiscoveredCallback,
41+ this );
3842 } else {
3943 NRF_LOG_INFO (" [IAS] service not found" );
4044 onServiceDiscovered (connectionHandle);
@@ -44,9 +48,10 @@ bool ImmediateAlertClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble
4448
4549 if (service != nullptr && ble_uuid_cmp (&immediateAlertClientUuid.u , &service->uuid .u ) == 0 ) {
4650 NRF_LOG_INFO (" [IAS] discovered : 0x%x - 0x%x" , service->start_handle , service->end_handle );
47- isDiscovered = true ;
48- iasStartHandle = service->start_handle ;
49- iasEndHandle = service->end_handle ;
51+ iasHandles.emplace (HandleRange {
52+ .startHandle = service->start_handle ,
53+ .endHandle = service->end_handle ,
54+ });
5055 }
5156 return false ;
5257}
@@ -62,8 +67,8 @@ int ImmediateAlertClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle,
6267 }
6368
6469 if (characteristic == nullptr && error->status == BLE_HS_EDONE) {
65- if (!isCharacteristicDiscovered ) {
66- NRF_LOG_INFO (" [IAS] Characteristic discovery unsuccessful " );
70+ if (!alertLevelHandle. has_value () ) {
71+ NRF_LOG_INFO (" [IAS] Alert level characteristic not found. " );
6772 onServiceDiscovered (conn_handle);
6873 }
6974
@@ -72,7 +77,6 @@ int ImmediateAlertClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle,
7277
7378 if (characteristic != nullptr && ble_uuid_cmp (&alertLevelCharacteristicUuid.u , &characteristic->uuid .u ) == 0 ) {
7479 NRF_LOG_INFO (" [IAS] Characteristic discovered : 0x%x" , characteristic->val_handle );
75- isCharacteristicDiscovered = true ;
7680 alertLevelHandle = characteristic->val_handle ;
7781 }
7882 return 0 ;
@@ -93,7 +97,10 @@ bool ImmediateAlertClient::SendImmediateAlert(ImmediateAlertClient::Levels level
9397 if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
9498 return false ;
9599 }
100+ if (!alertLevelHandle.has_value ()) {
101+ return false ;
102+ }
96103
97- ble_gattc_write_no_rsp (connectionHandle, alertLevelHandle, om);
104+ ble_gattc_write_no_rsp (connectionHandle, * alertLevelHandle, om);
98105 return true ;
99106}
0 commit comments