BtLEQueue: update device state on main looper

If the device connection state is updated from two threads simultaneously
(as in, from the main looper and from the thread that handles
BluetoothDevice.connectGatt), a second update may get overridden by the
first update if the broadcasts are handled out-of-order by the
LocalBroadcastManager.

By updating the device state through a handler on the main looper, the
broadcasts are sent in order as they are processed from the looper's
queue.

This may be a potential solve for issue #3524.
This commit is contained in:
MrYoranimo 2024-01-21 01:56:34 +01:00
parent 7955bdfb6f
commit 8a7de15841
1 changed files with 12 additions and 6 deletions

View File

@ -227,7 +227,12 @@ public final class BtLEQueue {
} }
protected boolean isConnected() { protected boolean isConnected() {
return mGbDevice.isConnected(); if (mGbDevice.isConnected()) {
return true;
}
LOG.debug("isConnected(): current state = {}", mGbDevice.getState());
return false;
} }
/** /**
@ -285,11 +290,12 @@ public final class BtLEQueue {
return result; return result;
} }
private void setDeviceConnectionState(State newState) { private void setDeviceConnectionState(final State newState) {
LOG.debug("new device connection state: " + newState); new Handler(Looper.getMainLooper()).post(() -> {
LOG.debug("new device connection state: " + newState);
mGbDevice.setState(newState); mGbDevice.setState(newState);
mGbDevice.sendDeviceUpdateIntent(mContext, GBDevice.DeviceUpdateSubject.CONNECTION_STATE); mGbDevice.sendDeviceUpdateIntent(mContext, GBDevice.DeviceUpdateSubject.CONNECTION_STATE);
});
} }
public void disconnect() { public void disconnect() {