summaryrefslogtreecommitdiffstats
path: root/java/direct_bt
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-07-22 02:31:17 +0200
committerSven Gothel <[email protected]>2020-07-22 02:31:17 +0200
commit8b08db6177dc2e5ce6df69d6ea3589a53158b173 (patch)
tree05a361d12fa024dccd7844389d240f797983098e /java/direct_bt
parent60e72dd2776dfd3649a791ad5f8dbab20c77cc1e (diff)
Java DBTAdapter + DBTDevice close @ JVM shutdown: Avoid all interaction, native dtor (deleteImpl) cleans up.
Shortcut of 'close' take-down @ JVM shutdown, avoiding any potential deadlocks. However, all device's and adapter's deleteImpl() gets issued, allowing the native adapter's destructor to be called for orderly device cleanup.
Diffstat (limited to 'java/direct_bt')
-rw-r--r--java/direct_bt/tinyb/DBTAdapter.java41
-rw-r--r--java/direct_bt/tinyb/DBTDevice.java40
-rw-r--r--java/direct_bt/tinyb/DBTManager.java4
3 files changed, 54 insertions, 31 deletions
diff --git a/java/direct_bt/tinyb/DBTAdapter.java b/java/direct_bt/tinyb/DBTAdapter.java
index 637fec05..46879a67 100644
--- a/java/direct_bt/tinyb/DBTAdapter.java
+++ b/java/direct_bt/tinyb/DBTAdapter.java
@@ -87,24 +87,41 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter
@Override
public synchronized void close() {
+ close(false);
+ }
+
+ /* pp */ synchronized void close(final boolean isShutdown) {
if( !isValid() ) {
return;
}
- stopDiscovery();
+ if( !isShutdown ) { // avoid all interaction @ JVM shutdown, native dtor (deleteImpl) cleans up.
- final List<BluetoothDevice> devices = getDevices();
- for(final Iterator<BluetoothDevice> id = devices.iterator(); id.hasNext(); ) {
- final BluetoothDevice d = id.next();
- d.close();
- }
+ // mute all listener first
+ removeAllStatusListener();
+ disableDiscoverableNotifications();
+ disableDiscoveringNotifications();
+ disablePairableNotifications();
+ disablePoweredNotifications();
+
+ stopDiscovery();
+
+ final List<BluetoothDevice> devices = getDevices();
+ for(final Iterator<BluetoothDevice> id = devices.iterator(); id.hasNext(); ) {
+ final DBTDevice d = (DBTDevice) id.next();
+ d.close( false );
+ }
+
+ // done in native dtor: removeDevicesImpl();
- removeAllStatusListener();
- disableDiscoverableNotifications();
- disableDiscoveringNotifications();
- disablePairableNotifications();
- disablePoweredNotifications();
+ } else {
- removeDevicesImpl();
+ final List<BluetoothDevice> devices = getDevices();
+ for(final Iterator<BluetoothDevice> id = devices.iterator(); id.hasNext(); ) {
+ final DBTDevice d = (DBTDevice) id.next();
+ d.close( true );
+ }
+
+ }
discoveredDevices.clear();
super.close();
diff --git a/java/direct_bt/tinyb/DBTDevice.java b/java/direct_bt/tinyb/DBTDevice.java
index a98636bb..1f9ec3be 100644
--- a/java/direct_bt/tinyb/DBTDevice.java
+++ b/java/direct_bt/tinyb/DBTDevice.java
@@ -233,30 +233,36 @@ public class DBTDevice extends DBTObject implements BluetoothDevice
@Override
public synchronized void close() {
+ close(false);
+ }
+
+ /* pp */ synchronized void close(final boolean isShutdown) {
if( !isValid() ) {
return;
}
- disconnect();
+ if( !isShutdown ) { // avoid all interaction @ JVM shutdown, native dtor (deleteImpl) cleans up.
+ disconnect();
- disableConnectedNotifications();
- disableRSSINotifications();
- disableManufacturerDataNotifications();
- disableServicesResolvedNotifications();
+ disableConnectedNotifications();
+ disableRSSINotifications();
+ disableManufacturerDataNotifications();
+ disableServicesResolvedNotifications();
- disableBlockedNotifications();
- disableBlockedNotificationsImpl();
- disablePairedNotifications();
- disablePairedNotificationsImpl();
- disableServiceDataNotifications();
- disableTrustedNotifications();
- // FIXME disableTrustedNotificationsImpl();
+ disableBlockedNotifications();
+ disableBlockedNotificationsImpl();
+ disablePairedNotifications();
+ disablePairedNotificationsImpl();
+ disableServiceDataNotifications();
+ disableTrustedNotifications();
+ // FIXME disableTrustedNotificationsImpl();
- clearServiceCache();
+ clearServiceCache();
- final DBTAdapter a = getAdapter();
- if( null != a ) {
- a.removeStatusListener(statusListener);
- a.removeDiscoveredDevice(this);
+ final DBTAdapter a = getAdapter();
+ if( null != a ) {
+ a.removeStatusListener(statusListener);
+ a.removeDiscoveredDevice(this);
+ }
}
super.close();
}
diff --git a/java/direct_bt/tinyb/DBTManager.java b/java/direct_bt/tinyb/DBTManager.java
index 8dee59a5..45faed64 100644
--- a/java/direct_bt/tinyb/DBTManager.java
+++ b/java/direct_bt/tinyb/DBTManager.java
@@ -315,8 +315,8 @@ public class DBTManager implements BluetoothManager
@Override
public void shutdown() {
for(final Iterator<BluetoothAdapter> ia= adapters.iterator(); ia.hasNext(); ) {
- final BluetoothAdapter a = ia.next();
- a.close();
+ final DBTAdapter a = (DBTAdapter)ia.next();
+ a.close( true );
}
adapters.clear();
deleteImpl(nativeInstance);