From ac6e1b8f92ca8f84c8a3f24f370aad6d6e12b1a6 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Mon, 18 Jan 2021 18:50:31 +0100 Subject: *Adapter: Clarity [get|remove]Devices() -> [get|remove]DiscoveredDevices(); Add removeDiscoveredDevice(); Use: Flush discovered devices after adding listener. Flushing discovered devices after adding listener to adapter. This helps avoiding skipping deviceFound callbacks of earlier found devices before adding the AdapterStatusListener when setting up the adapter. --- java/direct_bt/tinyb/DBTAdapter.java | 41 +++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 8 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 dd1a498f..4a12cd27 100644 --- a/java/direct_bt/tinyb/DBTAdapter.java +++ b/java/direct_bt/tinyb/DBTAdapter.java @@ -116,7 +116,7 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter stopDiscovery(); - final List devices = getDevices(); + final List devices = getDiscoveredDevices(); for(final Iterator id = devices.iterator(); id.hasNext(); ) { final DBTDevice d = (DBTDevice) id.next(); d.close(); @@ -382,7 +382,7 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter synchronized( discoveryLock ) { // Ignoring 'isDiscovering', as native implementation also handles change of 'keepAlive'. // The discoveredDevices shall always get cleared. - removeDevices(); + removeDiscoveredDevices(); return HCIStatusCode.get( startDiscoveryImpl(keepAlive) ); // event callbacks will be generated by implementation } } @@ -398,7 +398,7 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter private native byte stopDiscoveryImpl() throws BluetoothException; @Override - public List getDevices() { + public List getDiscoveredDevices() { synchronized(discoveredDevicesLock) { return new ArrayList(discoveredDevices); } @@ -407,9 +407,9 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter private native List getDiscoveredDevicesImpl(); @Override - public int removeDevices() throws BluetoothException { - final int cj = removeDiscoveredDevices(); - final int cn = removeDevicesImpl(); + public int removeDiscoveredDevices() throws BluetoothException { + final int cj = removeDiscoveredDevicesImpl2j(); + final int cn = removeDiscoveredDevicesImpl1(); if( cj != cn ) { if( DEBUG ) { System.err.println("DBTAdapter::removeDevices: Inconsistent discovered device count: Native "+cn+", callback "+cj); @@ -417,8 +417,8 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter } return cn; } - private native int removeDevicesImpl() throws BluetoothException; - private int removeDiscoveredDevices() { + private native int removeDiscoveredDevicesImpl1() throws BluetoothException; + private int removeDiscoveredDevicesImpl2j() { synchronized(discoveredDevicesLock) { final int n = discoveredDevices.size(); discoveredDevices.clear(); @@ -431,6 +431,31 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter } } + @Override + public boolean removeDiscoveredDevice(final BDAddressAndType addressAndType) { + final boolean cj = removeDiscoveredDeviceImpl2j(addressAndType); + final boolean cn = removeDiscoveredDeviceImpl1(addressAndType.address.b, addressAndType.type.value); + if( cj != cn ) { + if( DEBUG ) { + System.err.println("DBTAdapter::removeDevices("+addressAndType+"): Inconsistent discovered device count: Native "+cn+", callback "+cj); + } + } + return cn; + } + private native boolean removeDiscoveredDeviceImpl1(final byte[] address, final byte addressType); + private boolean removeDiscoveredDeviceImpl2j(final BDAddressAndType addressAndType) { + synchronized(discoveredDevicesLock) { + for( final Iterator iter = discoveredDevices.iterator(); iter.hasNext(); ) { + final BluetoothDevice device = iter.next(); + if( device.getAddressAndType().equals(addressAndType) ) { + iter.remove(); + return true; + } + } + return false; + } + } + @Override public native boolean addStatusListener(final AdapterStatusListener l, final BluetoothDevice deviceMatch); -- cgit v1.2.3