Skip to content

Commit

Permalink
Merge pull request #530 from NordicSemiconductor/bugfix/npe
Browse files Browse the repository at this point in the history
Preventing NPE
  • Loading branch information
philips77 authored Oct 18, 2023
2 parents 63ffd5d + f1d041d commit 7a03e30
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2119,6 +2119,10 @@ public void onConnectionStateChange(@NonNull final BluetoothGatt gatt,
}
} else {
if (newState == BluetoothProfile.STATE_DISCONNECTED) {
final Request r = BleManagerHandler.this.request;
final ConnectRequest cr = connectRequest;
final AwaitingRequest<?> ar = awaitingRequest;

final long now = SystemClock.elapsedRealtime();
final boolean canTimeout = connectionTime > 0;
final boolean timeout = canTimeout && now > connectionTime + CONNECTION_TIMEOUT_THRESHOLD;
Expand All @@ -2130,12 +2134,12 @@ public void onConnectionStateChange(@NonNull final BluetoothGatt gatt,

// In case of a connection error, retry if required.
if (status != BluetoothGatt.GATT_SUCCESS && canTimeout && !timeout
&& connectRequest != null && connectRequest.canRetry()) {
final int delay = connectRequest.getRetryDelay();
&& cr != null && cr.canRetry()) {
final int delay = cr.getRetryDelay();
if (delay > 0)
log(Log.DEBUG, () -> "wait(" + delay + ")");
postDelayed(() -> {
internalConnect(gatt.getDevice(), connectRequest);
internalConnect(gatt.getDevice(), cr);
// If ConnectRequest was cancelled during wait(200) in internalConnect(),
// the gatt will be null, but the state is still CONNECTING.
// We need to notify observers about cancellation.
Expand All @@ -2150,10 +2154,10 @@ public void onConnectionStateChange(@NonNull final BluetoothGatt gatt,
return;
}

if (connectRequest != null && connectRequest.shouldAutoConnect() && initialConnection
if (cr != null && cr.shouldAutoConnect() && initialConnection
&& gatt.getDevice().getBondState() == BluetoothDevice.BOND_BONDED) {
log(Log.DEBUG, () -> "autoConnect = false called failed; retrying with autoConnect = true");
post(() -> internalConnect(gatt.getDevice(), connectRequest));
post(() -> internalConnect(gatt.getDevice(), cr));
return;
}

Expand All @@ -2172,32 +2176,32 @@ public void onConnectionStateChange(@NonNull final BluetoothGatt gatt,
notifyDeviceDisconnected(gatt.getDevice(), ConnectionObserver.REASON_TIMEOUT);
} else if (notSupported) {
notifyDeviceDisconnected(gatt.getDevice(), ConnectionObserver.REASON_NOT_SUPPORTED);
} else if (request != null && request.type == Request.Type.DISCONNECT) {
} else if (r != null && r.type == Request.Type.DISCONNECT) {
notifyDeviceDisconnected(gatt.getDevice(), ConnectionObserver.REASON_SUCCESS);
} else {
// Note, that even if the status is SUCCESS, the reported reason won't be success.
notifyDeviceDisconnected(gatt.getDevice(), mapDisconnectStatusToReason(status));
}

// Signal the current request, if any.
if (request != null) {
if (request.type != Request.Type.DISCONNECT && request.type != Request.Type.REMOVE_BOND) {
if (r != null) {
if (r.type != Request.Type.DISCONNECT && r.type != Request.Type.REMOVE_BOND) {
// The CONNECT request is notified below.
// The DISCONNECT request is notified below in
// notifyDeviceDisconnected(BluetoothDevice).
// The REMOVE_BOND request will be notified when the bond state changes
// to BOND_NONE in the broadcast received on the top of this file.
request.notifyFail(gatt.getDevice(),
r.notifyFail(gatt.getDevice(),
status == BluetoothGatt.GATT_SUCCESS ?
FailCallback.REASON_DEVICE_DISCONNECTED : status);
request = null;
}
}
if (awaitingRequest != null) {
awaitingRequest.notifyFail(gatt.getDevice(), FailCallback.REASON_DEVICE_DISCONNECTED);
if (ar != null) {
ar.notifyFail(gatt.getDevice(), FailCallback.REASON_DEVICE_DISCONNECTED);
awaitingRequest = null;
}
if (connectRequest != null) {
if (cr != null) {
int reason;
if (notSupported)
reason = FailCallback.REASON_DEVICE_NOT_SUPPORTED;
Expand All @@ -2207,7 +2211,7 @@ else if (status == GattError.GATT_ERROR && timeout)
reason = FailCallback.REASON_TIMEOUT;
else
reason = status;
connectRequest.notifyFail(gatt.getDevice(), reason);
cr.notifyFail(gatt.getDevice(), reason);
connectRequest = null;
}

Expand Down

0 comments on commit 7a03e30

Please sign in to comment.