diff options
author | Sven Gothel <[email protected]> | 2021-01-18 18:50:31 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2021-01-18 18:50:31 +0100 |
commit | ac6e1b8f92ca8f84c8a3f24f370aad6d6e12b1a6 (patch) | |
tree | c36b2806e63a3835d6a568c9ec2ed2c99d4edfe2 /java/direct_bt/tinyb/DBTAdapter.java | |
parent | cada67fce32dfecf9f8c8709935c13681ad7bb2f (diff) |
*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.
Diffstat (limited to 'java/direct_bt/tinyb/DBTAdapter.java')
-rw-r--r-- | java/direct_bt/tinyb/DBTAdapter.java | 41 |
1 files changed, 33 insertions, 8 deletions
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<BluetoothDevice> devices = getDevices(); + final List<BluetoothDevice> devices = getDiscoveredDevices(); for(final Iterator<BluetoothDevice> 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<BluetoothDevice> getDevices() { + public List<BluetoothDevice> getDiscoveredDevices() { synchronized(discoveredDevicesLock) { return new ArrayList<BluetoothDevice>(discoveredDevices); } @@ -407,9 +407,9 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter private native List<BluetoothDevice> 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(); @@ -432,6 +432,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<BluetoothDevice> 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); @Override |