Properly handle registration timeout in BLE.
Bug:25384098 Change-Id: I7877f6368c4982fcd91e68810c4da0ab7b5fc6b7
This commit is contained in:
committed by
Andre Eisenbach
parent
b72bf5cfdd
commit
02bc008607
@@ -237,8 +237,8 @@ public final class BluetoothLeAdvertiser {
|
||||
private final IBluetoothGatt mBluetoothGatt;
|
||||
|
||||
// mClientIf 0: not registered
|
||||
// -1: scan stopped
|
||||
// >0: registered and scan started
|
||||
// -1: advertise stopped or registration timeout
|
||||
// >0: registered and advertising started
|
||||
private int mClientIf;
|
||||
private boolean mIsAdvertising = false;
|
||||
|
||||
@@ -268,6 +268,10 @@ public final class BluetoothLeAdvertiser {
|
||||
if (mClientIf > 0 && mIsAdvertising) {
|
||||
mLeAdvertisers.put(mAdvertiseCallback, this);
|
||||
} else if (mClientIf <= 0) {
|
||||
|
||||
// Registration timeout, reset mClientIf to -1 so no subsequent operations can
|
||||
// proceed.
|
||||
if (mClientIf == 0) mClientIf = -1;
|
||||
// Post internal error if registration failed.
|
||||
postStartFailure(mAdvertiseCallback,
|
||||
AdvertiseCallback.ADVERTISE_FAILED_INTERNAL_ERROR);
|
||||
@@ -308,10 +312,15 @@ public final class BluetoothLeAdvertiser {
|
||||
Log.d(TAG, "onClientRegistered() - status=" + status + " clientIf=" + clientIf);
|
||||
synchronized (this) {
|
||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||
mClientIf = clientIf;
|
||||
try {
|
||||
mBluetoothGatt.startMultiAdvertising(mClientIf, mAdvertisement,
|
||||
mScanResponse, mSettings);
|
||||
if (mClientIf == -1) {
|
||||
// Registration succeeds after timeout, unregister client.
|
||||
mBluetoothGatt.unregisterClient(clientIf);
|
||||
} else {
|
||||
mClientIf = clientIf;
|
||||
mBluetoothGatt.startMultiAdvertising(mClientIf, mAdvertisement,
|
||||
mScanResponse, mSettings);
|
||||
}
|
||||
return;
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "failed to start advertising", e);
|
||||
|
||||
@@ -278,7 +278,7 @@ public final class BluetoothLeScanner {
|
||||
private List<List<ResultStorageDescriptor>> mResultStorages;
|
||||
|
||||
// mLeHandle 0: not registered
|
||||
// -1: scan stopped
|
||||
// -1: scan stopped or registration failed
|
||||
// > 0: registered and scan started
|
||||
private int mClientIf;
|
||||
|
||||
@@ -310,6 +310,9 @@ public final class BluetoothLeScanner {
|
||||
if (mClientIf > 0) {
|
||||
mLeScanClients.put(mScanCallback, this);
|
||||
} else {
|
||||
// Registration timed out or got exception, reset clientIf to -1 so no
|
||||
// subsequent operations can proceed.
|
||||
if (mClientIf == 0) mClientIf = -1;
|
||||
postCallbackError(mScanCallback,
|
||||
ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED);
|
||||
}
|
||||
@@ -352,19 +355,19 @@ public final class BluetoothLeScanner {
|
||||
@Override
|
||||
public void onClientRegistered(int status, int clientIf) {
|
||||
Log.d(TAG, "onClientRegistered() - status=" + status +
|
||||
" clientIf=" + clientIf);
|
||||
" clientIf=" + clientIf + " mClientIf=" + mClientIf);
|
||||
synchronized (this) {
|
||||
if (mClientIf == -1) {
|
||||
if (DBG) Log.d(TAG, "onClientRegistered LE scan canceled");
|
||||
}
|
||||
|
||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||
mClientIf = clientIf;
|
||||
try {
|
||||
mBluetoothGatt.startScan(mClientIf, false, mSettings, mFilters,
|
||||
mWorkSource, mResultStorages,
|
||||
ActivityThread.currentOpPackageName());
|
||||
|
||||
if (mClientIf == -1) {
|
||||
// Registration succeeds after timeout, unregister client.
|
||||
mBluetoothGatt.unregisterClient(clientIf);
|
||||
} else {
|
||||
mClientIf = clientIf;
|
||||
mBluetoothGatt.startScan(mClientIf, false, mSettings, mFilters,
|
||||
mWorkSource, mResultStorages,
|
||||
ActivityThread.currentOpPackageName());
|
||||
}
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "fail to start le scan: " + e);
|
||||
mClientIf = -1;
|
||||
|
||||
Reference in New Issue
Block a user