Skip to content

Commit e63c941

Browse files
committed
fixed conversation
#783 (comment) #783 (comment)
1 parent ea3154a commit e63c941

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
lines changed

cpp_utils/BLEClient.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,22 @@ void BLEClient::gattClientEventHandler(
182182
// - uint16_t conn_id
183183
// - esp_bd_addr_t remote_bda
184184
case ESP_GATTC_DISCONNECT_EVT: {
185-
// If we receive a disconnect event, set the class flag that indicates that we are
186-
// no longer connected.
187-
m_isConnected = false;
188-
if (m_pClientCallbacks != nullptr) {
189-
m_pClientCallbacks->onDisconnect(this);
190-
}
191-
BLEDevice::removePeerDevice(m_appId, true);
192-
esp_ble_gattc_app_unregister(m_gattc_if);
193-
m_semaphoreRssiCmplEvt.give();
194-
m_semaphoreSearchCmplEvt.give(1);
185+
ESP_LOGE(__func__, "disconnect event, conn_id: %d", evtParam->disconnect.conn_id);
186+
if(getConnId() != evtParam->disconnect.conn_id)
195187
break;
188+
m_semaphoreOpenEvt.give(evtParam->disconnect.reason);
189+
if(!m_isConnected)
190+
break;
191+
// If we receive a disconnect event, set the class flag that indicates that we are
192+
// no longer connected.
193+
esp_ble_gattc_close(m_gattc_if, m_conn_id);
194+
m_isConnected = false;
195+
if (m_pClientCallbacks != nullptr) {
196+
m_pClientCallbacks->onDisconnect(this);
197+
}
198+
break;
196199
} // ESP_GATTC_DISCONNECT_EVT
197-
200+
198201
//
199202
// ESP_GATTC_OPEN_EVT
200203
//
@@ -224,8 +227,12 @@ void BLEClient::gattClientEventHandler(
224227
// uint16_t app_id
225228
//
226229
case ESP_GATTC_REG_EVT: {
227-
m_gattc_if = gattc_if;
228-
m_semaphoreRegEvt.give();
230+
if(m_appId == evtParam->reg.app_id){
231+
ESP_LOGI(__func__, "register app id: %d, %d, gattc_if: %d", m_appId, evtParam->reg.app_id, gattc_if);
232+
m_gattc_if = gattc_if;
233+
m_appId = evtParam->reg.app_id;
234+
m_semaphoreRegEvt.give();
235+
}
229236
break;
230237
} // ESP_GATTC_REG_EVT
231238

cpp_utils/BLEDevice.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,22 +162,10 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr;
162162
break;
163163
} // switch
164164
for(auto &myPair : BLEDevice::getPeerDevices(true)) {
165-
BLEClient* client = (BLEClient*)((conn_status_t)myPair.second).peer_device;
166-
bool raiseEvent = false;
167-
switch(event) {
168-
case ESP_GATTC_REG_EVT:
169-
raiseEvent = (myPair.first == param->reg.app_id);
170-
break;
171-
case ESP_GATTC_DISCONNECT_EVT:
172-
raiseEvent = (client->getConnId() == param->disconnect.conn_id && client->getGattcIf() == gattc_if);
173-
break;
174-
default:
175-
raiseEvent = (client->getGattcIf() == gattc_if || client->getGattcIf() == ESP_GATT_IF_NONE || gattc_if == ESP_GATT_IF_NONE);
176-
break;
165+
conn_status_t conn_status = (conn_status_t)myPair.second;
166+
if(((BLEClient*)conn_status.peer_device)->getGattcIf() == gattc_if || ((BLEClient*)conn_status.peer_device)->getGattcIf() == ESP_GATT_IF_NONE || gattc_if == ESP_GATT_IF_NONE){
167+
((BLEClient*)conn_status.peer_device)->gattClientEventHandler(event, gattc_if, param);
177168
}
178-
179-
if(raiseEvent)
180-
client->gattClientEventHandler(event, gattc_if, param);
181169
}
182170

183171
if(m_customGattcHandler != nullptr) {

0 commit comments

Comments
 (0)