From f4afc04ee7a085a89ad84ba89344bb50ca9e6e04 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 21 Jun 2015 12:27:48 +0200 Subject: o Trying to make the Bluetooth API more C++ idiomatic, a GATT connection has the same lifecycle as a BluetoothGatt. sm-get-value: o Better error handling. --- apps/SoilMoisture.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'apps/SoilMoisture.cpp') diff --git a/apps/SoilMoisture.cpp b/apps/SoilMoisture.cpp index 89c2fe5..d1d8b7c 100644 --- a/apps/SoilMoisture.cpp +++ b/apps/SoilMoisture.cpp @@ -62,10 +62,10 @@ ByteBuffer createSetUpdateInterval(uint8_t sensor, uint8_t interval_in_seconds) write8(interval_in_seconds); } -SoilMoisture SoilMoisture::create(BluetoothGatt &gatt) { - gatt.discoverServices(); +SoilMoisture SoilMoisture::create(shared_ptr gatt) { + gatt->discoverServices(); - auto service = gatt.findService(soil_moisture_service); + auto service = gatt->findService(soil_moisture_service); if (!service) { throw runtime_error("The device is missing the soil moisture service"); @@ -80,8 +80,8 @@ SoilMoisture SoilMoisture::create(BluetoothGatt &gatt) { return SoilMoisture(gatt, *service, *c); } -SoilMoisture::SoilMoisture(BluetoothGatt &gatt, BluetoothGattService &s, BluetoothGattCharacteristic &c) : - gatt(gatt), s(s), c(c) { +SoilMoisture::SoilMoisture(shared_ptr gatt, BluetoothGattService &s, BluetoothGattCharacteristic &c) : + gatt(std::move(gatt)), s(s), c(c) { } ByteBuffer SoilMoisture::writeAndRead(ByteBuffer &requestBytes) { @@ -89,9 +89,9 @@ ByteBuffer SoilMoisture::writeAndRead(ByteBuffer &requestBytes) { uint8_t expectedCode = requestBytes.get8(0); - gatt.writeValue(c, requestBytes); + gatt->writeValue(c, requestBytes); - auto responseBytes = gatt.readValue(c); + auto responseBytes = gatt->readValue(c); if (responseBytes.getSize() < 1) { throw runtime_error("Unexpected number of bytes read: " + to_string(requestBytes.getSize())); -- cgit v1.2.3