Reduced the impact of "synchronized" statements

* Removed "synchronized" statements that are not needed
* Replaced "synchronized" statements with Read/Write lock as
 appropriate. The lock protects the access to and the setting of
 BluetoothAdapter.mService and BluetoothManagerService.mBluetooth and
 associated state.

Bug: 28734075
Bug: 28799467
Change-Id: I8f8281c505f0a1ae0add1e14a3caba1f5b2a98e4
This commit is contained in:
Pavlin Radoslavov
2016-05-22 22:16:41 -07:00
parent 9c252700f6
commit eb50a39e98
2 changed files with 389 additions and 249 deletions

View File

@@ -57,6 +57,7 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/** /**
* Represents the local device Bluetooth adapter. The {@link BluetoothAdapter} * Represents the local device Bluetooth adapter. The {@link BluetoothAdapter}
@@ -483,6 +484,8 @@ public final class BluetoothAdapter {
private final IBluetoothManager mManagerService; private final IBluetoothManager mManagerService;
private IBluetooth mService; private IBluetooth mService;
private final ReentrantReadWriteLock mServiceLock =
new ReentrantReadWriteLock();
private final Object mLock = new Object(); private final Object mLock = new Object();
private final Map<LeScanCallback, ScanCallback> mLeScanClients; private final Map<LeScanCallback, ScanCallback> mLeScanClients;
@@ -517,8 +520,13 @@ public final class BluetoothAdapter {
throw new IllegalArgumentException("bluetooth manager service is null"); throw new IllegalArgumentException("bluetooth manager service is null");
} }
try { try {
mServiceLock.writeLock().lock();
mService = managerService.registerAdapter(mManagerCallback); mService = managerService.registerAdapter(mManagerCallback);
} catch (RemoteException e) {Log.e(TAG, "", e);} } catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.writeLock().unlock();
}
mManagerService = managerService; mManagerService = managerService;
mLeScanClients = new HashMap<LeScanCallback, ScanCallback>(); mLeScanClients = new HashMap<LeScanCallback, ScanCallback>();
mToken = new Binder(); mToken = new Binder();
@@ -605,10 +613,14 @@ public final class BluetoothAdapter {
@RequiresPermission(Manifest.permission.BLUETOOTH) @RequiresPermission(Manifest.permission.BLUETOOTH)
public boolean isEnabled() { public boolean isEnabled() {
try { try {
synchronized(mManagerCallback) { mServiceLock.readLock().lock();
if (mService != null) return mService.isEnabled(); if (mService != null) return mService.isEnabled();
} catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
} }
} catch (RemoteException e) {Log.e(TAG, "", e);}
return false; return false;
} }
@@ -639,12 +651,12 @@ public final class BluetoothAdapter {
* or OFF if BT is in BLE_ON state * or OFF if BT is in BLE_ON state
*/ */
private void notifyUserAction(boolean enable) { private void notifyUserAction(boolean enable) {
try {
mServiceLock.readLock().lock();
if (mService == null) { if (mService == null) {
Log.e(TAG, "mService is null"); Log.e(TAG, "mService is null");
return; return;
} }
try {
if (enable) { if (enable) {
mService.onLeServiceUp(); //NA:TODO implementation pending mService.onLeServiceUp(); //NA:TODO implementation pending
} else { } else {
@@ -652,6 +664,8 @@ public final class BluetoothAdapter {
} }
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
} }
} }
@@ -783,27 +797,29 @@ public final class BluetoothAdapter {
@RequiresPermission(Manifest.permission.BLUETOOTH) @RequiresPermission(Manifest.permission.BLUETOOTH)
@AdapterState @AdapterState
public int getState() { public int getState() {
int state = BluetoothAdapter.STATE_OFF;
try { try {
synchronized(mManagerCallback) { mServiceLock.readLock().lock();
if (mService != null) if (mService != null) {
{ state = mService.getState();
int state= mService.getState(); }
if (VDBG) Log.d(TAG, "" + hashCode() + ": getState(). Returning " + state); } catch (RemoteException e) {
//consider all internal states as OFF Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
}
// Consider all internal states as OFF
if (state == BluetoothAdapter.STATE_BLE_ON if (state == BluetoothAdapter.STATE_BLE_ON
|| state == BluetoothAdapter.STATE_BLE_TURNING_ON || state == BluetoothAdapter.STATE_BLE_TURNING_ON
|| state == BluetoothAdapter.STATE_BLE_TURNING_OFF) { || state == BluetoothAdapter.STATE_BLE_TURNING_OFF) {
if (VDBG) Log.d(TAG, "Consider internal state as OFF"); if (VDBG) Log.d(TAG, "Consider internal state as OFF");
state = BluetoothAdapter.STATE_OFF; state = BluetoothAdapter.STATE_OFF;
} }
if (VDBG) Log.d(TAG, "" + hashCode() + ": getState(). Returning " + state);
return state; return state;
} }
// TODO(BT) there might be a small gap during STATE_TURNING_ON that
// mService is null, handle that case
}
} catch (RemoteException e) {Log.e(TAG, "", e);}
return STATE_OFF;
}
/** /**
* Get the current state of the local Bluetooth adapter * Get the current state of the local Bluetooth adapter
@@ -825,19 +841,21 @@ public final class BluetoothAdapter {
@RequiresPermission(Manifest.permission.BLUETOOTH) @RequiresPermission(Manifest.permission.BLUETOOTH)
@AdapterState @AdapterState
public int getLeState() { public int getLeState() {
int state = BluetoothAdapter.STATE_OFF;
try { try {
synchronized(mManagerCallback) { mServiceLock.readLock().lock();
if (mService != null) if (mService != null) {
{ state = mService.getState();
int state= mService.getState();
if (VDBG) Log.d(TAG,"getLeState() returning " + state);
return state;
}
} }
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
} }
return BluetoothAdapter.STATE_OFF;
if (VDBG) Log.d(TAG,"getLeState() returning " + state);
return state;
} }
boolean getLeAccess() { boolean getLeAccess() {
@@ -879,16 +897,21 @@ public final class BluetoothAdapter {
*/ */
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public boolean enable() { public boolean enable() {
int state = STATE_OFF; int state = BluetoothAdapter.STATE_OFF;
if (isEnabled() == true){ if (isEnabled() == true) {
if (DBG) Log.d(TAG, "enable(): BT is already enabled..!"); if (DBG) Log.d(TAG, "enable(): BT is already enabled..!");
return true; return true;
} }
//Use service interface to get the exact state // Use service interface to get the exact state
if (mService != null) {
try { try {
mServiceLock.readLock().lock();
if (mService != null) {
state = mService.getState(); state = mService.getState();
} catch (RemoteException e) {Log.e(TAG, "", e);} }
} catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
} }
if (state == BluetoothAdapter.STATE_BLE_ON) { if (state == BluetoothAdapter.STATE_BLE_ON) {
@@ -993,10 +1016,13 @@ public final class BluetoothAdapter {
*/ */
public boolean configHciSnoopLog(boolean enable) { public boolean configHciSnoopLog(boolean enable) {
try { try {
synchronized(mManagerCallback) { mServiceLock.readLock().lock();
if (mService != null) return mService.configHciSnoopLog(enable); if (mService != null) return mService.configHciSnoopLog(enable);
} catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
} }
} catch (RemoteException e) {Log.e(TAG, "", e);}
return false; return false;
} }
@@ -1012,12 +1038,16 @@ public final class BluetoothAdapter {
*/ */
public boolean factoryReset() { public boolean factoryReset() {
try { try {
mServiceLock.readLock().lock();
if (mService != null) { if (mService != null) {
return mService.factoryReset(); return mService.factoryReset();
} else {
SystemProperties.set("persist.bluetooth.factoryreset", "true");
} }
} catch (RemoteException e) {Log.e(TAG, "", e);} SystemProperties.set("persist.bluetooth.factoryreset", "true");
} catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
}
return false; return false;
} }
@@ -1032,10 +1062,13 @@ public final class BluetoothAdapter {
public ParcelUuid[] getUuids() { public ParcelUuid[] getUuids() {
if (getState() != STATE_ON) return null; if (getState() != STATE_ON) return null;
try { try {
synchronized(mManagerCallback) { mServiceLock.readLock().lock();
if (mService != null) return mService.getUuids(); if (mService != null) return mService.getUuids();
} catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
} }
} catch (RemoteException e) {Log.e(TAG, "", e);}
return null; return null;
} }
@@ -1058,10 +1091,13 @@ public final class BluetoothAdapter {
public boolean setName(String name) { public boolean setName(String name) {
if (getState() != STATE_ON) return false; if (getState() != STATE_ON) return false;
try { try {
synchronized(mManagerCallback) { mServiceLock.readLock().lock();
if (mService != null) return mService.setName(name); if (mService != null) return mService.setName(name);
} catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
} }
} catch (RemoteException e) {Log.e(TAG, "", e);}
return false; return false;
} }
@@ -1086,10 +1122,13 @@ public final class BluetoothAdapter {
public int getScanMode() { public int getScanMode() {
if (getState() != STATE_ON) return SCAN_MODE_NONE; if (getState() != STATE_ON) return SCAN_MODE_NONE;
try { try {
synchronized(mManagerCallback) { mServiceLock.readLock().lock();
if (mService != null) return mService.getScanMode(); if (mService != null) return mService.getScanMode();
} catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
} }
} catch (RemoteException e) {Log.e(TAG, "", e);}
return SCAN_MODE_NONE; return SCAN_MODE_NONE;
} }
@@ -1124,10 +1163,13 @@ public final class BluetoothAdapter {
public boolean setScanMode(@ScanMode int mode, int duration) { public boolean setScanMode(@ScanMode int mode, int duration) {
if (getState() != STATE_ON) return false; if (getState() != STATE_ON) return false;
try { try {
synchronized(mManagerCallback) { mServiceLock.readLock().lock();
if (mService != null) return mService.setScanMode(mode, duration); if (mService != null) return mService.setScanMode(mode, duration);
} catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
} }
} catch (RemoteException e) {Log.e(TAG, "", e);}
return false; return false;
} }
@@ -1142,10 +1184,13 @@ public final class BluetoothAdapter {
public int getDiscoverableTimeout() { public int getDiscoverableTimeout() {
if (getState() != STATE_ON) return -1; if (getState() != STATE_ON) return -1;
try { try {
synchronized(mManagerCallback) { mServiceLock.readLock().lock();
if (mService != null) return mService.getDiscoverableTimeout(); if (mService != null) return mService.getDiscoverableTimeout();
} catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
} }
} catch (RemoteException e) {Log.e(TAG, "", e);}
return -1; return -1;
} }
@@ -1153,10 +1198,13 @@ public final class BluetoothAdapter {
public void setDiscoverableTimeout(int timeout) { public void setDiscoverableTimeout(int timeout) {
if (getState() != STATE_ON) return; if (getState() != STATE_ON) return;
try { try {
synchronized(mManagerCallback) { mServiceLock.readLock().lock();
if (mService != null) mService.setDiscoverableTimeout(timeout); if (mService != null) mService.setDiscoverableTimeout(timeout);
} catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
} }
} catch (RemoteException e) {Log.e(TAG, "", e);}
} }
/** /**
@@ -1193,10 +1241,13 @@ public final class BluetoothAdapter {
public boolean startDiscovery() { public boolean startDiscovery() {
if (getState() != STATE_ON) return false; if (getState() != STATE_ON) return false;
try { try {
synchronized(mManagerCallback) { mServiceLock.readLock().lock();
if (mService != null) return mService.startDiscovery(); if (mService != null) return mService.startDiscovery();
} catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
} }
} catch (RemoteException e) {Log.e(TAG, "", e);}
return false; return false;
} }
@@ -1221,10 +1272,13 @@ public final class BluetoothAdapter {
public boolean cancelDiscovery() { public boolean cancelDiscovery() {
if (getState() != STATE_ON) return false; if (getState() != STATE_ON) return false;
try { try {
synchronized(mManagerCallback) { mServiceLock.readLock().lock();
if (mService != null) return mService.cancelDiscovery(); if (mService != null) return mService.cancelDiscovery();
} catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
} }
} catch (RemoteException e) {Log.e(TAG, "", e);}
return false; return false;
} }
@@ -1251,10 +1305,13 @@ public final class BluetoothAdapter {
public boolean isDiscovering() { public boolean isDiscovering() {
if (getState() != STATE_ON) return false; if (getState() != STATE_ON) return false;
try { try {
synchronized(mManagerCallback) { mServiceLock.readLock().lock();
if (mService != null ) return mService.isDiscovering(); if (mService != null) return mService.isDiscovering();
} catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
} }
} catch (RemoteException e) {Log.e(TAG, "", e);}
return false; return false;
} }
@@ -1266,9 +1323,12 @@ public final class BluetoothAdapter {
public boolean isMultipleAdvertisementSupported() { public boolean isMultipleAdvertisementSupported() {
if (getState() != STATE_ON) return false; if (getState() != STATE_ON) return false;
try { try {
return mService.isMultiAdvertisementSupported(); mServiceLock.readLock().lock();
if (mService != null) return mService.isMultiAdvertisementSupported();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "failed to get isMultipleAdvertisementSupported, error: ", e); Log.e(TAG, "failed to get isMultipleAdvertisementSupported, error: ", e);
} finally {
mServiceLock.readLock().unlock();
} }
return false; return false;
} }
@@ -1301,9 +1361,12 @@ public final class BluetoothAdapter {
public boolean isPeripheralModeSupported() { public boolean isPeripheralModeSupported() {
if (getState() != STATE_ON) return false; if (getState() != STATE_ON) return false;
try { try {
return mService.isPeripheralModeSupported(); mServiceLock.readLock().lock();
if (mService != null) return mService.isPeripheralModeSupported();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "failed to get peripheral mode capability: ", e); Log.e(TAG, "failed to get peripheral mode capability: ", e);
} finally {
mServiceLock.readLock().unlock();
} }
return false; return false;
} }
@@ -1316,9 +1379,12 @@ public final class BluetoothAdapter {
public boolean isOffloadedFilteringSupported() { public boolean isOffloadedFilteringSupported() {
if (!getLeAccess()) return false; if (!getLeAccess()) return false;
try { try {
return mService.isOffloadedFilteringSupported(); mServiceLock.readLock().lock();
if (mService != null) return mService.isOffloadedFilteringSupported();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "failed to get isOffloadedFilteringSupported, error: ", e); Log.e(TAG, "failed to get isOffloadedFilteringSupported, error: ", e);
} finally {
mServiceLock.readLock().unlock();
} }
return false; return false;
} }
@@ -1331,9 +1397,12 @@ public final class BluetoothAdapter {
public boolean isOffloadedScanBatchingSupported() { public boolean isOffloadedScanBatchingSupported() {
if (!getLeAccess()) return false; if (!getLeAccess()) return false;
try { try {
return mService.isOffloadedScanBatchingSupported(); mServiceLock.readLock().lock();
if (mService != null) return mService.isOffloadedScanBatchingSupported();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "failed to get isOffloadedScanBatchingSupported, error: ", e); Log.e(TAG, "failed to get isOffloadedScanBatchingSupported, error: ", e);
} finally {
mServiceLock.readLock().unlock();
} }
return false; return false;
} }
@@ -1399,15 +1468,15 @@ public final class BluetoothAdapter {
*/ */
public void requestControllerActivityEnergyInfo(ResultReceiver result) { public void requestControllerActivityEnergyInfo(ResultReceiver result) {
try { try {
synchronized(mManagerCallback) { mServiceLock.readLock().lock();
if (mService != null) { if (mService != null) {
mService.requestActivityInfo(result); mService.requestActivityInfo(result);
result = null; result = null;
} }
}
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "getControllerActivityEnergyInfoCallback: " + e); Log.e(TAG, "getControllerActivityEnergyInfoCallback: " + e);
} finally { } finally {
mServiceLock.readLock().unlock();
if (result != null) { if (result != null) {
// Only send an immediate result if we failed. // Only send an immediate result if we failed.
result.send(0, null); result.send(0, null);
@@ -1432,11 +1501,14 @@ public final class BluetoothAdapter {
return toDeviceSet(new BluetoothDevice[0]); return toDeviceSet(new BluetoothDevice[0]);
} }
try { try {
synchronized(mManagerCallback) { mServiceLock.readLock().lock();
if (mService != null) return toDeviceSet(mService.getBondedDevices()); if (mService != null) return toDeviceSet(mService.getBondedDevices());
}
return toDeviceSet(new BluetoothDevice[0]); return toDeviceSet(new BluetoothDevice[0]);
} catch (RemoteException e) {Log.e(TAG, "", e);} } catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
}
return null; return null;
} }
@@ -1456,10 +1528,13 @@ public final class BluetoothAdapter {
public int getConnectionState() { public int getConnectionState() {
if (getState() != STATE_ON) return BluetoothAdapter.STATE_DISCONNECTED; if (getState() != STATE_ON) return BluetoothAdapter.STATE_DISCONNECTED;
try { try {
synchronized(mManagerCallback) { mServiceLock.readLock().lock();
if (mService != null) return mService.getAdapterConnectionState(); if (mService != null) return mService.getAdapterConnectionState();
} catch (RemoteException e) {
Log.e(TAG, "getConnectionState:", e);
} finally {
mServiceLock.readLock().unlock();
} }
} catch (RemoteException e) {Log.e(TAG, "getConnectionState:", e);}
return BluetoothAdapter.STATE_DISCONNECTED; return BluetoothAdapter.STATE_DISCONNECTED;
} }
@@ -1482,11 +1557,12 @@ public final class BluetoothAdapter {
public int getProfileConnectionState(int profile) { public int getProfileConnectionState(int profile) {
if (getState() != STATE_ON) return BluetoothProfile.STATE_DISCONNECTED; if (getState() != STATE_ON) return BluetoothProfile.STATE_DISCONNECTED;
try { try {
synchronized(mManagerCallback) { mServiceLock.readLock().lock();
if (mService != null) return mService.getProfileConnectionState(profile); if (mService != null) return mService.getProfileConnectionState(profile);
}
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "getProfileConnectionState:", e); Log.e(TAG, "getProfileConnectionState:", e);
} finally {
mServiceLock.readLock().unlock();
} }
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
@@ -1790,7 +1866,9 @@ public final class BluetoothAdapter {
byte[] hash; byte[] hash;
byte[] randomizer; byte[] randomizer;
byte[] ret = mService.readOutOfBandData(); byte[] ret = null;
mServiceLock.readLock().lock();
if (mService != null) mService.readOutOfBandData();
if (ret == null || ret.length != 32) return null; if (ret == null || ret.length != 32) return null;
@@ -1803,7 +1881,12 @@ public final class BluetoothAdapter {
} }
return new Pair<byte[], byte[]>(hash, randomizer); return new Pair<byte[], byte[]>(hash, randomizer);
} catch (RemoteException e) {Log.e(TAG, "", e);}*/ } catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
}
*/
return null; return null;
} }
@@ -1939,17 +2022,21 @@ public final class BluetoothAdapter {
new IBluetoothManagerCallback.Stub() { new IBluetoothManagerCallback.Stub() {
public void onBluetoothServiceUp(IBluetooth bluetoothService) { public void onBluetoothServiceUp(IBluetooth bluetoothService) {
if (VDBG) Log.d(TAG, "onBluetoothServiceUp: " + bluetoothService); if (VDBG) Log.d(TAG, "onBluetoothServiceUp: " + bluetoothService);
synchronized (mManagerCallback) {
mServiceLock.writeLock().lock();
mService = bluetoothService; mService = bluetoothService;
mServiceLock.writeLock().unlock();
synchronized (mProxyServiceStateCallbacks) { synchronized (mProxyServiceStateCallbacks) {
for (IBluetoothManagerCallback cb : mProxyServiceStateCallbacks ){ for (IBluetoothManagerCallback cb : mProxyServiceStateCallbacks ) {
try { try {
if (cb != null) { if (cb != null) {
cb.onBluetoothServiceUp(bluetoothService); cb.onBluetoothServiceUp(bluetoothService);
} else { } else {
Log.d(TAG, "onBluetoothServiceUp: cb is null!!!"); Log.d(TAG, "onBluetoothServiceUp: cb is null!!!");
} }
} catch (Exception e) { Log.e(TAG,"",e);} } catch (Exception e) {
Log.e(TAG,"",e);
} }
} }
} }
@@ -1957,11 +2044,14 @@ public final class BluetoothAdapter {
public void onBluetoothServiceDown() { public void onBluetoothServiceDown() {
if (VDBG) Log.d(TAG, "onBluetoothServiceDown: " + mService); if (VDBG) Log.d(TAG, "onBluetoothServiceDown: " + mService);
synchronized (mManagerCallback) {
mServiceLock.writeLock().lock();
mService = null; mService = null;
if (mLeScanClients != null) mLeScanClients.clear(); if (mLeScanClients != null) mLeScanClients.clear();
if (sBluetoothLeAdvertiser != null) sBluetoothLeAdvertiser.cleanup(); if (sBluetoothLeAdvertiser != null) sBluetoothLeAdvertiser.cleanup();
if (sBluetoothLeScanner != null) sBluetoothLeScanner.cleanup(); if (sBluetoothLeScanner != null) sBluetoothLeScanner.cleanup();
mServiceLock.writeLock().unlock();
synchronized (mProxyServiceStateCallbacks) { synchronized (mProxyServiceStateCallbacks) {
for (IBluetoothManagerCallback cb : mProxyServiceStateCallbacks ){ for (IBluetoothManagerCallback cb : mProxyServiceStateCallbacks ){
try { try {
@@ -1970,7 +2060,8 @@ public final class BluetoothAdapter {
} else { } else {
Log.d(TAG, "onBluetoothServiceDown: cb is null!!!"); Log.d(TAG, "onBluetoothServiceDown: cb is null!!!");
} }
} catch (Exception e) { Log.e(TAG,"",e);} } catch (Exception e) {
Log.e(TAG,"",e);
} }
} }
} }
@@ -2033,11 +2124,17 @@ public final class BluetoothAdapter {
//TODO(BT) //TODO(BT)
/* /*
try { try {
mServiceLock.readLock().lock();
if (mService != null) {
return mService.changeApplicationBluetoothState(on, new return mService.changeApplicationBluetoothState(on, new
StateChangeCallbackWrapper(callback), new Binder()); StateChangeCallbackWrapper(callback), new Binder());
}
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "changeBluetoothState", e); Log.e(TAG, "changeBluetoothState", e);
}*/ } finally {
mServiceLock.readLock().unlock();
}
*/
return false; return false;
} }

View File

@@ -52,6 +52,7 @@ import android.os.UserManager;
import android.provider.Settings; import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException; import android.provider.Settings.SettingNotFoundException;
import android.util.Slog; import android.util.Slog;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.io.PrintWriter; import java.io.PrintWriter;
@@ -126,6 +127,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
private IBinder mBluetoothBinder; private IBinder mBluetoothBinder;
private IBluetooth mBluetooth; private IBluetooth mBluetooth;
private IBluetoothGatt mBluetoothGatt; private IBluetoothGatt mBluetoothGatt;
private final ReentrantReadWriteLock mBluetoothLock =
new ReentrantReadWriteLock();
private boolean mBinding; private boolean mBinding;
private boolean mUnbinding; private boolean mUnbinding;
// used inside handler thread // used inside handler thread
@@ -190,12 +193,15 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
} }
int st = BluetoothAdapter.STATE_OFF; int st = BluetoothAdapter.STATE_OFF;
if (mBluetooth != null) {
try { try {
mBluetoothLock.readLock().lock();
if (mBluetooth != null) {
st = mBluetooth.getState(); st = mBluetooth.getState();
} catch (RemoteException e) {
Slog.e(TAG,"Unable to call getState", e);
} }
} catch (RemoteException e) {
Slog.e(TAG, "Unable to call getState", e);
} finally {
mBluetoothLock.readLock().unlock();
} }
Slog.d(TAG, "state" + st); Slog.d(TAG, "state" + st);
@@ -208,12 +214,15 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
if (st == BluetoothAdapter.STATE_BLE_ON) { if (st == BluetoothAdapter.STATE_BLE_ON) {
//if state is BLE_ON make sure you trigger disableBLE part //if state is BLE_ON make sure you trigger disableBLE part
try { try {
mBluetoothLock.readLock().lock();
if (mBluetooth != null) { if (mBluetooth != null) {
mBluetooth.onBrEdrDown(); mBluetooth.onBrEdrDown();
mEnableExternal = false; mEnableExternal = false;
} }
} catch(RemoteException e) { } catch (RemoteException e) {
Slog.e(TAG,"Unable to call onBrEdrDown", e); Slog.e(TAG,"Unable to call onBrEdrDown", e);
} finally {
mBluetoothLock.readLock().lock();
} }
} else if (st == BluetoothAdapter.STATE_ON){ } else if (st == BluetoothAdapter.STATE_ON){
// disable without persisting the setting // disable without persisting the setting
@@ -366,10 +375,9 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_ADAPTER); Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_ADAPTER);
msg.obj = callback; msg.obj = callback;
mHandler.sendMessage(msg); mHandler.sendMessage(msg);
synchronized(mConnection) {
return mBluetooth; return mBluetooth;
} }
}
public void unregisterAdapter(IBluetoothManagerCallback callback) { public void unregisterAdapter(IBluetoothManagerCallback callback) {
if (callback == null) { if (callback == null) {
@@ -406,12 +414,13 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
return false; return false;
} }
synchronized(mConnection) {
try { try {
return (mBluetooth != null && mBluetooth.isEnabled()); mBluetoothLock.readLock().lock();
if (mBluetooth != null) return mBluetooth.isEnabled();
} catch (RemoteException e) { } catch (RemoteException e) {
Slog.e(TAG, "isEnabled()", e); Slog.e(TAG, "isEnabled()", e);
} } finally {
mBluetoothLock.readLock().unlock();
} }
return false; return false;
} }
@@ -424,11 +433,14 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
if (mBleAppCount == 0) { if (mBleAppCount == 0) {
if (DBG) Slog.d(TAG, "Disabling LE only mode after application crash"); if (DBG) Slog.d(TAG, "Disabling LE only mode after application crash");
try { try {
mBluetoothLock.readLock().lock();
if (mBluetooth != null) { if (mBluetooth != null) {
mBluetooth.onBrEdrDown(); mBluetooth.onBrEdrDown();
} }
} catch(RemoteException e) { } catch (RemoteException e) {
Slog.e(TAG,"Unable to call onBrEdrDown", e); Slog.e(TAG,"Unable to call onBrEdrDown", e);
} finally {
mBluetoothLock.readLock().unlock();
} }
} }
} }
@@ -456,9 +468,12 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
disableBleScanMode(); disableBleScanMode();
clearBleApps(); clearBleApps();
try { try {
mBluetoothLock.readLock().lock();
if (mBluetooth != null) mBluetooth.onBrEdrDown(); if (mBluetooth != null) mBluetooth.onBrEdrDown();
} catch (RemoteException e) { } catch (RemoteException e) {
Slog.e(TAG, "error when disabling bluetooth", e); Slog.e(TAG, "error when disabling bluetooth", e);
} finally {
mBluetoothLock.readLock().unlock();
} }
} }
} }
@@ -472,12 +487,15 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
// Disable ble scan only mode. // Disable ble scan only mode.
private void disableBleScanMode() { private void disableBleScanMode() {
try { try {
mBluetoothLock.writeLock().lock();
if (mBluetooth != null && (mBluetooth.getState() != BluetoothAdapter.STATE_ON)) { if (mBluetooth != null && (mBluetooth.getState() != BluetoothAdapter.STATE_ON)) {
if (DBG) Slog.d(TAG, "Reseting the mEnable flag for clean disable"); if (DBG) Slog.d(TAG, "Reseting the mEnable flag for clean disable");
mEnable = false; mEnable = false;
} }
} catch (RemoteException e) { } catch (RemoteException e) {
Slog.e(TAG, "getState()", e); Slog.e(TAG, "getState()", e);
} finally {
mBluetoothLock.writeLock().unlock();
} }
} }
@@ -536,7 +554,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
*/ */
private void onBluetoothGattServiceUp() { private void onBluetoothGattServiceUp() {
if (DBG) Slog.d(TAG,"BluetoothGatt Service is Up"); if (DBG) Slog.d(TAG,"BluetoothGatt Service is Up");
try{ try {
mBluetoothLock.readLock().lock();
if (isBleAppPresent() == false && mBluetooth != null if (isBleAppPresent() == false && mBluetooth != null
&& mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON) { && mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON) {
mBluetooth.onLeServiceUp(); mBluetooth.onLeServiceUp();
@@ -546,8 +565,10 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH); persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);
Binder.restoreCallingIdentity(callingIdentity); Binder.restoreCallingIdentity(callingIdentity);
} }
} catch(RemoteException e) { } catch (RemoteException e) {
Slog.e(TAG,"Unable to call onServiceUp", e); Slog.e(TAG,"Unable to call onServiceUp", e);
} finally {
mBluetoothLock.readLock().unlock();
} }
} }
@@ -558,22 +579,25 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
private void sendBrEdrDownCallback() { private void sendBrEdrDownCallback() {
if (DBG) Slog.d(TAG,"Calling sendBrEdrDownCallback callbacks"); if (DBG) Slog.d(TAG,"Calling sendBrEdrDownCallback callbacks");
if(mBluetooth == null) { if (mBluetooth == null) {
Slog.w(TAG, "Bluetooth handle is null"); Slog.w(TAG, "Bluetooth handle is null");
return; return;
} }
if (isBleAppPresent() == false) { if (isBleAppPresent() == false) {
try { try {
mBluetooth.onBrEdrDown(); mBluetoothLock.readLock().lock();
} catch(RemoteException e) { if (mBluetooth != null) mBluetooth.onBrEdrDown();
} catch (RemoteException e) {
Slog.e(TAG, "Call to onBrEdrDown() failed.", e); Slog.e(TAG, "Call to onBrEdrDown() failed.", e);
} finally {
mBluetoothLock.readLock().unlock();
} }
} else { } else {
// Need to stay at BLE ON. Disconnect all Gatt connections // Need to stay at BLE ON. Disconnect all Gatt connections
try{ try {
mBluetoothGatt.unregAll(); mBluetoothGatt.unregAll();
} catch(RemoteException e) { } catch (RemoteException e) {
Slog.e(TAG, "Unable to disconnect all apps.", e); Slog.e(TAG, "Unable to disconnect all apps.", e);
} }
} }
@@ -673,7 +697,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
" mBinding = " + mBinding); " mBinding = " + mBinding);
} }
synchronized (mConnection) { try {
mBluetoothLock.writeLock().lock();
if (mUnbinding) return; if (mUnbinding) return;
mUnbinding = true; mUnbinding = true;
if (mBluetooth != null) { if (mBluetooth != null) {
@@ -695,6 +720,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
mUnbinding=false; mUnbinding=false;
} }
mBluetoothGatt = null; mBluetoothGatt = null;
} finally {
mBluetoothLock.writeLock().unlock();
} }
} }
@@ -1010,14 +1037,13 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
return BluetoothAdapter.DEFAULT_MAC_ADDRESS; return BluetoothAdapter.DEFAULT_MAC_ADDRESS;
} }
synchronized(mConnection) {
if (mBluetooth != null) {
try { try {
return mBluetooth.getAddress(); mBluetoothLock.readLock().lock();
if (mBluetooth != null) return mBluetooth.getAddress();
} catch (RemoteException e) { } catch (RemoteException e) {
Slog.e(TAG, "getAddress(): Unable to retrieve address remotely..Returning cached address",e); Slog.e(TAG, "getAddress(): Unable to retrieve address remotely. Returning cached address", e);
} } finally {
} mBluetoothLock.readLock().unlock();
} }
// mAddress is accessed from outside. // mAddress is accessed from outside.
@@ -1036,15 +1062,15 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
return null; return null;
} }
synchronized(mConnection) {
if (mBluetooth != null) {
try { try {
return mBluetooth.getName(); mBluetoothLock.readLock().lock();
if (mBluetooth != null) return mBluetooth.getName();
} catch (RemoteException e) { } catch (RemoteException e) {
Slog.e(TAG, "getName(): Unable to retrieve name remotely..Returning cached name",e); Slog.e(TAG, "getName(): Unable to retrieve name remotely. Returning cached name", e);
} } finally {
} mBluetoothLock.readLock().unlock();
} }
// mName is accessed from outside. // mName is accessed from outside.
// It alright without a lock. Here, bluetooth is off, no other thread is // It alright without a lock. Here, bluetooth is off, no other thread is
// changing mName // changing mName
@@ -1101,7 +1127,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
switch (msg.what) { switch (msg.what) {
case MESSAGE_GET_NAME_AND_ADDRESS: case MESSAGE_GET_NAME_AND_ADDRESS:
if (DBG) Slog.d(TAG, "MESSAGE_GET_NAME_AND_ADDRESS"); if (DBG) Slog.d(TAG, "MESSAGE_GET_NAME_AND_ADDRESS");
synchronized(mConnection) { try {
mBluetoothLock.writeLock().lock();
if ((mBluetooth == null) && (!mBinding)) { if ((mBluetooth == null) && (!mBinding)) {
if (DBG) Slog.d(TAG, "Binding to service to get name and address"); if (DBG) Slog.d(TAG, "Binding to service to get name and address");
mGetNameAddressOnly = true; mGetNameAddressOnly = true;
@@ -1127,6 +1154,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
} }
mGetNameAddressOnly = false; mGetNameAddressOnly = false;
} }
} finally {
mBluetoothLock.writeLock().unlock();
} }
break; break;
@@ -1209,7 +1238,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
if (DBG) Slog.d(TAG,"MESSAGE_BLUETOOTH_SERVICE_CONNECTED: " + msg.arg1); if (DBG) Slog.d(TAG,"MESSAGE_BLUETOOTH_SERVICE_CONNECTED: " + msg.arg1);
IBinder service = (IBinder) msg.obj; IBinder service = (IBinder) msg.obj;
synchronized(mConnection) { try {
mBluetoothLock.writeLock().lock();
if (msg.arg1 == SERVICE_IBLUETOOTHGATT) { if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
mBluetoothGatt = IBluetoothGatt.Stub.asInterface(service); mBluetoothGatt = IBluetoothGatt.Stub.asInterface(service);
onBluetoothGattServiceUp(); onBluetoothGattServiceUp();
@@ -1264,6 +1294,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
} catch (RemoteException e) { } catch (RemoteException e) {
Slog.e(TAG,"Unable to call enable()",e); Slog.e(TAG,"Unable to call enable()",e);
} }
} finally {
mBluetoothLock.writeLock().unlock();
} }
if (!mEnable) { if (!mEnable) {
@@ -1275,9 +1307,10 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
} }
case MESSAGE_TIMEOUT_BIND: { case MESSAGE_TIMEOUT_BIND: {
Slog.e(TAG, "MESSAGE_TIMEOUT_BIND"); Slog.e(TAG, "MESSAGE_TIMEOUT_BIND");
synchronized(mConnection) { mBluetoothLock.writeLock().lock();
mBinding = false; mBinding = false;
} mBluetoothLock.writeLock().unlock();
break; break;
} }
case MESSAGE_BLUETOOTH_STATE_CHANGE: case MESSAGE_BLUETOOTH_STATE_CHANGE:
@@ -1312,7 +1345,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED:
{ {
Slog.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: " + msg.arg1); Slog.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: " + msg.arg1);
synchronized(mConnection) { try {
mBluetoothLock.writeLock().lock();
if (msg.arg1 == SERVICE_IBLUETOOTH) { if (msg.arg1 == SERVICE_IBLUETOOTH) {
// if service is unbinded already, do nothing and return // if service is unbinded already, do nothing and return
if (mBluetooth == null) break; if (mBluetooth == null) break;
@@ -1324,6 +1358,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
Slog.e(TAG, "Bad msg.arg1: " + msg.arg1); Slog.e(TAG, "Bad msg.arg1: " + msg.arg1);
break; break;
} }
} finally {
mBluetoothLock.writeLock().unlock();
} }
if (mEnable) { if (mEnable) {
@@ -1369,9 +1405,9 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
case MESSAGE_TIMEOUT_UNBIND: case MESSAGE_TIMEOUT_UNBIND:
{ {
Slog.e(TAG, "MESSAGE_TIMEOUT_UNBIND"); Slog.e(TAG, "MESSAGE_TIMEOUT_UNBIND");
synchronized(mConnection) { mBluetoothLock.writeLock().lock();
mUnbinding = false; mUnbinding = false;
} mBluetoothLock.writeLock().unlock();
break; break;
} }
@@ -1381,15 +1417,15 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
/* disable and enable BT when detect a user switch */ /* disable and enable BT when detect a user switch */
if (mEnable && mBluetooth != null) { if (mEnable && mBluetooth != null) {
synchronized (mConnection) {
if (mBluetooth != null) {
//Unregister callback object
try { try {
mBluetoothLock.readLock().lock();
if (mBluetooth != null) {
mBluetooth.unregisterCallback(mBluetoothCallback); mBluetooth.unregisterCallback(mBluetoothCallback);
}
} catch (RemoteException re) { } catch (RemoteException re) {
Slog.e(TAG, "Unable to unregister",re); Slog.e(TAG, "Unable to unregister", re);
} } finally {
} mBluetoothLock.readLock().unlock();
} }
if (mState == BluetoothAdapter.STATE_TURNING_OFF) { if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
@@ -1420,14 +1456,16 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF, bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
BluetoothAdapter.STATE_OFF); BluetoothAdapter.STATE_OFF);
sendBluetoothServiceDownCallback(); sendBluetoothServiceDownCallback();
synchronized (mConnection) {
mBluetoothLock.writeLock().lock();
if (mBluetooth != null) { if (mBluetooth != null) {
mBluetooth = null; mBluetooth = null;
//Unbind // Unbind
mContext.unbindService(mConnection); mContext.unbindService(mConnection);
} }
mBluetoothGatt = null; mBluetoothGatt = null;
} mBluetoothLock.writeLock().unlock();
SystemClock.sleep(100); SystemClock.sleep(100);
mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE); mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
@@ -1450,7 +1488,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
if (DBG) Slog.d(TAG, "MESSAGE_USER_UNLOCKED"); if (DBG) Slog.d(TAG, "MESSAGE_USER_UNLOCKED");
mHandler.removeMessages(MESSAGE_USER_SWITCHED); mHandler.removeMessages(MESSAGE_USER_SWITCHED);
synchronized (mConnection) {
if (mEnable && !mBinding && (mBluetooth == null)) { if (mEnable && !mBinding && (mBluetooth == null)) {
// We should be connected, but we gave up for some // We should be connected, but we gave up for some
// reason; maybe the Bluetooth service wasn't encryption // reason; maybe the Bluetooth service wasn't encryption
@@ -1462,12 +1499,12 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
} }
} }
} }
}
private void handleEnable(boolean quietMode) { private void handleEnable(boolean quietMode) {
mQuietEnable = quietMode; mQuietEnable = quietMode;
synchronized(mConnection) { try {
mBluetoothLock.writeLock().lock();
if ((mBluetooth == null) && (!mBinding)) { if ((mBluetooth == null) && (!mBinding)) {
//Start bind timeout and bind //Start bind timeout and bind
Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND); Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
@@ -1496,6 +1533,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
Slog.e(TAG,"Unable to call enable()",e); Slog.e(TAG,"Unable to call enable()",e);
} }
} }
} finally {
mBluetoothLock.writeLock().unlock();
} }
} }
@@ -1510,18 +1549,18 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
} }
private void handleDisable() { private void handleDisable() {
synchronized(mConnection) { try {
mBluetoothLock.readLock().lock();
if (mBluetooth != null) { if (mBluetooth != null) {
if (DBG) Slog.d(TAG,"Sending off request."); if (DBG) Slog.d(TAG,"Sending off request.");
if (!mBluetooth.disable()) {
try {
if(!mBluetooth.disable()) {
Slog.e(TAG,"IBluetooth.disable() returned false"); Slog.e(TAG,"IBluetooth.disable() returned false");
} }
}
} catch (RemoteException e) { } catch (RemoteException e) {
Slog.e(TAG,"Unable to call disable()",e); Slog.e(TAG,"Unable to call disable()",e);
} } finally {
} mBluetoothLock.readLock().unlock();
} }
} }
@@ -1648,8 +1687,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
private boolean waitForOnOff(boolean on, boolean off) { private boolean waitForOnOff(boolean on, boolean off) {
int i = 0; int i = 0;
while (i < 10) { while (i < 10) {
synchronized(mConnection) {
try { try {
mBluetoothLock.readLock().lock();
if (mBluetooth == null) break; if (mBluetooth == null) break;
if (on) { if (on) {
if (mBluetooth.getState() == BluetoothAdapter.STATE_ON) return true; if (mBluetooth.getState() == BluetoothAdapter.STATE_ON) return true;
@@ -1661,7 +1700,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
} catch (RemoteException e) { } catch (RemoteException e) {
Slog.e(TAG, "getState()", e); Slog.e(TAG, "getState()", e);
break; break;
} } finally {
mBluetoothLock.readLock().unlock();
} }
if (on || off) { if (on || off) {
SystemClock.sleep(300); SystemClock.sleep(300);
@@ -1684,34 +1724,36 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
} }
private boolean canUnbindBluetoothService() { private boolean canUnbindBluetoothService() {
synchronized(mConnection) { try {
//Only unbind with mEnable flag not set //Only unbind with mEnable flag not set
//For race condition: disable and enable back-to-back //For race condition: disable and enable back-to-back
//Avoid unbind right after enable due to callback from disable //Avoid unbind right after enable due to callback from disable
//Only unbind with Bluetooth at OFF state //Only unbind with Bluetooth at OFF state
//Only unbind without any MESSAGE_BLUETOOTH_STATE_CHANGE message //Only unbind without any MESSAGE_BLUETOOTH_STATE_CHANGE message
try { mBluetoothLock.readLock().lock();
if (mEnable || (mBluetooth == null)) return false; if (mEnable || (mBluetooth == null)) return false;
if (mHandler.hasMessages(MESSAGE_BLUETOOTH_STATE_CHANGE)) return false; if (mHandler.hasMessages(MESSAGE_BLUETOOTH_STATE_CHANGE)) return false;
return (mBluetooth.getState() == BluetoothAdapter.STATE_OFF); return (mBluetooth.getState() == BluetoothAdapter.STATE_OFF);
} catch (RemoteException e) { } catch (RemoteException e) {
Slog.e(TAG, "getState()", e); Slog.e(TAG, "getState()", e);
} } finally {
mBluetoothLock.readLock().unlock();
} }
return false; return false;
} }
private void recoverBluetoothServiceFromError() { private void recoverBluetoothServiceFromError() {
Slog.e(TAG,"recoverBluetoothServiceFromError"); Slog.e(TAG,"recoverBluetoothServiceFromError");
synchronized (mConnection) { try {
mBluetoothLock.readLock().lock();
if (mBluetooth != null) { if (mBluetooth != null) {
//Unregister callback object //Unregister callback object
try {
mBluetooth.unregisterCallback(mBluetoothCallback); mBluetooth.unregisterCallback(mBluetoothCallback);
}
} catch (RemoteException re) { } catch (RemoteException re) {
Slog.e(TAG, "Unable to unregister",re); Slog.e(TAG, "Unable to unregister", re);
} } finally {
} mBluetoothLock.readLock().unlock();
} }
SystemClock.sleep(500); SystemClock.sleep(500);
@@ -1722,14 +1764,15 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
waitForOnOff(false, true); waitForOnOff(false, true);
sendBluetoothServiceDownCallback(); sendBluetoothServiceDownCallback();
synchronized (mConnection) {
mBluetoothLock.writeLock().lock();
if (mBluetooth != null) { if (mBluetooth != null) {
mBluetooth = null; mBluetooth = null;
//Unbind // Unbind
mContext.unbindService(mConnection); mContext.unbindService(mConnection);
} }
mBluetoothGatt = null; mBluetoothGatt = null;
} mBluetoothLock.writeLock().unlock();
mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE); mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
mState = BluetoothAdapter.STATE_OFF; mState = BluetoothAdapter.STATE_OFF;