From bc09707edf110da018a68be4d16bdfcee8af600f Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 17 Jan 2015 18:15:52 +0100 Subject: o Not disconnecting when the promise is done. o All user code can assume that they're already connected to the device, simplifying the life cycle. o When starting, send a 'new device' even for all stored devices. --- .../soilmoisture/DefaultSoilMoistureService.java | 137 ++++++++++++--------- 1 file changed, 79 insertions(+), 58 deletions(-) (limited to 'app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java') diff --git a/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java b/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java index 174c245..56f38f4 100644 --- a/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java +++ b/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java @@ -112,64 +112,90 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS sendBroadcast(createNewDevice(address)); + boolean candidate = btDevice.getAddress().startsWith("FB:"); + + if (!candidate) { + Log.w(TAG, "Skipping device: " + btDevice.getAddress()); + markDeviceAsNotUseful(smDevice); + return; + } + if (!smDevice.isProbed()) { - Log.i(TAG, "Probing " + address + ", name=" + btDevice.getName()); - BtPromise executor = new BtPromise(). - onDirect(v -> { - BluetoothGatt gatt = (BluetoothGatt) v; - return gatt.discoverServices() ? waitForNextEvent() : stop(); - }). - onServicesDiscovered(gatt -> { - Log.i(TAG, "Services discovered"); - - BluetoothGattService service = gatt.getService(TrygvisIoUuids.Services.SOIL_MOISTURE_SERVICE); - - if (service == null) { - return stop(); - } - - BluetoothGattCharacteristic soilMoisture = service.getCharacteristic(TrygvisIoUuids.Characteristics.SOIL_MOISTURE); - - if (soilMoisture == null) { - return stop(); - } - - BluetoothGattDescriptor ccg = soilMoisture.getDescriptor(TrygvisIoUuids.CLIENT_CHARACTERISTIC_CONFIG); - ccg.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); - gatt.setCharacteristicNotification(soilMoisture, true); - - return gatt.writeDescriptor(ccg) ? waitForNextEvent() : stop(); - }). - onDescriptorWrite((gatt, descriptor) -> { - Log.i(TAG, "Notifications enabled, getting sensor count"); - BluetoothGattCharacteristic c = descriptor.getCharacteristic(); - c.setValue(createGetSensorCountReq()); - return gatt.writeCharacteristic(c) ? waitForNextEvent() : stop(); - }). - onCharacteristicWrite((gatt, characteristic) -> waitForNextEvent()). - onCharacteristicChanged((gatt, characteristic) -> { - GetSensorCountRes getSensorCountRes = parseResponse(characteristic.getValue(), - GET_SENSOR_COUNT, GetSensorCountRes.class); - - Log.i(TAG, "The device has " + getSensorCountRes.count + " sensors."); - - markDeviceAsUseful(smDevice, getSensorCountRes.count); - - return stop(); - }). - onFinally(() -> { -// btDevice.disconnect(); - - if (smDevice.getIsUseful() == null) { - smDevice.setIsUseful(false); - } - }); - - btDevice.withConnection(executor); + probe(smDevice.getBtDevice().getAddress()); } } }; + public void probe(String address) { + BtDevice btDevice = btService.getDevice(address); + SmDevice smDevice = btDevice.getTag(); + + Log.i(TAG, "Probing " + address + ", name=" + btDevice.getName()); + BtPromise executor = new BtPromise(). + onDirect(v -> { + Log.i(TAG, "Discovering services"); + BluetoothGatt gatt = (BluetoothGatt) v; + return gatt.discoverServices() ? waitForNextEvent() : stop(); + }). + onServicesDiscovered(gatt -> { + Log.i(TAG, "Services discovered"); + + BluetoothGattService service = gatt.getService(TrygvisIoUuids.Services.SOIL_MOISTURE_SERVICE); + + if (service == null) { + return stop(); + } + + BluetoothGattCharacteristic soilMoisture = service.getCharacteristic(TrygvisIoUuids.Characteristics.SOIL_MOISTURE); + + if (soilMoisture == null) { + return stop(); + } + + BluetoothGattDescriptor ccg = soilMoisture.getDescriptor(TrygvisIoUuids.CLIENT_CHARACTERISTIC_CONFIG); + ccg.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); + gatt.setCharacteristicNotification(soilMoisture, true); + + return gatt.writeDescriptor(ccg) ? waitForNextEvent() : stop(); + }). + onDescriptorWrite((gatt, descriptor) -> { + Log.i(TAG, "Notifications enabled, getting sensor count"); + BluetoothGattCharacteristic c = descriptor.getCharacteristic(); + c.setValue(createGetSensorCountReq()); + return gatt.writeCharacteristic(c) ? waitForNextEvent() : stop(); + }). + onCharacteristicWrite((gatt, characteristic) -> waitForNextEvent()). + onCharacteristicChanged((gatt, characteristic) -> { + GetSensorCountRes getSensorCountRes = parseResponse(characteristic.getValue(), + GET_SENSOR_COUNT, GetSensorCountRes.class); + + Log.i(TAG, "The device has " + getSensorCountRes.count + " sensors."); + + markDeviceAsUseful(smDevice, getSensorCountRes.count); + + return stop(); + }). + onFinally(() -> { + if (smDevice.getIsUseful() == null) { + smDevice.setIsUseful(false); + } + }); + + btDevice.withConnection(executor); + } + + private void markDeviceAsNotUseful(SmDevice device) { + btService.runTx(db -> { + device.setIsUseful(false); + + ContentValues values = new ContentValues(); + values.put(Tables.C_USEFUL, false); + db.update(Tables.T_SM_DEVICE, values, "id=?", new String[]{valueOf(device.id)}); + + return null; + }); + } + private void markDeviceAsUseful(SmDevice device, int sensorCount) { btService.runTx(db -> { device.setIsUseful(true); @@ -274,9 +300,6 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS BtPromise promise = new BtPromise(). onDirect(v -> { BluetoothGatt gatt = (BluetoothGatt) v; - return gatt.discoverServices() ? waitForNextEvent() : stop(); - }). - onServicesDiscovered(gatt -> { BluetoothGattService service = gatt.getService(TrygvisIoUuids.Services.SOIL_MOISTURE_SERVICE); BluetoothGattCharacteristic soilMoisture = service.getCharacteristic(TrygvisIoUuids.Characteristics.SOIL_MOISTURE); @@ -298,8 +321,6 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS handleNewSensorValueReady(sensor, getSensorCountRes.value); -// gatt.disconnect(); - return stop(); }). onFinally(() -> { -- cgit v1.2.3