aboutsummaryrefslogtreecommitdiffstats
path: root/java/direct_bt/tinyb/DBTAdapter.java
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-05-24 19:57:07 +0200
committerSven Gothel <[email protected]>2020-05-24 19:57:07 +0200
commit461c54a3212f003cbb5f9a0afaeef5fb80a9f57e (patch)
tree97e7da5d77bf57dd41fd295d98fdce8aa379a015 /java/direct_bt/tinyb/DBTAdapter.java
parenteb67f2d4e0100193822df670e17ed659b1debfd4 (diff)
AdapterStatusListener (C++/Java): Add discoveringChanged(..), converge deviceConnected/deviceDisconnected
Add discoveringChanged(..), as it is of high interest to the user to adjust connect/disconnect and GATT operations. Hence add it to this central adapter listener, also being used to synthesize the old fashion TinyB 'discovering' callback. We also expose the 'keepAlive' flag in this callback to the user. Converge deviceConnected(..)/deviceDisconnected(..) -> deviceConnectionChanged(..) to be a bit more efficient. +++ DBTAdapter: keepAlive in startDiscovery defaults to false, aligned w/ TinyB. A new startDiscovery(..) method has been added to the Java API, also adding keepAlive. +++ Java TODO: Add the whitelist add/remove functionality
Diffstat (limited to 'java/direct_bt/tinyb/DBTAdapter.java')
-rw-r--r--java/direct_bt/tinyb/DBTAdapter.java59
1 files changed, 36 insertions, 23 deletions
diff --git a/java/direct_bt/tinyb/DBTAdapter.java b/java/direct_bt/tinyb/DBTAdapter.java
index 727503c7..8f8ca0c6 100644
--- a/java/direct_bt/tinyb/DBTAdapter.java
+++ b/java/direct_bt/tinyb/DBTAdapter.java
@@ -56,15 +56,15 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter
private final Object stateLock = new Object();
private final Object discoveredDevicesLock = new Object();
+ private BluetoothNotification<Boolean> discoverableNotification = null;
+ private volatile boolean isDiscoverable = false;
+ private BluetoothNotification<Boolean> discoveringNotification = null;
private volatile boolean isDiscovering = false;
- private final long discoveringNotificationRef = 0;
- private BluetoothNotification<Boolean> discoverableNotification = null;
- private boolean isDiscoverable = false;
private BluetoothNotification<Boolean> poweredNotification = null;
- private boolean isPowered = false;
+ private volatile boolean isPowered = false;
private BluetoothNotification<Boolean> pairableNotification = null;
- private boolean isPairable = false;
+ private volatile boolean isPairable = false;
private boolean isOpen = false;
private final List<BluetoothDevice> discoveredDevices = new ArrayList<BluetoothDevice>();
@@ -192,6 +192,19 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter
}
@Override
+ public boolean getDiscovering() { return isDiscovering; }
+
+ @Override
+ public synchronized void enableDiscoveringNotifications(final BluetoothNotification<Boolean> callback) {
+ discoveringNotification = callback;
+ }
+
+ @Override
+ public synchronized void disableDiscoveringNotifications() {
+ discoveringNotification = null;
+ }
+
+ @Override
public boolean getPairable() { return isPairable; }
@Override
@@ -240,8 +253,10 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter
@Override
public synchronized boolean startDiscovery() throws BluetoothException {
- return startDiscovery(true);
+ return startDiscovery(false);
}
+
+ @Override
public synchronized boolean startDiscovery(final boolean keepAlive) throws BluetoothException {
removeDevices();
final boolean res = startDiscoveryImpl(keepAlive);
@@ -290,9 +305,6 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter
}
@Override
- public boolean getDiscovering() { return isDiscovering; }
-
- @Override
public native boolean addStatusListener(final AdapterStatusListener l, final BluetoothDevice deviceMatch);
@Override
@@ -302,12 +314,6 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter
public native int removeAllStatusListener();
@Override
- public native void enableDiscoveringNotifications(final BluetoothNotification<Boolean> callback);
-
- @Override
- public native void disableDiscoveringNotifications();
-
- @Override
public void setDiscoveryFilter(final List<UUID> uuids, final int rssi, final int pathloss, final TransportType transportType) {
final List<String> uuidsFmt = new ArrayList<>(uuids.size());
for (final UUID uuid : uuids) {
@@ -361,6 +367,19 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter
}
}
@Override
+ public void discoveringChanged(final BluetoothAdapter adapter, final boolean enabled, final boolean keepAlive, final long timestamp) {
+ if( DEBUG ) {
+ System.err.println("Adapter.StatusListener.DISCOVERING: enabled "+enabled+", keepAlive "+keepAlive+" on "+adapter);
+ }
+ if( isDiscovering != enabled ) {
+ isDiscovering = enabled;
+ if( null != discoveringNotification ) {
+ discoveringNotification.run(enabled);
+ }
+ }
+
+ }
+ @Override
public void deviceFound(final BluetoothDevice device, final long timestamp) {
if( DEBUG ) {
System.err.println("Adapter.StatusListener.FOUND: "+device+" on "+device.getAdapter());
@@ -379,15 +398,9 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter
}
@Override
- public void deviceConnected(final BluetoothDevice device, final long timestamp) {
- if( DEBUG ) {
- System.err.println("Adapter.StatusListener.CONNECTED: "+device+" on "+device.getAdapter());
- }
- }
- @Override
- public void deviceDisconnected(final BluetoothDevice device, final long timestamp) {
+ public void deviceConnectionChanged(final BluetoothDevice device, final boolean connected, final long timestamp) {
if( DEBUG ) {
- System.err.println("Adapter.StatusListener.DISCONNECTED: "+device+" on "+device.getAdapter());
+ System.err.println("Adapter.StatusListener.CONNECTION: connected "+connected+": "+device+" on "+device.getAdapter());
}
}
};