From bda1e0d415257544c94ebfff87af8424166dd316 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Wed, 30 Sep 2020 11:07:13 +0200 Subject: DBT[Adapter|Device].close(): Utilize own AtomicBoolean synchronization avoiding reentry; DBTAdapter.removeStatusListener(): Avoid @ within isClosing (warning on failure) --- java/direct_bt/tinyb/DBTAdapter.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'java/direct_bt/tinyb/DBTAdapter.java') diff --git a/java/direct_bt/tinyb/DBTAdapter.java b/java/direct_bt/tinyb/DBTAdapter.java index d3e42fab..645f93a1 100644 --- a/java/direct_bt/tinyb/DBTAdapter.java +++ b/java/direct_bt/tinyb/DBTAdapter.java @@ -65,6 +65,8 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter private final Object discoveredDevicesLock = new Object(); private final Object userCallbackLock = new Object(); + private final AtomicBoolean isClosing = new AtomicBoolean(false); + private final AtomicBoolean isPowered = new AtomicBoolean(false); // AdapterSettings private BluetoothNotification userPairableNotificationCB = null; private final AtomicBoolean isDiscoverable = new AtomicBoolean(false); // AdapterSettings @@ -89,10 +91,13 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter public final BluetoothManager getManager() { return DBTManager.getManager(); } @Override - public synchronized void close() { + public void close() { if( !isValid() ) { return; } + if( !isClosing.compareAndSet(false, true) ) { + return; + } // mute all listener first removeAllStatusListener(); disableDiscoverableNotifications(); @@ -386,7 +391,14 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter public native boolean addStatusListener(final AdapterStatusListener l, final BluetoothDevice deviceMatch); @Override - public native boolean removeStatusListener(final AdapterStatusListener l); + public boolean removeStatusListener(final AdapterStatusListener l) { + if( !isClosing.get() ) { + return removeStatusListenerImpl(l); + } + return false; + } + + private native boolean removeStatusListenerImpl(final AdapterStatusListener l); @Override public native int removeAllStatusListener(); -- cgit v1.2.3