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. --- .../main/java/io/trygvis/android/bt/BtDevice.java | 81 ++++++++---- .../main/java/io/trygvis/android/bt/BtPromise.java | 89 ++++++++----- .../main/java/io/trygvis/android/bt/BtService.java | 9 +- .../io/trygvis/android/bt/DefaultBtService.java | 72 +++++++---- .../soilmoisture/DefaultSoilMoistureService.java | 137 ++++++++++++--------- .../java/io/trygvis/soilmoisture/MainActivity.java | 106 ++++++++++++++-- .../java/io/trygvis/soilmoisture/SmDevice.java | 2 +- .../trygvis/soilmoisture/SoilMoistureService.java | 2 + app/src/main/res/values/strings.xml | 3 + 9 files changed, 347 insertions(+), 154 deletions(-) diff --git a/app/src/main/java/io/trygvis/android/bt/BtDevice.java b/app/src/main/java/io/trygvis/android/bt/BtDevice.java index 2a4a5d8..6a4d92e 100644 --- a/app/src/main/java/io/trygvis/android/bt/BtDevice.java +++ b/app/src/main/java/io/trygvis/android/bt/BtDevice.java @@ -10,10 +10,11 @@ import android.util.Log; import java.util.Date; +import static io.trygvis.android.bt.BtPromise.BtBluetoothGattCallback; import static io.trygvis.android.bt.BtPromise.PromiseResult.detour; import static io.trygvis.android.bt.BtPromise.PromiseResult.stop; -public class BtDevice> { +public class BtDevice { private final static String TAG = BtDevice.class.getSimpleName(); private final DefaultBtService btService; @@ -22,6 +23,7 @@ public class BtDevice> { private Integer rssi; private BtScanResult scanResult; private A tag; + private final String address; private final long id; private final boolean seenBefore; @@ -30,9 +32,7 @@ public class BtDevice> { private boolean connected; private final WrappingBluetoothGattCallback wrappingCallback = new WrappingBluetoothGattCallback(); - public static interface BtDeviceWrapper> { - BtDevice getBtDevice(); - } + private BtBluetoothGattCallback callback; BtDevice(DefaultBtService btService, BluetoothDevice bluetoothDevice, SQLiteDatabase db, BtService.BtDbIntegration btDbIntegration, long id, Integer rssi, @@ -47,6 +47,7 @@ public class BtDevice> { this.lastSeen = lastSeen; this.tag = btDbIntegration.createTag(db, this); + this.address = bluetoothDevice.getAddress(); } public long getId() { @@ -65,7 +66,7 @@ public class BtDevice> { return bluetoothDevice.getName(); } - public int getRssi() { + public Integer getRssi() { return rssi; } @@ -103,8 +104,9 @@ public class BtDevice> { // return true; // } - private BluetoothGattCallback callback; - + /** + * The first handler must handle a onDirect(). + */ public synchronized void withConnection(BtPromise promise) { if (callback != null) { throw new RuntimeException("The current callback is not done."); @@ -112,13 +114,15 @@ public class BtDevice> { Log.i(TAG, "withConnection(), address=" + bluetoothDevice.getAddress() + ", connected: " + (gatt != null)); + BtPromise newPromise; if (gatt == null) { - callback = new BtPromise(). + newPromise = new BtPromise(). ignoreFailureForNext(). onConnectionStateChange((gatt, status, newState) -> { Log.i(TAG, "defaultConnectCallback: status=" + status + ", newState=" + newState); - String address = gatt.getDevice().getAddress(); - //noinspection SimplifiableIfStatement + + btService.sendBroadcast(btService.createDeviceConnection(address)); + if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothGatt.STATE_CONNECTED) { Log.i(TAG, "Connected to " + address); return detour(promise); @@ -136,21 +140,26 @@ public class BtDevice> { return stop(); })); } - }). - asCallback(bluetoothDevice.getAddress()); + }); } else { - callback = promise.asCallback(bluetoothDevice.getAddress()); + newPromise = promise; } - gatt = bluetoothDevice.connectGatt(btService, false, wrappingCallback); - } + callback = newPromise. + onFinally(() -> { + Log.i(TAG, "Promise done, device is available again: address=" + address); + callback = null; + }). + asCallback(bluetoothDevice.getAddress()); -// public synchronized void disconnect() { -// if (gatt != null) { -// gatt.disconnect(); -// gatt = null; -// } -// } + if (gatt == null) { + gatt = bluetoothDevice.connectGatt(btService, false, wrappingCallback); + } else { + callback.onEvent(BtPromise.EventType.onDirect, "", gatt, null, null, + BluetoothGatt.GATT_SUCCESS, + BluetoothGatt.STATE_CONNECTED, gatt); + } + } public boolean connected() { return connected; @@ -184,10 +193,32 @@ public class BtDevice> { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { - BtDevice.this.connected = newState == BluetoothGatt.STATE_CONNECTED; - - if (callback != null) { - callback.onConnectionStateChange(gatt, status, newState); + Log.i(TAG, "Wrapping: onConnectionStateChange: " + + "address=" + gatt.getDevice().getAddress() + ", " + + "status=" + status + ", " + + "newState=" + newState); + + boolean oldConnected = connected; + + BtDevice.this.connected = status == BluetoothGatt.GATT_SUCCESS && + newState == BluetoothGatt.STATE_CONNECTED; + + try { + if (callback != null) { + callback.onConnectionStateChange(gatt, status, newState); + } + } finally { + if (!BtDevice.this.connected) { + if (oldConnected) { + Log.i(TAG, "Wrapper: Lost connection, removing gatt. gatt=" + gatt); + } else { + Log.i(TAG, "Wrapper: Lost connection, was not connected. gatt=" + gatt); + } + + BtDevice.this.gatt = null; + } else { + Log.i(TAG, "Wrapper: connected"); + } } } diff --git a/app/src/main/java/io/trygvis/android/bt/BtPromise.java b/app/src/main/java/io/trygvis/android/bt/BtPromise.java index d4b3b67..c569ec2 100644 --- a/app/src/main/java/io/trygvis/android/bt/BtPromise.java +++ b/app/src/main/java/io/trygvis/android/bt/BtPromise.java @@ -21,7 +21,6 @@ import static io.trygvis.android.bt.BtPromise.EventType.onConnectionStateChange; import static io.trygvis.android.bt.BtPromise.EventType.onDescriptorRead; import static io.trygvis.android.bt.BtPromise.EventType.onDescriptorWrite; import static io.trygvis.android.bt.BtPromise.EventType.onDirect; -import static io.trygvis.android.bt.BtPromise.EventType.onFailure; import static io.trygvis.android.bt.BtPromise.EventType.onFinally; import static io.trygvis.android.bt.BtPromise.EventType.onReliableWriteCompleted; import static io.trygvis.android.bt.BtPromise.EventType.onServicesDiscovered; @@ -29,7 +28,7 @@ import static io.trygvis.android.bt.BtPromise.EventType.onServicesDiscovered; public class BtPromise { private final static String TAG = BtPromise.class.getSimpleName(); private final List actionQ = new ArrayList<>(); - private final List failureQ = new ArrayList<>(); + // private final List failureQ = new ArrayList<>(); private final List finallyQ = new ArrayList<>(); private static final PromiseResult waitForNextEvent = new WaitForNextEvent(); @@ -194,15 +193,15 @@ public class BtPromise { return this; } - public synchronized BtPromise onFailure(Runnable callback) { - failureQ.add(new BtCallback(stopOnFailure(), "onFailure") { - @Override - public void onFailure() { - callback.run(); - } - }); - return this; - } +// public synchronized BtPromise onFailure(Runnable callback) { +// failureQ.add(new BtCallback(stopOnFailure(), "onFailure") { +// @Override +// public void onFailure() { +// callback.run(); +// } +// }); +// return this; +// } public synchronized BtPromise onFinally(Runnable callback) { finallyQ.add(new BtCallback(stopOnFailure(), "finally") { @@ -233,7 +232,7 @@ public class BtPromise { } BtBluetoothGattCallback asCallback(String address) { - return new BtBluetoothGattCallback(address, actionQ, failureQ, finallyQ); + return new BtBluetoothGattCallback(address, actionQ, /*failureQ, */finallyQ); } enum EventType { @@ -294,15 +293,15 @@ public class BtPromise { private final String address; private List actionQ; private int currentAction = 0; - private List failureQ; + // private List failureQ; private List finallyQ; private List events = new ArrayList<>(); private BtBluetoothGattCallback(String address, List actionQ, - List failureQ, List finallyQ) { + /*List failureQ, */List finallyQ) { this.address = address; this.actionQ = new ArrayList<>(actionQ); - this.failureQ = new ArrayList<>(failureQ); +// this.failureQ = new ArrayList<>(failureQ); this.finallyQ = new ArrayList<>(finallyQ); } @@ -318,7 +317,6 @@ public class BtPromise { if (!hasNext()) { Log.d(TAG, "All Bluetooth actions are done, no handler for last event."); - showEvents(); doFinally(); return; } @@ -327,7 +325,8 @@ public class BtPromise { if (!success) { if (btCallback.stopOnFailure) { - doFailure(); +// doFailure(); + doFinally(); return; } else { Log.i(TAG, "Last status was a failure, but the callback still want it."); @@ -350,11 +349,15 @@ public class BtPromise { if (result instanceof Stop) { Log.i(TAG, "The chain want to stop."); doFinally(); - } else if (result instanceof Detour) { + return; + } + + if (result instanceof Detour) { BtPromise detour = ((Detour) result).promise; Log.i(TAG, "Adding detour with " + detour.actionQ.size() + " actions."); - events.add("detour, action size=" + detour.actionQ.size() + ", failure size=" + - detour.failureQ.size() + ", finally size=" + detour.finallyQ.size()); + events.add("detour, action size=" + detour.actionQ.size() + ", " + +// "failure size=" + detour.failureQ.size() + ", " + + "finally size=" + detour.finallyQ.size()); Log.i(TAG, "hasNext(): " + hasNext()); Log.i(TAG, "currentAction: " + currentAction); @@ -363,13 +366,17 @@ public class BtPromise { } actionQ.addAll(currentAction, detour.actionQ); - failureQ.addAll(detour.failureQ); +// failureQ.addAll(detour.failureQ); finallyQ.addAll(detour.finallyQ); Log.i(TAG, "hasNext(): " + hasNext()); if (hasNext()) { Log.i(TAG, "next action: " + actionQ.get(currentAction).name); } - } else if (result instanceof ContinueDirectly) { + + result = PromiseResult.continueDirectly(gatt); + } + + if (result instanceof ContinueDirectly) { value = ((ContinueDirectly) result).value; onEvent(onDirect, "value=" + value, null, null, null, BluetoothGatt.GATT_SUCCESS, 0, value); @@ -382,7 +389,8 @@ public class BtPromise { } } catch (NotOverriddenException e) { Log.w(TAG, "Unexpected callback by listener: " + key); - doFailure(); +// doFailure(); + doFinally(); } } @@ -390,27 +398,38 @@ public class BtPromise { return currentAction < actionQ.size(); } - private void doFailure() { - showEvents(); - - Log.w(TAG, "Executing " + failureQ.size() + " failure handlers"); - - for (BtCallback callback : failureQ) { - callCallback(onFailure, null, null, null, 0, 0, callback, null); - } - - doFinally(); - } +// private void doFailure() { +// showEvents(); +// +// Log.w(TAG, "Executing " + failureQ.size() + " failure handlers"); +// +// for (BtCallback callback : failureQ) { +// callCallback(onFailure, null, null, null, 0, 0, callback, null); +// } +// +// doFinally(); +// } private void showEvents() { StringBuilder msg = new StringBuilder(); msg.append("Address: ").append(address).append("\n"); + msg.append("Event handlers: \n"); for (BtCallback cb : actionQ) { msg.append("- ").append(cb.name).append("\n"); } +// msg.append("Failure handlers: \n"); +// for (BtCallback cb : failureQ) { +// msg.append("- ").append(cb.name).append("\n"); +// } + + msg.append("Finally handlers: \n"); + for (BtCallback cb : finallyQ) { + msg.append("- ").append(cb.name).append("\n"); + } + msg.append("Events received: \n"); for (String event : events) { msg.append("- ").append(event).append("\n"); @@ -420,6 +439,8 @@ public class BtPromise { } private void doFinally() { + showEvents(); + actionQ.clear(); Log.w(TAG, "Executing " + finallyQ.size() + " finally handlers"); diff --git a/app/src/main/java/io/trygvis/android/bt/BtService.java b/app/src/main/java/io/trygvis/android/bt/BtService.java index ff2c68c..06857ee 100644 --- a/app/src/main/java/io/trygvis/android/bt/BtService.java +++ b/app/src/main/java/io/trygvis/android/bt/BtService.java @@ -10,7 +10,7 @@ import java.util.Collection; import io.trygvis.android.Function; -public interface BtService> { +public interface BtService { boolean initialize(BtDbIntegration btDbIntegration); @@ -32,7 +32,7 @@ public interface BtService> { T runTx(Function action); - public static interface BtDbIntegration> { + public static interface BtDbIntegration { A createTag(SQLiteDatabase db, BtDevice a); } @@ -53,10 +53,13 @@ public interface BtService> { public void onScanStarted() { } + public void onScanStopped() { + } + public void onNewDevice(String address) { } - public void onScanStopped() { + public void onDeviceConnection(String address) { } } } diff --git a/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java b/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java index 718a1cd..e42e685 100644 --- a/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java +++ b/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java @@ -24,6 +24,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.HashSet; +import java.util.List; import java.util.Set; import io.trygvis.android.Function; @@ -32,7 +33,7 @@ import io.trygvis.soilmoisture.R; import static java.util.Collections.unmodifiableCollection; -public class DefaultBtService> extends Service implements BtService { +public class DefaultBtService extends Service implements BtService { private final static String TAG = DefaultBtService.class.getSimpleName(); private final IBinder binder = new LocalBinder<>(this); @@ -90,6 +91,29 @@ public class DefaultBtService> extends Ser } Log.i(TAG, "Bluetooth initialized"); + + List addresses = runTx(db -> { + String[] columns = {Tables.C_ADDRESS}; + + List as = new ArrayList(); + Cursor cursor = db.query(Tables.T_BT_DEVICE, columns, null, new String[0], null, null, null); + try { + while (cursor.moveToNext()) { + String address = cursor.getString(0); + as.add(address); + } + } finally { + cursor.close(); + } + + return as; + }); + + for (String address : addresses) { + BluetoothDevice bluetoothDevice = bluetoothAdapter.getRemoteDevice(address); + register(bluetoothDevice, null, null); + } + return true; } @@ -173,7 +197,9 @@ public class DefaultBtService> extends Ser return value; } finally { - db.endTransaction(); + if (db.inTransaction()) { + db.endTransaction(); + } db.close(); } } @@ -185,11 +211,6 @@ public class DefaultBtService> extends Ser private BluetoothAdapter.LeScanCallback leScanCallback = (device, rssi, scanRecord) -> { BtScanResult scanResult = new BtScanResult(scanRecord); - if (!device.getAddress().startsWith("FB:")) { - Log.w(TAG, "filtering out device: " + device.getAddress()); - return; - } - register(device, rssi, scanResult); }; @@ -268,8 +289,8 @@ public class DefaultBtService> extends Ser long now = System.currentTimeMillis(); btDevice = runTx(db -> { - Cursor cursor = db.query("bt_device", new String[]{"id", "first_seen"}, "address=?", - new String[]{address}, null, null, null); + Cursor cursor = db.query(Tables.T_BT_DEVICE, new String[]{Tables.C_ID, Tables.C_FIRST_SEEN}, + Tables.C_ADDRESS + "=?", new String[]{address}, null, null, null); long id; Date firstSeen, lastSeen; @@ -281,14 +302,14 @@ public class DefaultBtService> extends Ser lastSeen = new Date(now); ContentValues values = new ContentValues(); - values.put("last_seen", now); - db.update("bt_device", values, "address=?", new String[]{address}); + values.put(Tables.C_LAST_SEEN, now); + db.update(Tables.T_BT_DEVICE, values, "address=?", new String[]{address}); } else { ContentValues values = new ContentValues(); - values.put("address", address); - values.put("first_seen", now); - values.put("last_seen", now); - id = db.insert("bt_device", null, values); + values.put(Tables.C_ADDRESS, address); + values.put(Tables.C_FIRST_SEEN, now); + values.put(Tables.C_LAST_SEEN, now); + id = db.insert(Tables.T_BT_DEVICE, null, values); firstSeen = lastSeen = new Date(now); } @@ -305,35 +326,44 @@ public class DefaultBtService> extends Ser return btDevice; } - private Intent createScanStarted() { + Intent createScanStarted() { return new Intent(BtServiceListenerBroadcastReceiver.INTENT_NAME). putExtra("event", "scanStarted"); } - private Intent createScanStopped() { + Intent createScanStopped() { return new Intent(BtServiceListenerBroadcastReceiver.INTENT_NAME). putExtra("event", "scanStopped"); } - private Intent createNewDevice(String address) { + Intent createNewDevice(String address) { return new Intent(BtServiceListenerBroadcastReceiver.INTENT_NAME). putExtra("event", "newDevice"). putExtra("address", address); } + Intent createDeviceConnection(String address) { + return new Intent(BtServiceListenerBroadcastReceiver.INTENT_NAME). + putExtra("event", "deviceConnection"). + putExtra("address", address); + } + public static void dispatchEvent(Intent intent, BtServiceListenerBroadcastReceiver listener) { String event = intent.getStringExtra("event"); Log.i(TAG, "Dispatching event " + intent.getAction() + "/" + event); switch (event) { - case "newDevice": - listener.onNewDevice(intent.getStringExtra("address")); - break; case "scanStarted": listener.onScanStarted(); break; case "scanStopped": listener.onScanStopped(); break; + case "newDevice": + listener.onNewDevice(intent.getStringExtra("address")); + break; + case "deviceConnection": + listener.onDeviceConnection(intent.getStringExtra("address")); + break; default: break; } 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(() -> { diff --git a/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java b/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java index 3a88f6c..385a566 100644 --- a/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java +++ b/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java @@ -1,6 +1,10 @@ package io.trygvis.soilmoisture; import android.app.ActionBar; +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.app.DialogFragment; import android.app.ListActivity; import android.app.ProgressDialog; import android.content.ComponentName; @@ -29,6 +33,7 @@ import java.util.List; import io.fabric.sdk.android.Fabric; import io.trygvis.android.LocalBinder; import io.trygvis.android.bt.BtActivitySupport; +import io.trygvis.android.bt.BtDevice; import static io.trygvis.soilmoisture.ExceptionHandler.EXCEPTION_HANDLER; import static io.trygvis.soilmoisture.SoilMoistureService.SoilMoistureListener; @@ -211,8 +216,52 @@ public class MainActivity extends ListActivity { // // ----------------------------------------------------------------------- - private void onDeviceClick(SmDevice device) { - Log.i(TAG, "onDeviceClick, device=" + device.getBtDevice().getId()); + public static class BtDeviceDialogFragment extends DialogFragment { + + private MainActivity mainActivity; +// private final BtDevice device; +// +// public BtDeviceDialogFragment(MainActivity mainActivity, BtDevice device) { +// this.mainActivity = mainActivity; +// this.device = device; +// } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + + String address = getArguments().getString("address"); + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setMessage(R.string.dialog_probe_device). + setPositiveButton(R.string.yes, (dialog, id) -> mainActivity.probe(address)); + return builder.create(); + } + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + + mainActivity = (MainActivity) activity; + } + } + + private void probe(String address) { + soilMoistureService.probe(address); + } + + private boolean onBtDeviceLongClick(BtDevice device) { + Log.i(TAG, "onBtDeviceClick, device=" + device.getId()); + + BtDeviceDialogFragment dialog = new BtDeviceDialogFragment(); + Bundle arguments = new Bundle(); + arguments.putString("address", device.getAddress()); + dialog.setArguments(arguments); + dialog.show(getFragmentManager(), "BtDeviceDialogFragment"); + + return true; + } + + private void onSmDeviceClick(SmDevice device) { + Log.i(TAG, "onSmDeviceClick, device=" + device.getBtDevice().getId()); } private void onSensorClick(SmSensor sensor) { @@ -266,9 +315,13 @@ public class MainActivity extends ListActivity { current = new ArrayList<>(); List usefulDevices = new ArrayList<>(devices.size()); - List unusefulDevices = new ArrayList<>(devices.size()); + List unusefulDevices = new ArrayList<>(devices.size()); for (SmDevice d : devices) { - (d.isUseful() ? usefulDevices : unusefulDevices).add(d); + if (d.isUseful()) { + usefulDevices.add(d); + } else { + unusefulDevices.add(d.getBtDevice()); + } } List sensors = new ArrayList<>(); for (SmDevice d : devices) { @@ -355,10 +408,12 @@ public class MainActivity extends ListActivity { public int getItemViewType(int position) { Object o = current.get(position); - if (o instanceof SmDevice) { + if (o instanceof BtDevice) { return 0; - } else if (o instanceof SmSensor) { + } else if (o instanceof SmDevice) { return 1; + } else if (o instanceof SmSensor) { + return 2; } throw new RuntimeException("Unknown kind: " + o.getClass()); @@ -366,7 +421,7 @@ public class MainActivity extends ListActivity { @Override public int getViewTypeCount() { - return 2; + return 3; } @Override @@ -393,7 +448,10 @@ public class MainActivity extends ListActivity { public View getView(int position, View view, ViewGroup viewGroup) { Object o = current.get(position); - if (o instanceof SmDevice) { + if (o instanceof BtDevice) { + //noinspection unchecked + return getBtDeviceView((BtDevice) o, view); + } else if (o instanceof SmDevice) { return getSmDeviceView((SmDevice) o, view); } else if (o instanceof SmSensor) { return getSoilSensorView((SmSensor) o, view); @@ -402,12 +460,38 @@ public class MainActivity extends ListActivity { throw new RuntimeException("Not implemented"); } + private View getBtDeviceView(BtDevice device, View view) { + if (view == null) { + view = inflater.inflate(R.layout.fragment_device, null); + view.setTag(new DeviceItem(view)); + view.setClickable(false); + view.setLongClickable(true); + view.setOnLongClickListener(v -> MainActivity.this.onBtDeviceLongClick(device)); + } + + DeviceItem item = (DeviceItem) view.getTag(); + + if (device.getName() != null) { + item.deviceName.setText(device.getName()); + } else { + item.deviceName.setText(R.string.unknown_device); + } + String address = device.getAddress() + ", not useful"; + + item.deviceAddress.setText(address); + + item.rssi.setText(getText(R.string.rssi) + ": " + + (device.getRssi() != null ? valueOf(device.getRssi()) : getText(R.string.unknown))); + + return view; + } + private View getSmDeviceView(SmDevice smDevice, View view) { if (view == null) { view = inflater.inflate(R.layout.fragment_device, null); view.setTag(new DeviceItem(view)); view.setClickable(true); - view.setOnClickListener(v -> onDeviceClick(smDevice)); + view.setOnClickListener(v -> onSmDeviceClick(smDevice)); } DeviceItem item = (DeviceItem) view.getTag(); @@ -430,7 +514,7 @@ public class MainActivity extends ListActivity { item.deviceAddress.setText(address); item.rssi.setText(getText(R.string.rssi) + ": " + - (smDevice.getBtDevice().getRssi() != 0 ? valueOf(smDevice.getBtDevice().getRssi()) : getText(R.string.unknown))); + (smDevice.getBtDevice().getRssi() != null ? valueOf(smDevice.getBtDevice().getRssi()) : getText(R.string.unknown))); boolean useful = smDevice.isUseful(); @@ -440,8 +524,6 @@ public class MainActivity extends ListActivity { item.info.setText(""); } -// view.setClickable(useful); - return view; } diff --git a/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java b/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java index 4cb25f3..5d439bc 100644 --- a/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java +++ b/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java @@ -15,7 +15,7 @@ import static io.trygvis.soilmoisture.SmDevice.SmCmdCode.GET_WARNING_VALUE; import static io.trygvis.soilmoisture.SmDevice.SmCmdCode.SET_SENSOR_NAME; import static io.trygvis.soilmoisture.SmDevice.SmCmdCode.SET_WARNING_VALUE; -class SmDevice implements BtDevice.BtDeviceWrapper { +class SmDevice { private final static String TAG = SmDevice.class.getSimpleName(); final DefaultSoilMoistureService smService; diff --git a/app/src/main/java/io/trygvis/soilmoisture/SoilMoistureService.java b/app/src/main/java/io/trygvis/soilmoisture/SoilMoistureService.java index 643fa08..a287c7b 100644 --- a/app/src/main/java/io/trygvis/soilmoisture/SoilMoistureService.java +++ b/app/src/main/java/io/trygvis/soilmoisture/SoilMoistureService.java @@ -13,6 +13,8 @@ public interface SoilMoistureService { SmDevice getDevice(String address); + void probe(String address); + boolean isScanning(); boolean startScanning(long scanPeriod); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0764a26..2dc3820 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -33,5 +33,8 @@ Current value Show all Group by device + Probe device? + Yes + No -- cgit v1.2.3