From fe238450f161a503d61c5ae59ecdd82c60c0e9ec Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 18 Jan 2015 12:52:17 +0100 Subject: BtPromise: Adding a distinction between successful and failure. o Chain can now call fail() instead of stop() to signal failure. o The finally handlers can be changed to get this info later. o Should probably make all callbacks take the BtDevice as a callback instead of BluetoothGatt and make the gatt instance available through the device. This way state can be kept in the device's tag. BtPromise: always discover services when operating inside a connection. --- .../soilmoisture/DefaultSoilMoistureService.java | 63 ++++++++++++++-------- 1 file changed, 41 insertions(+), 22 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 992bf97..25dac5c 100644 --- a/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java +++ b/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java @@ -1,7 +1,6 @@ package io.trygvis.soilmoisture; import android.app.Service; -import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattDescriptor; import android.bluetooth.BluetoothGattService; @@ -13,6 +12,7 @@ import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.IBinder; import android.util.Log; +import android.widget.Toast; import java.util.ArrayList; import java.util.Comparator; @@ -133,14 +133,7 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS 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"); - + onDirect(gatt -> { BluetoothGattService service = gatt.getService(TrygvisIoUuids.Services.SOIL_MOISTURE_SERVICE); if (service == null) { @@ -270,19 +263,44 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS // ----------------------------------------------------------------------- private SmDevice createTag(SQLiteDatabase db, BtDevice btDevice) { - Cursor cursor = db.query(Tables.T_SM_DEVICE, new String[]{Tables.C_ID}, Tables.C_BT_DEVICE + "=?", - new String[]{valueOf(btDevice.getId())}, null, null, null); + Cursor cursor = null; + try { + cursor = db.query(Tables.T_SM_DEVICE, new String[]{Tables.C_ID}, Tables.C_BT_DEVICE + "=?", + new String[]{valueOf(btDevice.getId())}, null, null, null); + + long id; + if (cursor.moveToNext()) { + id = cursor.getLong(0); + } else { + ContentValues values = new ContentValues(); + values.put(Tables.C_BT_DEVICE, btDevice.getId()); + id = db.insert(Tables.T_SM_DEVICE, null, values); + } - long id; - if (cursor.moveToNext()) { - id = cursor.getLong(0); - } else { - ContentValues values = new ContentValues(); - values.put(Tables.C_BT_DEVICE, btDevice.getId()); - id = db.insert(Tables.T_SM_DEVICE, null, values); - } + cursor.close(); + + String[] sensorColumns = { + Tables.C_ID, + Tables.C_INDEX, + }; + cursor = db.query(Tables.T_SM_SENSOR, sensorColumns, Tables.C_SM_DEVICE + "=?", + new String[]{valueOf(id)}, null, null, Tables.C_INDEX); - return new SmDevice(this, btDevice, id); + SmDevice device = new SmDevice(this, btDevice, id); + + if (cursor.moveToNext()) { + device.setIsUseful(true); + do { + device.addSensor(new SmSensor(device, cursor.getLong(0), cursor.getInt(1))); + } while (cursor.moveToNext()); + } + + return device; + } finally { + if (cursor != null) { + cursor.close(); + } + } } void handleNewSensorValueReady(SmSensor sensor, int value) { @@ -302,8 +320,7 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS void readCurrentValue(SmSensor sensor) { BtPromise promise = new BtPromise(). - onDirect(v -> { - BluetoothGatt gatt = (BluetoothGatt) v; + onDirect(gatt -> { BluetoothGattService service = gatt.getService(TrygvisIoUuids.Services.SOIL_MOISTURE_SERVICE); BluetoothGattCharacteristic soilMoisture = service.getCharacteristic(TrygvisIoUuids.Characteristics.SOIL_MOISTURE); @@ -328,6 +345,8 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS return stop(); }). onFinally(() -> { + Toast.makeText(context, R.string.error_could_not_read_value, Toast.LENGTH_SHORT). + show(); }); sensor.getDevice().getBtDevice().withConnection(promise); -- cgit v1.2.3