diff options
Diffstat (limited to 'app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java')
-rw-r--r-- | app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java | 69 |
1 files changed, 55 insertions, 14 deletions
diff --git a/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java b/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java index 0f08076..a2723d5 100644 --- a/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java +++ b/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java @@ -3,6 +3,7 @@ package io.trygvis.soilmoisture; import android.app.Service; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; +import android.bluetooth.BluetoothGattDescriptor; import android.bluetooth.BluetoothGattService; import android.content.ComponentName; import android.content.ContentValues; @@ -18,13 +19,17 @@ import java.util.Set; import java.util.TreeSet; import io.trygvis.android.LocalBinder; -import io.trygvis.android.bt.BtActionExecutor; import io.trygvis.android.bt.BtDevice; +import io.trygvis.android.bt.BtPromise; import io.trygvis.android.bt.BtService; import io.trygvis.android.bt.DefaultBtService; import io.trygvis.bluetooth.TrygvisIoUuids; +import static io.trygvis.android.bt.BtPromise.continueDownChain; +import static io.trygvis.android.bt.BtPromise.doneWithChain; import static io.trygvis.android.bt.BtService.BtServiceListenerBroadcastReceiver; +import static io.trygvis.soilmoisture.SmDevice.GetSensorCountRes; +import static io.trygvis.soilmoisture.SmDevice.SmCmdCode.GET_SENSOR_COUNT; public class DefaultSoilMoistureService extends Service implements SoilMoistureService { private final static String TAG = DefaultSoilMoistureService.class.getSimpleName(); @@ -95,37 +100,73 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS if (!smDevice.isProbed()) { Log.i(TAG, "Probing " + address + ", name=" + btDevice.getName()); - BtActionExecutor executor = new BtActionExecutor(). + BtPromise executor = new BtPromise(). onConnectionStateChange((gatt, newState) -> { //noinspection SimplifiableIfStatement if (newState == BluetoothGatt.STATE_CONNECTED) { Log.i(TAG, "Connected to " + address + ", getting services"); - return gatt.discoverServices(); - } + return gatt.discoverServices() ? continueDownChain : doneWithChain; + } else { + Log.i(TAG, "Disconnected from " + address + ", trying again"); + + return new BtPromise().onConnectionStateChange((gatt2, newState2) -> { + if (newState2 == BluetoothGatt.STATE_CONNECTED) { + Log.i(TAG, "Connected to " + address + ", getting services"); + return continueDownChain; + } - Log.i(TAG, "Could not connect to to " + address); - smDevice.setIsUseful(false); - return false; + Log.i(TAG, "Could still not connect to " + address + ", failing."); + + return doneWithChain; + }).toDetour(); + } }). onServicesDiscovered(gatt -> { Log.i(TAG, "Services discovered"); BluetoothGattService service = gatt.getService(TrygvisIoUuids.Services.SOIL_MOISTURE_SERVICE); - boolean useful = false; - if (service != null) { - BluetoothGattCharacteristic characteristic = service.getCharacteristic(TrygvisIoUuids.Characteristics.SOIL_MOISTURE); + if (service == null) { + return doneWithChain; + } - useful = characteristic != null; + BluetoothGattCharacteristic soilMoisture = service.getCharacteristic(TrygvisIoUuids.Characteristics.SOIL_MOISTURE); - smDevice.setIsUseful(useful); + if (soilMoisture == null) { + return doneWithChain; } - gatt.disconnect(); + BluetoothGattDescriptor ccg = soilMoisture.getDescriptor(TrygvisIoUuids.CLIENT_CHARACTERISTIC_CONFIG); + ccg.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); + gatt.setCharacteristicNotification(soilMoisture, true); - return false; + return gatt.writeDescriptor(ccg) ? continueDownChain : doneWithChain; + }). + onDescriptorWrite((gatt, descriptor) -> { + Log.i(TAG, "Notifications enabled, getting sensor count"); + BluetoothGattCharacteristic c = descriptor.getCharacteristic(); + c.setValue(SmDevice.createGetSensorCountReq()); + return gatt.writeCharacteristic(c) ? continueDownChain : doneWithChain; + }). + onCharacteristicWrite((gatt, characteristic) -> continueDownChain). + onCharacteristicChanged((gatt, characteristic) -> { + GetSensorCountRes getSensorCountRes = SmDevice.parseResponse(characteristic.getValue(), + GET_SENSOR_COUNT, GetSensorCountRes.class); + + Log.i(TAG, "The device has " + getSensorCountRes.count + " sensors."); + + smDevice.setIsUseful(true); + smDevice.setSensorCount(getSensorCountRes.count); + + return doneWithChain; }). onFinally(() -> { + btDevice.disconnect(); + + if (smDevice.getIsUseful() == null) { + smDevice.setIsUseful(false); + } + btService.runTx(db -> { if (!btDevice.isSeenBefore() && smDevice.isUseful()) { for (SmSensor soilMonitor : smDevice.getSensors()) { |