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