From fbddd8967dd34f093dc3d7d7537d8869daefb2ad Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 20 Apr 2018 14:52:14 +0100 Subject: handle exceptions from BLE --- sensor/thermostat.ino | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) (limited to 'sensor/thermostat.ino') diff --git a/sensor/thermostat.ino b/sensor/thermostat.ino index 0b90053..e6090ac 100644 --- a/sensor/thermostat.ino +++ b/sensor/thermostat.ino @@ -82,29 +82,36 @@ uint32_t send_data_and_get_time(BLEAddress* pAddress,String* data) { String status = "SC"; status += pClient->getPeerAddress().toString().c_str(); show(status); - BLERemoteService* pThermoService = pClient->getService(thermo_service_uuid); - if (!pThermoService) { - show("NO SERVICE"); - return 60; - } - BLERemoteCharacteristic* pTemp = pThermoService->getCharacteristic(thermo_temp_uuid); - if (!pTemp) { - show("NO TEMP CHAR"); - return 60; + try { + BLERemoteService* pThermoService = pClient->getService(thermo_service_uuid); + if (!pThermoService) { + show("NO SERVICE"); + return 60; + } + BLERemoteCharacteristic* pTemp = pThermoService->getCharacteristic(thermo_temp_uuid); + if (!pTemp) { + show("NO TEMP CHAR"); + return 60; + } + show("SCw"); + pTemp->writeValue(data->c_str(),data->length()); + show("SCWr"); + BLERemoteCharacteristic* pTime = pThermoService->getCharacteristic(thermo_time_uuid); + if (!pTime) { + show("NO TIME CHAR"); + return 60; + } + std::string next_time = pTime->readValue(); + show("SCWRd"); + pClient->disconnect(); + show("SCWRD"); + return String(next_time.c_str()).toInt(); } - show("SCw"); - pTemp->writeValue(data->c_str(),data->length()); - show("SCWr"); - BLERemoteCharacteristic* pTime = pThermoService->getCharacteristic(thermo_time_uuid); - if (!pTime) { - show("NO TIME CHAR"); - return 60; + catch (...) { + show("Fail"); + pClient->disconnect(); + return 10; } - std::string next_time = pTime->readValue(); - show("SCWRd"); - pClient->disconnect(); - show("SCWRD"); - return String(next_time.c_str()).toInt(); } void setup() { -- cgit v1.2.3