aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/io/trygvis/android/bt/DefaultBtService.java')
-rw-r--r--app/src/main/java/io/trygvis/android/bt/DefaultBtService.java72
1 files changed, 51 insertions, 21 deletions
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<A extends BtDevice.BtDeviceWrapper<A>> extends Service implements BtService<A> {
+public class DefaultBtService<A> extends Service implements BtService<A> {
private final static String TAG = DefaultBtService.class.getSimpleName();
private final IBinder binder = new LocalBinder<>(this);
@@ -90,6 +91,29 @@ public class DefaultBtService<A extends BtDevice.BtDeviceWrapper<A>> extends Ser
}
Log.i(TAG, "Bluetooth initialized");
+
+ List<String> addresses = runTx(db -> {
+ String[] columns = {Tables.C_ADDRESS};
+
+ List<String> as = new ArrayList<String>();
+ 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<A extends BtDevice.BtDeviceWrapper<A>> extends Ser
return value;
} finally {
- db.endTransaction();
+ if (db.inTransaction()) {
+ db.endTransaction();
+ }
db.close();
}
}
@@ -185,11 +211,6 @@ public class DefaultBtService<A extends BtDevice.BtDeviceWrapper<A>> 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<A extends BtDevice.BtDeviceWrapper<A>> 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<A extends BtDevice.BtDeviceWrapper<A>> 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<A extends BtDevice.BtDeviceWrapper<A>> 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;
}