Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset connection flags when Bluetooth goes off #428

Merged
merged 1 commit into from
Sep 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ public void onReceive(final Context context, final Intent intent) {
operationInProgress = true;
taskQueue.clear();
initQueue = null;
// Reset the state, so that the callbacks below see the correct state.
final boolean wasConnected = connected;
connected = false;
ready = false;
connectionState = BluetoothGatt.STATE_DISCONNECTED;

final BluetoothDevice device = bluetoothDevice;
if (device != null) {
Expand All @@ -285,6 +290,7 @@ public void onReceive(final Context context, final Intent intent) {
operationInProgress = false;
// This will call close()
if (device != null) {
connected = wasConnected;
notifyDeviceDisconnected(device, ConnectionObserver.REASON_TERMINATE_LOCAL_HOST);
}
} else {
Expand Down Expand Up @@ -1774,6 +1780,7 @@ protected void onDeviceDisconnected() {
private void notifyDeviceDisconnected(@NonNull final BluetoothDevice device, final int status) {
final boolean wasConnected = connected;
connected = false;
ready = false;
servicesDiscovered = false;
serviceDiscoveryRequested = false;
deviceNotSupported = false;
Expand All @@ -1792,11 +1799,11 @@ private void notifyDeviceDisconnected(@NonNull final BluetoothDevice device, fin
// If Remove Bond was called, the broadcast may be called AFTER the device has disconnected.
// In that case, we can't call close() here, as that would unregister the broadcast
// receiver. Instead, close() will be called from the receiver.
final Request request = this.request;
if (request == null || request.type != Request.Type.REMOVE_BOND)
close();
postCallback(c -> c.onDeviceDisconnected(device));
postConnectionStateChange(o -> o.onDeviceDisconnected(device, status));
final Request request = this.request;
if (request != null && request.type == Request.Type.DISCONNECT) {
request.notifySuccess(device);
this.request = null;
Expand Down