aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java')
-rw-r--r--app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java137
1 files changed, 79 insertions, 58 deletions
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<SmDevice> 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(() -> {