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.java100
1 files changed, 72 insertions, 28 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 8638544..51d84af 100644
--- a/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java
+++ b/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java
@@ -13,12 +13,17 @@ import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+import io.trygvis.android.Function;
+import io.trygvis.android.LocalBinder;
import io.trygvis.soilmoisture.R;
-public class DefaultBtService<A> extends Service implements BtService<A> {
+import static java.util.Collections.unmodifiableCollection;
+
+public class DefaultBtService<A extends BtDevice.BtDeviceWrapper<A>> extends Service implements BtService<A> {
private final static String TAG = DefaultBtService.class.getSimpleName();
private final IBinder binder = new LocalBinder<>(this);
@@ -29,19 +34,13 @@ public class DefaultBtService<A> extends Service implements BtService<A> {
// State
// -----------------------------------------------------------------------
- private BtServiceListener<A> serviceListener = new AbstractBtServiceListener<A>() {
- @Override
- public void onNewDevice(BtDevice<A> device) {
- }
- };
-
- private Supplier<A> tagConstructor;
+ private Function<BtDevice<A>, A> tagConstructor;
private BluetoothManager bluetoothManager;
private BluetoothAdapter bluetoothAdapter;
- private final List<BtDevice<A>> devices = new ArrayList<>();
+ private final Set<BtDevice<A>> devices = new HashSet<>();
private boolean scanning = false;
@@ -50,17 +49,13 @@ public class DefaultBtService<A> extends Service implements BtService<A> {
// -----------------------------------------------------------------------
@Override
- public boolean initialize(BtServiceListener<A> serviceListener, Supplier<A> dataSupplier) {
+ public boolean initialize(Function<BtDevice<A>, A> tagConstructor) {
if (bluetoothManager != null) {
- Log.e(TAG, "Already initialized");
+ Log.i(TAG, "Already initialized");
return false;
}
- this.tagConstructor = dataSupplier;
-
- if (serviceListener != null) {
- this.serviceListener = serviceListener;
- }
+ this.tagConstructor = tagConstructor;
// Use this check to determine whether BLE is supported on the device. Then you can
// selectively disable BLE-related features.
@@ -85,7 +80,7 @@ public class DefaultBtService<A> extends Service implements BtService<A> {
return false;
}
- Log.e(TAG, "Bluetooth initialized");
+ Log.i(TAG, "Bluetooth initialized");
return true;
}
@@ -100,13 +95,15 @@ public class DefaultBtService<A> extends Service implements BtService<A> {
@Override
public boolean startScanning(long timeoutMs) {
+ Log.d(TAG, "startScanning, timeoutMs=" + timeoutMs);
+
if (timeoutMs > 0) {
handler.postDelayed(this::stopScanning, timeoutMs);
}
if (bluetoothAdapter.startLeScan(leScanCallback)) {
scanning = true;
- serviceListener.onScanStarted();
+ sendBroadcast(createScanStarted());
return true;
}
@@ -115,15 +112,16 @@ public class DefaultBtService<A> extends Service implements BtService<A> {
@Override
public void stopScanning() {
+ Log.d(TAG, "stopScanning");
+
// This doesn't mind being called twice.
bluetoothAdapter.stopLeScan(leScanCallback);
scanning = false;
- serviceListener.onScanStopped();
+ sendBroadcast(createScanStopped());
}
-// @Override
public BtDevice<A> getDevice(String mac) {
BtDevice<A> device = findDevice(mac);
@@ -136,8 +134,22 @@ public class DefaultBtService<A> extends Service implements BtService<A> {
}
@Override
- public List<BtDevice<A>> getDevices() {
- return Collections.unmodifiableList(devices);
+ public A getTag(String address) {
+ return getDevice(address).getTag();
+ }
+
+ @Override
+ public Collection<BtDevice<A>> getDevices() {
+ return unmodifiableCollection(devices);
+ }
+
+ @Override
+ public Collection<A> getTags() {
+ ArrayList<A> tags = new ArrayList<>();
+ for (BtDevice<A> device : devices) {
+ tags.add(device.getTag());
+ }
+ return tags;
}
// -----------------------------------------------------------------------
@@ -145,8 +157,6 @@ public class DefaultBtService<A> extends Service implements BtService<A> {
// -----------------------------------------------------------------------
private BluetoothAdapter.LeScanCallback leScanCallback = (device, rssi, scanRecord) -> {
-// Log.i(TAG, "onLeScan()");
-
BtScanResult scanResult = new BtScanResult(scanRecord);
register(device, rssi, scanResult);
@@ -173,14 +183,48 @@ public class DefaultBtService<A> extends Service implements BtService<A> {
}
Log.i(TAG, "New device: " + bluetoothDevice.getAddress());
- btDevice = new BtDevice<>(this, bluetoothDevice, tagConstructor.get(), rssi, scanResult);
+ btDevice = new BtDevice<>(this, bluetoothDevice, tagConstructor, rssi, scanResult);
devices.add(btDevice);
- serviceListener.onNewDevice(btDevice);
+ sendBroadcast(createNewDevice(btDevice.getAddress()));
return btDevice;
}
+ private Intent createScanStarted() {
+ return new Intent(BtServiceListenerBroadcastReceiver.INTENT_NAME).
+ putExtra("event", "scanStarted");
+ }
+
+ private Intent createScanStopped() {
+ return new Intent(BtServiceListenerBroadcastReceiver.INTENT_NAME).
+ putExtra("event", "scanStopped");
+ }
+
+ private Intent createNewDevice(String address) {
+ return new Intent(BtServiceListenerBroadcastReceiver.INTENT_NAME).
+ putExtra("event", "newDevice").
+ 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;
+ default:
+ break;
+ }
+ }
+
private BtDevice<A> findDevice(String mac) {
for (BtDevice<A> d : devices) {
if (d.getAddress().equals(mac)) {