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. --- .../io/trygvis/android/bt/DefaultBtService.java | 72 +++++++++++++++------- 1 file changed, 51 insertions(+), 21 deletions(-) (limited to 'app/src/main/java/io/trygvis/android/bt/DefaultBtService.java') 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; } -- cgit v1.2.3