Loading core/java/android/bluetooth/BluetoothAdapter.java +32 −65 Original line number Diff line number Diff line Loading @@ -200,6 +200,23 @@ public final class BluetoothAdapter { */ public static final int STATE_BLE_TURNING_OFF = 16; /** * Human-readable string helper for AdapterState * @hide */ public static String nameForState(@AdapterState int state) { switch(state) { case STATE_OFF: return "OFF"; case STATE_TURNING_ON: return "TURNING_ON"; case STATE_ON: return "ON"; case STATE_TURNING_OFF: return "TURNING_OFF"; case STATE_BLE_TURNING_ON: return "BLE_TURNING_ON"; case STATE_BLE_ON: return "BLE_ON"; case STATE_BLE_TURNING_OFF: return "BLE_TURNING_OFF"; default: return "?!?!? (" + state + ")"; } } /** * Activity Action: Show a system activity that requests discoverable mode. * This activity will also request the user to turn on Bluetooth if it Loading Loading @@ -658,15 +675,8 @@ public final class BluetoothAdapter { @SystemApi public boolean isLeEnabled() { final int state = getLeState(); if (state == BluetoothAdapter.STATE_ON) { if (DBG) Log.d (TAG, "STATE_ON"); } else if (state == BluetoothAdapter.STATE_BLE_ON) { if (DBG) Log.d (TAG, "STATE_BLE_ON"); } else { if (DBG) Log.d (TAG, "STATE_OFF"); return false; } return true; if (DBG) Log.d(TAG, "isLeEnabled(): " + BluetoothAdapter.nameForState(state)); return (state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_BLE_ON); } /** Loading Loading @@ -831,10 +841,10 @@ public final class BluetoothAdapter { if (state == BluetoothAdapter.STATE_BLE_ON || state == BluetoothAdapter.STATE_BLE_TURNING_ON || state == BluetoothAdapter.STATE_BLE_TURNING_OFF) { if (VDBG) Log.d(TAG, "Consider internal state as OFF"); if (VDBG) Log.d(TAG, "Consider " + BluetoothAdapter.nameForState(state) + " state as OFF"); state = BluetoothAdapter.STATE_OFF; } if (VDBG) Log.d(TAG, "" + hashCode() + ": getState(). Returning " + state); if (VDBG) Log.d(TAG, "" + hashCode() + ": getState(). Returning " + BluetoothAdapter.nameForState(state)); return state; } Loading Loading @@ -871,7 +881,7 @@ public final class BluetoothAdapter { mServiceLock.readLock().unlock(); } if (VDBG) Log.d(TAG,"getLeState() returning " + state); if (VDBG) Log.d(TAG,"getLeState() returning " + BluetoothAdapter.nameForState(state)); return state; } Loading Loading @@ -914,8 +924,8 @@ public final class BluetoothAdapter { */ @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) public boolean enable() { if (isEnabled() == true) { if (DBG) Log.d(TAG, "enable(): BT is already enabled..!"); if (isEnabled()) { if (DBG) Log.d(TAG, "enable(): BT already enabled!"); return true; } try { Loading Loading @@ -1518,8 +1528,9 @@ public final class BluetoothAdapter { } } } } catch (RemoteException e) {Log.e(TAG, "getSupportedProfiles:", e);} } catch (RemoteException e) { Log.e(TAG, "getSupportedProfiles:", e); } return supportedProfiles; } Loading Loading @@ -1870,34 +1881,6 @@ public final class BluetoothAdapter { * @hide */ public Pair<byte[], byte[]> readOutOfBandData() { if (getState() != STATE_ON) return null; //TODO(BT /* try { byte[] hash; byte[] randomizer; byte[] ret = null; mServiceLock.readLock().lock(); if (mService != null) mService.readOutOfBandData(); if (ret == null || ret.length != 32) return null; hash = Arrays.copyOfRange(ret, 0, 16); randomizer = Arrays.copyOfRange(ret, 16, 32); if (DBG) { Log.d(TAG, "readOutOfBandData:" + Arrays.toString(hash) + ":" + Arrays.toString(randomizer)); } return new Pair<byte[], byte[]>(hash, randomizer); } catch (RemoteException e) { Log.e(TAG, "", e); } finally { mServiceLock.readLock().unlock(); } */ return null; } Loading Loading @@ -2051,7 +2034,7 @@ public final class BluetoothAdapter { if (cb != null) { cb.onBluetoothServiceUp(bluetoothService); } else { Log.d(TAG, "onBluetoothServiceUp: cb is null!!!"); Log.d(TAG, "onBluetoothServiceUp: cb is null!"); } } catch (Exception e) { Log.e(TAG,"",e); Loading Loading @@ -2079,7 +2062,7 @@ public final class BluetoothAdapter { if (cb != null) { cb.onBluetoothServiceDown(); } else { Log.d(TAG, "onBluetoothServiceDown: cb is null!!!"); Log.d(TAG, "onBluetoothServiceDown: cb is null!"); } } catch (Exception e) { Log.e(TAG,"",e); Loading @@ -2089,7 +2072,7 @@ public final class BluetoothAdapter { } public void onBrEdrDown() { if (DBG) Log.i(TAG, "onBrEdrDown:"); if (VDBG) Log.i(TAG, "onBrEdrDown: " + mService); } }; Loading @@ -2100,7 +2083,7 @@ public final class BluetoothAdapter { */ public boolean enableNoAutoConnect() { if (isEnabled() == true){ if (DBG) Log.d(TAG, "enableNoAutoConnect(): BT is already enabled..!"); if (DBG) Log.d(TAG, "enableNoAutoConnect(): BT already enabled!"); return true; } try { Loading Loading @@ -2140,22 +2123,6 @@ public final class BluetoothAdapter { */ public boolean changeApplicationBluetoothState(boolean on, BluetoothStateChangeCallback callback) { if (callback == null) return false; //TODO(BT) /* try { mServiceLock.readLock().lock(); if (mService != null) { return mService.changeApplicationBluetoothState(on, new StateChangeCallbackWrapper(callback), new Binder()); } } catch (RemoteException e) { Log.e(TAG, "changeBluetoothState", e); } finally { mServiceLock.readLock().unlock(); } */ return false; } Loading services/core/java/com/android/server/BluetoothManagerService.java +119 −122 Original line number Diff line number Diff line Loading @@ -104,6 +104,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { private static final int MESSAGE_USER_UNLOCKED = 301; private static final int MESSAGE_ADD_PROXY_DELAYED = 400; private static final int MESSAGE_BIND_PROFILE_SERVICE = 401; private static final int MAX_SAVE_RETRIES = 3; private static final int MAX_ERROR_RESTART_RETRIES = 6; Loading Loading @@ -228,7 +229,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub { } finally { mBluetoothLock.readLock().unlock(); } Slog.d(TAG, "Airplane Mode change - current state: " + st); Slog.d(TAG, "Airplane Mode change - current state: " + BluetoothAdapter.nameForState(st)); if (isAirplaneModeOn()) { // Clear registered LE apps to force shut-off Loading Loading @@ -426,6 +428,10 @@ class BluetoothManagerService extends IBluetoothManager.Stub { public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) { mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); if (callback == null) { Slog.w(TAG, "registerStateChangeCallback: Callback is null!"); return; } Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_STATE_CHANGE_CALLBACK); msg.obj = callback; mHandler.sendMessage(msg); Loading @@ -434,6 +440,10 @@ class BluetoothManagerService extends IBluetoothManager.Stub { public void unregisterStateChangeCallback(IBluetoothStateChangeCallback callback) { mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); if (callback == null) { Slog.w(TAG, "unregisterStateChangeCallback: Callback is null!"); return; } Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK); msg.obj = callback; mHandler.sendMessage(msg); Loading @@ -460,7 +470,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { public int getState() { if ((Binder.getCallingUid() != Process.SYSTEM_UID) && (!checkIfCallerIsForegroundUser())) { Slog.w(TAG, "getState(): not allowed for non-active and non system user"); Slog.w(TAG, "getState(): report OFF for non-active and non system user"); return BluetoothAdapter.STATE_OFF; } Loading Loading @@ -712,7 +722,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub { if (DBG) { Slog.d(TAG,"enable(): mBluetooth =" + mBluetooth + " mBinding = " + mBinding + " mState = " + mState); " mBinding = " + mBinding + " mState = " + BluetoothAdapter.nameForState(mState)); } synchronized(mReceiver) { Loading Loading @@ -791,7 +802,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { public void unbindAndFinish() { if (DBG) { Slog.d(TAG,"unbindAndFinish(): " + mBluetooth + " mBinding = " + mBinding); " mBinding = " + mBinding + " mUnbinding = " + mUnbinding); } try { Loading @@ -807,11 +818,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub { } catch (RemoteException re) { Slog.e(TAG, "Unable to unregister BluetoothCallback",re); } if (DBG) Slog.d(TAG, "Sending unbind request."); mBluetoothBinder = null; mBluetooth = null; //Unbind mContext.unbindService(mConnection); mUnbinding = false; mBinding = false; Loading Loading @@ -1092,7 +1100,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub { * Inform BluetoothAdapter instances that Adapter service is up */ private void sendBluetoothServiceUpCallback() { if (DBG) Slog.d(TAG,"Calling onBluetoothServiceUp callbacks"); try { int n = mCallbacks.beginBroadcast(); Slog.d(TAG,"Broadcasting onBluetoothServiceUp() to " + n + " receivers."); Loading @@ -1111,7 +1118,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub { * Inform BluetoothAdapter instances that Adapter service is down */ private void sendBluetoothServiceDownCallback() { if (DBG) Slog.d(TAG,"Calling onBluetoothServiceDown callbacks"); try { int n = mCallbacks.beginBroadcast(); Slog.d(TAG,"Broadcasting onBluetoothServiceDown() to " + n + " receivers."); Loading Loading @@ -1183,34 +1189,33 @@ class BluetoothManagerService extends IBluetoothManager.Stub { } private class BluetoothServiceConnection implements ServiceConnection { public void onServiceConnected(ComponentName className, IBinder service) { if (DBG) Slog.d(TAG, "BluetoothServiceConnection: " + className.getClassName()); public void onServiceConnected(ComponentName componentName, IBinder service) { String name = componentName.getClassName(); if (DBG) Slog.d(TAG, "BluetoothServiceConnection: " + name); Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED); // TBD if (className.getClassName().equals(IBluetooth.class.getName())) { if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) { if (name.equals("com.android.bluetooth.btservice.AdapterService")) { msg.arg1 = SERVICE_IBLUETOOTH; // } else if (className.getClassName().equals(IBluetoothGatt.class.getName())) { } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) { } else if (name.equals("com.android.bluetooth.gatt.GattService")) { msg.arg1 = SERVICE_IBLUETOOTHGATT; } else { Slog.e(TAG, "Unknown service connected: " + className.getClassName()); Slog.e(TAG, "Unknown service connected: " + name); return; } msg.obj = service; mHandler.sendMessage(msg); } public void onServiceDisconnected(ComponentName className) { // Called if we unexpected disconnected. if (DBG) Slog.d(TAG, "BluetoothServiceConnection, disconnected: " + className.getClassName()); public void onServiceDisconnected(ComponentName componentName) { // Called if we unexpectedly disconnect. String name = componentName.getClassName(); if (DBG) Slog.d(TAG, "BluetoothServiceConnection, disconnected: " + name); Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED); if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) { if (name.equals("com.android.bluetooth.btservice.AdapterService")) { msg.arg1 = SERVICE_IBLUETOOTH; } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) { } else if (name.equals("com.android.bluetooth.gatt.GattService")) { msg.arg1 = SERVICE_IBLUETOOTHGATT; } else { Slog.e(TAG, "Unknown service disconnected: " + className.getClassName()); Slog.e(TAG, "Unknown service disconnected: " + name); return; } mHandler.sendMessage(msg); Loading @@ -1228,7 +1233,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub { @Override public void handleMessage(Message msg) { if (DBG) Slog.d (TAG, "Message: " + msg.what); switch (msg.what) { case MESSAGE_GET_NAME_AND_ADDRESS: if (DBG) Slog.d(TAG, "MESSAGE_GET_NAME_AND_ADDRESS"); Loading Loading @@ -1266,7 +1270,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { case MESSAGE_ENABLE: if (DBG) { Slog.d(TAG, "MESSAGE_ENABLE: mBluetooth = " + mBluetooth); Slog.d(TAG, "MESSAGE_ENABLE(" + msg.arg1 + "): mBluetooth = " + mBluetooth); } mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE); mEnable = true; Loading Loading @@ -1316,6 +1320,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { break; case MESSAGE_DISABLE: if (DBG) Slog.d(TAG, "MESSAGE_DISABLE: mBluetooth = " + mBluetooth); mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE); if (mEnable && mBluetooth != null) { waitForOnOff(true, false); Loading @@ -1331,31 +1336,25 @@ class BluetoothManagerService extends IBluetoothManager.Stub { case MESSAGE_REGISTER_ADAPTER: { IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj; boolean added = mCallbacks.register(callback); Slog.d(TAG,"Added callback: " + (callback == null? "null": callback) +":" +added ); } mCallbacks.register(callback); break; } case MESSAGE_UNREGISTER_ADAPTER: { IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj; boolean removed = mCallbacks.unregister(callback); Slog.d(TAG,"Removed callback: " + (callback == null? "null": callback) +":" + removed); mCallbacks.unregister(callback); break; } case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK: { IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj; if (callback != null) { mStateChangeCallbacks.register(callback); } break; } case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK: { IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj; if (callback != null) { mStateChangeCallbacks.unregister(callback); } break; } case MESSAGE_ADD_PROXY_DELAYED: Loading Loading @@ -1431,9 +1430,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { if (!mBluetooth.enable()) { Slog.e(TAG,"IBluetooth.enable() returned false"); } } else { } else { if (!mBluetooth.enableNoAutoConnect()) { Slog.e(TAG,"IBluetooth.enableNoAutoConnect() returned false"); } Loading @@ -1452,19 +1449,14 @@ class BluetoothManagerService extends IBluetoothManager.Stub { } break; } case MESSAGE_TIMEOUT_BIND: { Slog.e(TAG, "MESSAGE_TIMEOUT_BIND"); mBluetoothLock.writeLock().lock(); mBinding = false; mBluetoothLock.writeLock().unlock(); break; } case MESSAGE_BLUETOOTH_STATE_CHANGE: { int prevState = msg.arg1; int newState = msg.arg2; if (DBG) Slog.d(TAG, "MESSAGE_BLUETOOTH_STATE_CHANGE: prevState = " + prevState + ", newState =" + newState); if (DBG) { Slog.d(TAG, "MESSAGE_BLUETOOTH_STATE_CHANGE: " + BluetoothAdapter.nameForState(prevState) + " > " + BluetoothAdapter.nameForState(newState)); } mState = newState; bluetoothStateChangeHandler(prevState, newState); // handle error state transition case from TURNING_ON to OFF Loading Loading @@ -1504,7 +1496,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { } case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: { Slog.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: " + msg.arg1); Slog.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED(" + msg.arg1 + ")"); try { mBluetoothLock.writeLock().lock(); if (msg.arg1 == SERVICE_IBLUETOOTH) { Loading @@ -1515,7 +1507,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { mBluetoothGatt = null; break; } else { Slog.e(TAG, "Bad msg.arg1: " + msg.arg1); Slog.e(TAG, "Unknown argument for service disconnect!"); break; } } finally { Loading Loading @@ -1552,8 +1544,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { } case MESSAGE_RESTART_BLUETOOTH_SERVICE: { Slog.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE:" +" Restart IBluetooth service"); Slog.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE"); /* Enable without persisting the setting as it doesnt change when IBluetooth service restarts */ Loading @@ -1561,7 +1552,13 @@ class BluetoothManagerService extends IBluetoothManager.Stub { handleEnable(mQuietEnable); break; } case MESSAGE_TIMEOUT_BIND: { Slog.e(TAG, "MESSAGE_TIMEOUT_BIND"); mBluetoothLock.writeLock().lock(); mBinding = false; mBluetoothLock.writeLock().unlock(); break; } case MESSAGE_TIMEOUT_UNBIND: { Slog.e(TAG, "MESSAGE_TIMEOUT_UNBIND"); Loading Loading @@ -1647,11 +1644,10 @@ class BluetoothManagerService extends IBluetoothManager.Stub { } else if (mBinding || mBluetooth != null) { Message userMsg = mHandler.obtainMessage(MESSAGE_USER_SWITCHED); userMsg.arg2 = 1 + msg.arg2; // if user is switched when service is being binding // delay sending MESSAGE_USER_SWITCHED // if user is switched when service is binding retry after a delay mHandler.sendMessageDelayed(userMsg, USER_SWITCHED_TIME_MS); if (DBG) { Slog.d(TAG, "delay MESSAGE_USER_SWITCHED " + userMsg.arg2); Slog.d(TAG, "Retry MESSAGE_USER_SWITCHED " + userMsg.arg2); } } break; Loading Loading @@ -1752,7 +1748,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { parentUser == foregroundUser || callingAppId == Process.NFC_UID || callingAppId == mSystemUiUid; if (DBG) { if (DBG && !valid) { Slog.d(TAG, "checkIfCallerIsForegroundUser: valid=" + valid + " callingUser=" + callingUser + " parentUser=" + parentUser Loading @@ -1765,7 +1761,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub { } private void sendBleStateChanged(int prevState, int newState) { if (DBG) Slog.d(TAG,"BLE State Change Intent: " + prevState + " -> " + newState); if (DBG) Slog.d(TAG,"Sending BLE State Change: " + BluetoothAdapter.nameForState(prevState) + " > " + BluetoothAdapter.nameForState(newState)); // Send broadcast message to everyone else Intent intent = new Intent(BluetoothAdapter.ACTION_BLE_STATE_CHANGED); intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState); Loading @@ -1776,8 +1773,9 @@ class BluetoothManagerService extends IBluetoothManager.Stub { private void bluetoothStateChangeHandler(int prevState, int newState) { boolean isStandardBroadcast = true; if (DBG) Slog.d(TAG, "bluetoothStateChangeHandler: " + prevState + " -> " + newState); if (prevState != newState) { if (prevState == newState) { // No change. Nothing to do. return; } // Notify all proxy objects first of adapter state change if (newState == BluetoothAdapter.STATE_BLE_ON || newState == BluetoothAdapter.STATE_OFF) { Loading @@ -1786,7 +1784,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { if (newState == BluetoothAdapter.STATE_OFF) { // If Bluetooth is off, send service down event to proxy objects, and unbind if (DBG) Slog.d(TAG, "Bluetooth is complete turn off"); if (DBG) Slog.d(TAG, "Bluetooth is complete send Service Down"); sendBluetoothServiceDownCallback(); unbindAndFinish(); sendBleStateChanged(prevState, newState); Loading Loading @@ -1847,7 +1845,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub { mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM); } } } /** * if on is true, wait for state become ON Loading Loading
core/java/android/bluetooth/BluetoothAdapter.java +32 −65 Original line number Diff line number Diff line Loading @@ -200,6 +200,23 @@ public final class BluetoothAdapter { */ public static final int STATE_BLE_TURNING_OFF = 16; /** * Human-readable string helper for AdapterState * @hide */ public static String nameForState(@AdapterState int state) { switch(state) { case STATE_OFF: return "OFF"; case STATE_TURNING_ON: return "TURNING_ON"; case STATE_ON: return "ON"; case STATE_TURNING_OFF: return "TURNING_OFF"; case STATE_BLE_TURNING_ON: return "BLE_TURNING_ON"; case STATE_BLE_ON: return "BLE_ON"; case STATE_BLE_TURNING_OFF: return "BLE_TURNING_OFF"; default: return "?!?!? (" + state + ")"; } } /** * Activity Action: Show a system activity that requests discoverable mode. * This activity will also request the user to turn on Bluetooth if it Loading Loading @@ -658,15 +675,8 @@ public final class BluetoothAdapter { @SystemApi public boolean isLeEnabled() { final int state = getLeState(); if (state == BluetoothAdapter.STATE_ON) { if (DBG) Log.d (TAG, "STATE_ON"); } else if (state == BluetoothAdapter.STATE_BLE_ON) { if (DBG) Log.d (TAG, "STATE_BLE_ON"); } else { if (DBG) Log.d (TAG, "STATE_OFF"); return false; } return true; if (DBG) Log.d(TAG, "isLeEnabled(): " + BluetoothAdapter.nameForState(state)); return (state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_BLE_ON); } /** Loading Loading @@ -831,10 +841,10 @@ public final class BluetoothAdapter { if (state == BluetoothAdapter.STATE_BLE_ON || state == BluetoothAdapter.STATE_BLE_TURNING_ON || state == BluetoothAdapter.STATE_BLE_TURNING_OFF) { if (VDBG) Log.d(TAG, "Consider internal state as OFF"); if (VDBG) Log.d(TAG, "Consider " + BluetoothAdapter.nameForState(state) + " state as OFF"); state = BluetoothAdapter.STATE_OFF; } if (VDBG) Log.d(TAG, "" + hashCode() + ": getState(). Returning " + state); if (VDBG) Log.d(TAG, "" + hashCode() + ": getState(). Returning " + BluetoothAdapter.nameForState(state)); return state; } Loading Loading @@ -871,7 +881,7 @@ public final class BluetoothAdapter { mServiceLock.readLock().unlock(); } if (VDBG) Log.d(TAG,"getLeState() returning " + state); if (VDBG) Log.d(TAG,"getLeState() returning " + BluetoothAdapter.nameForState(state)); return state; } Loading Loading @@ -914,8 +924,8 @@ public final class BluetoothAdapter { */ @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) public boolean enable() { if (isEnabled() == true) { if (DBG) Log.d(TAG, "enable(): BT is already enabled..!"); if (isEnabled()) { if (DBG) Log.d(TAG, "enable(): BT already enabled!"); return true; } try { Loading Loading @@ -1518,8 +1528,9 @@ public final class BluetoothAdapter { } } } } catch (RemoteException e) {Log.e(TAG, "getSupportedProfiles:", e);} } catch (RemoteException e) { Log.e(TAG, "getSupportedProfiles:", e); } return supportedProfiles; } Loading Loading @@ -1870,34 +1881,6 @@ public final class BluetoothAdapter { * @hide */ public Pair<byte[], byte[]> readOutOfBandData() { if (getState() != STATE_ON) return null; //TODO(BT /* try { byte[] hash; byte[] randomizer; byte[] ret = null; mServiceLock.readLock().lock(); if (mService != null) mService.readOutOfBandData(); if (ret == null || ret.length != 32) return null; hash = Arrays.copyOfRange(ret, 0, 16); randomizer = Arrays.copyOfRange(ret, 16, 32); if (DBG) { Log.d(TAG, "readOutOfBandData:" + Arrays.toString(hash) + ":" + Arrays.toString(randomizer)); } return new Pair<byte[], byte[]>(hash, randomizer); } catch (RemoteException e) { Log.e(TAG, "", e); } finally { mServiceLock.readLock().unlock(); } */ return null; } Loading Loading @@ -2051,7 +2034,7 @@ public final class BluetoothAdapter { if (cb != null) { cb.onBluetoothServiceUp(bluetoothService); } else { Log.d(TAG, "onBluetoothServiceUp: cb is null!!!"); Log.d(TAG, "onBluetoothServiceUp: cb is null!"); } } catch (Exception e) { Log.e(TAG,"",e); Loading Loading @@ -2079,7 +2062,7 @@ public final class BluetoothAdapter { if (cb != null) { cb.onBluetoothServiceDown(); } else { Log.d(TAG, "onBluetoothServiceDown: cb is null!!!"); Log.d(TAG, "onBluetoothServiceDown: cb is null!"); } } catch (Exception e) { Log.e(TAG,"",e); Loading @@ -2089,7 +2072,7 @@ public final class BluetoothAdapter { } public void onBrEdrDown() { if (DBG) Log.i(TAG, "onBrEdrDown:"); if (VDBG) Log.i(TAG, "onBrEdrDown: " + mService); } }; Loading @@ -2100,7 +2083,7 @@ public final class BluetoothAdapter { */ public boolean enableNoAutoConnect() { if (isEnabled() == true){ if (DBG) Log.d(TAG, "enableNoAutoConnect(): BT is already enabled..!"); if (DBG) Log.d(TAG, "enableNoAutoConnect(): BT already enabled!"); return true; } try { Loading Loading @@ -2140,22 +2123,6 @@ public final class BluetoothAdapter { */ public boolean changeApplicationBluetoothState(boolean on, BluetoothStateChangeCallback callback) { if (callback == null) return false; //TODO(BT) /* try { mServiceLock.readLock().lock(); if (mService != null) { return mService.changeApplicationBluetoothState(on, new StateChangeCallbackWrapper(callback), new Binder()); } } catch (RemoteException e) { Log.e(TAG, "changeBluetoothState", e); } finally { mServiceLock.readLock().unlock(); } */ return false; } Loading
services/core/java/com/android/server/BluetoothManagerService.java +119 −122 Original line number Diff line number Diff line Loading @@ -104,6 +104,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { private static final int MESSAGE_USER_UNLOCKED = 301; private static final int MESSAGE_ADD_PROXY_DELAYED = 400; private static final int MESSAGE_BIND_PROFILE_SERVICE = 401; private static final int MAX_SAVE_RETRIES = 3; private static final int MAX_ERROR_RESTART_RETRIES = 6; Loading Loading @@ -228,7 +229,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub { } finally { mBluetoothLock.readLock().unlock(); } Slog.d(TAG, "Airplane Mode change - current state: " + st); Slog.d(TAG, "Airplane Mode change - current state: " + BluetoothAdapter.nameForState(st)); if (isAirplaneModeOn()) { // Clear registered LE apps to force shut-off Loading Loading @@ -426,6 +428,10 @@ class BluetoothManagerService extends IBluetoothManager.Stub { public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) { mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); if (callback == null) { Slog.w(TAG, "registerStateChangeCallback: Callback is null!"); return; } Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_STATE_CHANGE_CALLBACK); msg.obj = callback; mHandler.sendMessage(msg); Loading @@ -434,6 +440,10 @@ class BluetoothManagerService extends IBluetoothManager.Stub { public void unregisterStateChangeCallback(IBluetoothStateChangeCallback callback) { mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); if (callback == null) { Slog.w(TAG, "unregisterStateChangeCallback: Callback is null!"); return; } Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK); msg.obj = callback; mHandler.sendMessage(msg); Loading @@ -460,7 +470,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { public int getState() { if ((Binder.getCallingUid() != Process.SYSTEM_UID) && (!checkIfCallerIsForegroundUser())) { Slog.w(TAG, "getState(): not allowed for non-active and non system user"); Slog.w(TAG, "getState(): report OFF for non-active and non system user"); return BluetoothAdapter.STATE_OFF; } Loading Loading @@ -712,7 +722,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub { if (DBG) { Slog.d(TAG,"enable(): mBluetooth =" + mBluetooth + " mBinding = " + mBinding + " mState = " + mState); " mBinding = " + mBinding + " mState = " + BluetoothAdapter.nameForState(mState)); } synchronized(mReceiver) { Loading Loading @@ -791,7 +802,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { public void unbindAndFinish() { if (DBG) { Slog.d(TAG,"unbindAndFinish(): " + mBluetooth + " mBinding = " + mBinding); " mBinding = " + mBinding + " mUnbinding = " + mUnbinding); } try { Loading @@ -807,11 +818,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub { } catch (RemoteException re) { Slog.e(TAG, "Unable to unregister BluetoothCallback",re); } if (DBG) Slog.d(TAG, "Sending unbind request."); mBluetoothBinder = null; mBluetooth = null; //Unbind mContext.unbindService(mConnection); mUnbinding = false; mBinding = false; Loading Loading @@ -1092,7 +1100,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub { * Inform BluetoothAdapter instances that Adapter service is up */ private void sendBluetoothServiceUpCallback() { if (DBG) Slog.d(TAG,"Calling onBluetoothServiceUp callbacks"); try { int n = mCallbacks.beginBroadcast(); Slog.d(TAG,"Broadcasting onBluetoothServiceUp() to " + n + " receivers."); Loading @@ -1111,7 +1118,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub { * Inform BluetoothAdapter instances that Adapter service is down */ private void sendBluetoothServiceDownCallback() { if (DBG) Slog.d(TAG,"Calling onBluetoothServiceDown callbacks"); try { int n = mCallbacks.beginBroadcast(); Slog.d(TAG,"Broadcasting onBluetoothServiceDown() to " + n + " receivers."); Loading Loading @@ -1183,34 +1189,33 @@ class BluetoothManagerService extends IBluetoothManager.Stub { } private class BluetoothServiceConnection implements ServiceConnection { public void onServiceConnected(ComponentName className, IBinder service) { if (DBG) Slog.d(TAG, "BluetoothServiceConnection: " + className.getClassName()); public void onServiceConnected(ComponentName componentName, IBinder service) { String name = componentName.getClassName(); if (DBG) Slog.d(TAG, "BluetoothServiceConnection: " + name); Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED); // TBD if (className.getClassName().equals(IBluetooth.class.getName())) { if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) { if (name.equals("com.android.bluetooth.btservice.AdapterService")) { msg.arg1 = SERVICE_IBLUETOOTH; // } else if (className.getClassName().equals(IBluetoothGatt.class.getName())) { } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) { } else if (name.equals("com.android.bluetooth.gatt.GattService")) { msg.arg1 = SERVICE_IBLUETOOTHGATT; } else { Slog.e(TAG, "Unknown service connected: " + className.getClassName()); Slog.e(TAG, "Unknown service connected: " + name); return; } msg.obj = service; mHandler.sendMessage(msg); } public void onServiceDisconnected(ComponentName className) { // Called if we unexpected disconnected. if (DBG) Slog.d(TAG, "BluetoothServiceConnection, disconnected: " + className.getClassName()); public void onServiceDisconnected(ComponentName componentName) { // Called if we unexpectedly disconnect. String name = componentName.getClassName(); if (DBG) Slog.d(TAG, "BluetoothServiceConnection, disconnected: " + name); Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED); if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) { if (name.equals("com.android.bluetooth.btservice.AdapterService")) { msg.arg1 = SERVICE_IBLUETOOTH; } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) { } else if (name.equals("com.android.bluetooth.gatt.GattService")) { msg.arg1 = SERVICE_IBLUETOOTHGATT; } else { Slog.e(TAG, "Unknown service disconnected: " + className.getClassName()); Slog.e(TAG, "Unknown service disconnected: " + name); return; } mHandler.sendMessage(msg); Loading @@ -1228,7 +1233,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub { @Override public void handleMessage(Message msg) { if (DBG) Slog.d (TAG, "Message: " + msg.what); switch (msg.what) { case MESSAGE_GET_NAME_AND_ADDRESS: if (DBG) Slog.d(TAG, "MESSAGE_GET_NAME_AND_ADDRESS"); Loading Loading @@ -1266,7 +1270,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { case MESSAGE_ENABLE: if (DBG) { Slog.d(TAG, "MESSAGE_ENABLE: mBluetooth = " + mBluetooth); Slog.d(TAG, "MESSAGE_ENABLE(" + msg.arg1 + "): mBluetooth = " + mBluetooth); } mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE); mEnable = true; Loading Loading @@ -1316,6 +1320,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { break; case MESSAGE_DISABLE: if (DBG) Slog.d(TAG, "MESSAGE_DISABLE: mBluetooth = " + mBluetooth); mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE); if (mEnable && mBluetooth != null) { waitForOnOff(true, false); Loading @@ -1331,31 +1336,25 @@ class BluetoothManagerService extends IBluetoothManager.Stub { case MESSAGE_REGISTER_ADAPTER: { IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj; boolean added = mCallbacks.register(callback); Slog.d(TAG,"Added callback: " + (callback == null? "null": callback) +":" +added ); } mCallbacks.register(callback); break; } case MESSAGE_UNREGISTER_ADAPTER: { IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj; boolean removed = mCallbacks.unregister(callback); Slog.d(TAG,"Removed callback: " + (callback == null? "null": callback) +":" + removed); mCallbacks.unregister(callback); break; } case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK: { IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj; if (callback != null) { mStateChangeCallbacks.register(callback); } break; } case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK: { IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj; if (callback != null) { mStateChangeCallbacks.unregister(callback); } break; } case MESSAGE_ADD_PROXY_DELAYED: Loading Loading @@ -1431,9 +1430,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { if (!mBluetooth.enable()) { Slog.e(TAG,"IBluetooth.enable() returned false"); } } else { } else { if (!mBluetooth.enableNoAutoConnect()) { Slog.e(TAG,"IBluetooth.enableNoAutoConnect() returned false"); } Loading @@ -1452,19 +1449,14 @@ class BluetoothManagerService extends IBluetoothManager.Stub { } break; } case MESSAGE_TIMEOUT_BIND: { Slog.e(TAG, "MESSAGE_TIMEOUT_BIND"); mBluetoothLock.writeLock().lock(); mBinding = false; mBluetoothLock.writeLock().unlock(); break; } case MESSAGE_BLUETOOTH_STATE_CHANGE: { int prevState = msg.arg1; int newState = msg.arg2; if (DBG) Slog.d(TAG, "MESSAGE_BLUETOOTH_STATE_CHANGE: prevState = " + prevState + ", newState =" + newState); if (DBG) { Slog.d(TAG, "MESSAGE_BLUETOOTH_STATE_CHANGE: " + BluetoothAdapter.nameForState(prevState) + " > " + BluetoothAdapter.nameForState(newState)); } mState = newState; bluetoothStateChangeHandler(prevState, newState); // handle error state transition case from TURNING_ON to OFF Loading Loading @@ -1504,7 +1496,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { } case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: { Slog.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: " + msg.arg1); Slog.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED(" + msg.arg1 + ")"); try { mBluetoothLock.writeLock().lock(); if (msg.arg1 == SERVICE_IBLUETOOTH) { Loading @@ -1515,7 +1507,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { mBluetoothGatt = null; break; } else { Slog.e(TAG, "Bad msg.arg1: " + msg.arg1); Slog.e(TAG, "Unknown argument for service disconnect!"); break; } } finally { Loading Loading @@ -1552,8 +1544,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { } case MESSAGE_RESTART_BLUETOOTH_SERVICE: { Slog.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE:" +" Restart IBluetooth service"); Slog.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE"); /* Enable without persisting the setting as it doesnt change when IBluetooth service restarts */ Loading @@ -1561,7 +1552,13 @@ class BluetoothManagerService extends IBluetoothManager.Stub { handleEnable(mQuietEnable); break; } case MESSAGE_TIMEOUT_BIND: { Slog.e(TAG, "MESSAGE_TIMEOUT_BIND"); mBluetoothLock.writeLock().lock(); mBinding = false; mBluetoothLock.writeLock().unlock(); break; } case MESSAGE_TIMEOUT_UNBIND: { Slog.e(TAG, "MESSAGE_TIMEOUT_UNBIND"); Loading Loading @@ -1647,11 +1644,10 @@ class BluetoothManagerService extends IBluetoothManager.Stub { } else if (mBinding || mBluetooth != null) { Message userMsg = mHandler.obtainMessage(MESSAGE_USER_SWITCHED); userMsg.arg2 = 1 + msg.arg2; // if user is switched when service is being binding // delay sending MESSAGE_USER_SWITCHED // if user is switched when service is binding retry after a delay mHandler.sendMessageDelayed(userMsg, USER_SWITCHED_TIME_MS); if (DBG) { Slog.d(TAG, "delay MESSAGE_USER_SWITCHED " + userMsg.arg2); Slog.d(TAG, "Retry MESSAGE_USER_SWITCHED " + userMsg.arg2); } } break; Loading Loading @@ -1752,7 +1748,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { parentUser == foregroundUser || callingAppId == Process.NFC_UID || callingAppId == mSystemUiUid; if (DBG) { if (DBG && !valid) { Slog.d(TAG, "checkIfCallerIsForegroundUser: valid=" + valid + " callingUser=" + callingUser + " parentUser=" + parentUser Loading @@ -1765,7 +1761,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub { } private void sendBleStateChanged(int prevState, int newState) { if (DBG) Slog.d(TAG,"BLE State Change Intent: " + prevState + " -> " + newState); if (DBG) Slog.d(TAG,"Sending BLE State Change: " + BluetoothAdapter.nameForState(prevState) + " > " + BluetoothAdapter.nameForState(newState)); // Send broadcast message to everyone else Intent intent = new Intent(BluetoothAdapter.ACTION_BLE_STATE_CHANGED); intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState); Loading @@ -1776,8 +1773,9 @@ class BluetoothManagerService extends IBluetoothManager.Stub { private void bluetoothStateChangeHandler(int prevState, int newState) { boolean isStandardBroadcast = true; if (DBG) Slog.d(TAG, "bluetoothStateChangeHandler: " + prevState + " -> " + newState); if (prevState != newState) { if (prevState == newState) { // No change. Nothing to do. return; } // Notify all proxy objects first of adapter state change if (newState == BluetoothAdapter.STATE_BLE_ON || newState == BluetoothAdapter.STATE_OFF) { Loading @@ -1786,7 +1784,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub { if (newState == BluetoothAdapter.STATE_OFF) { // If Bluetooth is off, send service down event to proxy objects, and unbind if (DBG) Slog.d(TAG, "Bluetooth is complete turn off"); if (DBG) Slog.d(TAG, "Bluetooth is complete send Service Down"); sendBluetoothServiceDownCallback(); unbindAndFinish(); sendBleStateChanged(prevState, newState); Loading Loading @@ -1847,7 +1845,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub { mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM); } } } /** * if on is true, wait for state become ON Loading