diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-01-18 12:52:17 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-01-18 12:52:17 +0100 |
commit | fe238450f161a503d61c5ae59ecdd82c60c0e9ec (patch) | |
tree | 075ac1aa9b90d6895505d203a806d262bc41f273 /app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java | |
parent | f3288422d8dec949fcad33a84e413d8aa45f4500 (diff) | |
download | io.trygvis.soilmoisture-android-fe238450f161a503d61c5ae59ecdd82c60c0e9ec.tar.gz io.trygvis.soilmoisture-android-fe238450f161a503d61c5ae59ecdd82c60c0e9ec.tar.bz2 io.trygvis.soilmoisture-android-fe238450f161a503d61c5ae59ecdd82c60c0e9ec.tar.xz io.trygvis.soilmoisture-android-fe238450f161a503d61c5ae59ecdd82c60c0e9ec.zip |
BtPromise: Adding a distinction between successful and failure.1.0
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.
Diffstat (limited to 'app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java')
-rw-r--r-- | app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java | 63 |
1 files changed, 41 insertions, 22 deletions
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<SmDevice> 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); |