Skip to content

Commit

Permalink
Merge pull request #847 from FabianWessel/fix/unstableBluetoothConnec…
Browse files Browse the repository at this point in the history
…tWorkflow

Fix/unstableBluetoothConnectWorkflow
  • Loading branch information
SebaDro authored Nov 2, 2021
2 parents 7685dcb + d84f46f commit 308f3db
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
-->
<resources>
<string name="bluetooth_pairing_preference_toolbar_title">Bluetooth Pairing</string>
<string name="bluetooth_pairing_preference_toolbar_title">Bluetooth Kopplung</string>
<string name="select_bluetooth_preference_info_no_device_found">Kein Bluetoothgerät gefunden.</string>
<string name="bluetooth_pairing_preference_info_searching_devices">Suche nach anderen Bluetoothgeräten...</string>
<string name="bluetooth_pairing_preference_info_devices_found">%s Bluetooth devices found.</string>
<string name="bluetooth_pairing_preference_info_device_found">1 Bluetooth device found.</string>
<string name="bluetooth_pairing_preference_dialog_remove_pairing">Delete</string>
<string name="bluetooth_pairing_preference_dialog_remove_pairing">Löschen</string>

<string name="pref_bt_discovery_interval_title">Set Discovery Interval</string>
<string name="pref_bt_discovery_interval_explanation">Select the interval (in minutes + seconds) at which the application re-initiates the discovery process.</string>
Expand Down
1 change: 1 addition & 0 deletions org.envirocar.app/res/values-de/strings_obd_selection.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<string name="obd_selection_bluetooth_disabled">Bluetooth ist deaktiviert.</string>
<string name="obd_selection_bluetooth_disabled_snackbar">Bluetooth ist deaktiviert. Bitte aktivieren Sie Bluetooth, bevor Sie nach anderen Geräten suchen können.</string>
<string name="obd_selection_discovery_started">Suche gestartet!</string>
<string name="obd_selection_discovery_finished">Suche beendet!</string>
<string name="obd_selection_is_selected_template">%s ausgewählt.</string>

<string name="obd_selection_no_paired_devices">Keine gekoppelten Bluetooth-Geräte.</string>
Expand Down
1 change: 1 addition & 0 deletions org.envirocar.app/res/values/strings_obd_selection.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<string name="obd_selection_bluetooth_disabled">Bluetooth is disabled.</string>
<string name="obd_selection_bluetooth_disabled_snackbar">Bluetooth is disabled. Please enable Bluetooth before you can discover other devices.</string>
<string name="obd_selection_discovery_started">Bluetooth discovery started.</string>
<string name="obd_selection_discovery_finished">Bluetooth discovery finished.</string>
<string name="obd_selection_is_selected_template">%s selected.</string>

<string name="obd_selection_no_paired_devices">No paired Bluetooth devices</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -274,6 +277,7 @@ private void buildAlertMessageNoGps(){
@Override
public void onResume() {
super.onResume();
isResumed = true;
checkGpsAfterDialog();
}

Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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...)");
Expand All @@ -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,
Expand All @@ -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));
Expand Down

0 comments on commit 308f3db

Please sign in to comment.