From d84f46fb579d9c91012e1b8fbebf6c303e092442 Mon Sep 17 00:00:00 2001 From: "LAPTOP-TNA5QQH9\\FabianWessel" Date: Fri, 22 Oct 2021 14:38:14 +0200 Subject: [PATCH] fix multiple occurence of the same device --- .../strings_bluetooth_pairing_preference.xml | 4 +- .../res/values-de/strings_obd_selection.xml | 1 + .../res/values/strings_obd_selection.xml | 1 + .../app/handler/BluetoothHandler.java | 62 ++++++++++--------- .../obdselection/OBDSelectionFragment.java | 31 +++++++--- 5 files changed, 60 insertions(+), 39 deletions(-) diff --git a/org.envirocar.app/res/values-de/strings_bluetooth_pairing_preference.xml b/org.envirocar.app/res/values-de/strings_bluetooth_pairing_preference.xml index c3a9a4291..f5bd30a6a 100644 --- a/org.envirocar.app/res/values-de/strings_bluetooth_pairing_preference.xml +++ b/org.envirocar.app/res/values-de/strings_bluetooth_pairing_preference.xml @@ -20,12 +20,12 @@ --> - Bluetooth Pairing + Bluetooth Kopplung Kein Bluetoothgerät gefunden. Suche nach anderen Bluetoothgeräten... %s Bluetooth devices found. 1 Bluetooth device found. - Delete + Löschen Set Discovery Interval Select the interval (in minutes + seconds) at which the application re-initiates the discovery process. diff --git a/org.envirocar.app/res/values-de/strings_obd_selection.xml b/org.envirocar.app/res/values-de/strings_obd_selection.xml index 07a7384fd..3e94db048 100644 --- a/org.envirocar.app/res/values-de/strings_obd_selection.xml +++ b/org.envirocar.app/res/values-de/strings_obd_selection.xml @@ -27,6 +27,7 @@ Bluetooth ist deaktiviert. Bluetooth ist deaktiviert. Bitte aktivieren Sie Bluetooth, bevor Sie nach anderen Geräten suchen können. Suche gestartet! + Suche beendet! %s ausgewählt. Keine gekoppelten Bluetooth-Geräte. diff --git a/org.envirocar.app/res/values/strings_obd_selection.xml b/org.envirocar.app/res/values/strings_obd_selection.xml index e64999f83..495ed824c 100644 --- a/org.envirocar.app/res/values/strings_obd_selection.xml +++ b/org.envirocar.app/res/values/strings_obd_selection.xml @@ -27,6 +27,7 @@ Bluetooth is disabled. Bluetooth is disabled. Please enable Bluetooth before you can discover other devices. Bluetooth discovery started. + Bluetooth discovery finished. %s selected. No paired Bluetooth devices diff --git a/org.envirocar.app/src/org/envirocar/app/handler/BluetoothHandler.java b/org.envirocar.app/src/org/envirocar/app/handler/BluetoothHandler.java index 4f299ea0d..2e106bbdc 100644 --- a/org.envirocar.app/src/org/envirocar/app/handler/BluetoothHandler.java +++ b/org.envirocar.app/src/org/envirocar/app/handler/BluetoothHandler.java @@ -73,6 +73,8 @@ public class BluetoothHandler { private DisposableObserver mDiscoverySubscription; private boolean mIsAutoconnecting; + BroadcastReceiver bluetoothPairingReceiver; + // The bluetooth adapter private final BluetoothAdapter mBluetoothAdapter; protected final BroadcastReceiver mBluetoothStateChangedReceiver = new BroadcastReceiver() { @@ -516,37 +518,39 @@ public void disableBluetooth(Activity activity) { */ public void pairDevice(final BluetoothDevice device, final BluetoothDevicePairingCallback callback) { - - // Register a new BroadcastReceiver for BOND_STATE_CHANGED actions. - IntentFilter intent = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED); - context.registerReceiver(new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - String action = intent.getAction(); - - // if the action is a change of the pairing state - if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { - - // Get state and previous state. - final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, - BluetoothDevice.ERROR); - final int prevState = intent.getIntExtra( - BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR); - - if (state == BluetoothDevice.BOND_BONDED && - prevState == BluetoothDevice.BOND_BONDING) { - // The device has been successfully paired, inform the callback about - // the successful pairing. - callback.onDevicePaired(device); - } else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice - .BOND_BONDING) { - // It was not able to successfully establishing a pairing to the given - // device. Inform the callback - callback.onPairingError(device); + if (bluetoothPairingReceiver == null){ + bluetoothPairingReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + + // if the action is a change of the pairing state + if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { + + // Get state and previous state. + final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, + BluetoothDevice.ERROR); + final int prevState = intent.getIntExtra( + BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR); + + if (state == BluetoothDevice.BOND_BONDED && + prevState == BluetoothDevice.BOND_BONDING) { + // The device has been successfully paired, inform the callback about + // the successful pairing. + callback.onDevicePaired(device); + } else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice + .BOND_BONDING) { + // It was not able to successfully establishing a pairing to the given + // device. Inform the callback + callback.onPairingError(device); + } } } - } - }, intent); + }; + // Register a new BroadcastReceiver for BOND_STATE_CHANGED actions. + IntentFilter intent = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED); + context.registerReceiver(bluetoothPairingReceiver, intent); + } // Using reflection to invoke "createBond" method in order to pair with a given device. // This method is public in API lvl 18. diff --git a/org.envirocar.app/src/org/envirocar/app/views/obdselection/OBDSelectionFragment.java b/org.envirocar.app/src/org/envirocar/app/views/obdselection/OBDSelectionFragment.java index 764637520..1ba9d2ce7 100644 --- a/org.envirocar.app/src/org/envirocar/app/views/obdselection/OBDSelectionFragment.java +++ b/org.envirocar.app/src/org/envirocar/app/views/obdselection/OBDSelectionFragment.java @@ -116,10 +116,15 @@ public interface ShowSnackbarListener { private Disposable mBTDiscoverySubscription; + private boolean isResumed = false; + public boolean pairingIsRunning = false; + @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + super.onCreateView(inflater, container, savedInstanceState); + // infalte the content view of this activity. View contentView = inflater.inflate(R.layout.activity_obd_selection_fragment, container, false); @@ -130,9 +135,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle // Setup the listviews, its adapters, and its onClick listener. setupListViews(); - // Setup the paired devices. - updatePairedDevicesList(); - // Check the GPS and Location permissions // before Starting the discovery of bluetooth devices. updateContentView(); @@ -241,7 +243,8 @@ public void requestGps() { // Check whether the GPS is turned or not if (manager.isProviderEnabled(GPS_PROVIDER)) { // if the GPS is also enabled, start discovery - startBluetoothDiscovery(); + if(isResumed) + startBluetoothDiscovery(); } else { // Request to turn GPS on buildAlertMessageNoGps(); @@ -274,6 +277,7 @@ private void buildAlertMessageNoGps(){ @Override public void onResume() { super.onResume(); + isResumed = true; checkGpsAfterDialog(); } @@ -282,7 +286,7 @@ public void checkGpsAfterDialog(){ final LocationManager manager = (LocationManager) this.getContext().getSystemService(Context.LOCATION_SERVICE); // Check whether the GPS is turned or not - if (EasyPermissions.hasPermissions(getContext(), perms) && manager.isProviderEnabled(GPS_PROVIDER)) { + if (EasyPermissions.hasPermissions(getContext(), perms) && manager.isProviderEnabled(GPS_PROVIDER) && !pairingIsRunning) { startBluetoothDiscovery(); } } @@ -343,7 +347,7 @@ public void onComplete() { mProgressBar.setVisibility(View.GONE); mRescanImageView.setVisibility(View.VISIBLE); - showSnackbar("Discovery Finished!"); + showSnackbar(getString(R.string.obd_selection_discovery_finished)); if(mNewDevicesArrayAdapter.isEmpty()){ mNewDevicesInfoTextView.setVisibility(View.VISIBLE); } @@ -500,6 +504,7 @@ private void pairDevice(BluetoothDevice device, final View view) { @Override public void onPairingStarted(BluetoothDevice device) { + pairingIsRunning = true; showSnackbar(getString(R.string.obd_selection_pairing_started)); if (text != null) { text.setText(device.getName() + " (Pairing started...)"); @@ -508,6 +513,7 @@ public void onPairingStarted(BluetoothDevice device) { @Override public void onPairingError(BluetoothDevice device) { + pairingIsRunning = false; if (getActivity() != null) { Toast.makeText(getActivity(), R.string.obd_selection_pairing_error, @@ -519,14 +525,23 @@ public void onPairingError(BluetoothDevice device) { @Override public void onDevicePaired(BluetoothDevice device) { + pairingIsRunning = false; // Device is paired. Add it to the array adapter for paired devices and // remove it from the adapter for new devices. - showSnackbar(getString(R.string.obd_selection_pairing_success_template, + showSnackbar(String.format( + getString(R.string.obd_selection_pairing_success_template), device.getName())); + // TODO Issue: Unstable bluetooth connect workflow #844 + // --> under the in the issue explained circumstances the getString()-methode + // fails at this point because the fragment has no context + mNewDevicesArrayAdapter.remove(device); mPairedDevicesAdapter.add(device); + mPairedDevicesInfoTextView.setVisibility(View.GONE); - //mPairedDevicesTextView.setVisibility(View.VISIBLE); + if (mNewDevicesArrayAdapter.isEmpty()) { + mNewDevicesInfoTextView.setVisibility(View.VISIBLE); + } // Post an event to all registered handlers. mBus.post(new BluetoothPairingChangedEvent(device, true));