fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1 | /* |
Zhihai Xu | fa0fd39 | 2012-10-23 17:31:56 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
Svet Ganov | 408abf7 | 2015-05-12 19:13:36 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 20 | import android.app.ActivityManager; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 21 | import android.bluetooth.BluetoothAdapter; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 22 | import android.bluetooth.BluetoothProfile; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 23 | import android.bluetooth.IBluetooth; |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 24 | import android.bluetooth.IBluetoothCallback; |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 25 | import android.bluetooth.IBluetoothGatt; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 26 | import android.bluetooth.IBluetoothHeadset; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 27 | import android.bluetooth.IBluetoothManager; |
| 28 | import android.bluetooth.IBluetoothManagerCallback; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 29 | import android.bluetooth.IBluetoothProfileServiceConnection; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 30 | import android.bluetooth.IBluetoothStateChangeCallback; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 31 | import android.content.BroadcastReceiver; |
| 32 | import android.content.ComponentName; |
| 33 | import android.content.ContentResolver; |
| 34 | import android.content.Context; |
| 35 | import android.content.Intent; |
| 36 | import android.content.IntentFilter; |
| 37 | import android.content.ServiceConnection; |
Matthew Xie | 32ab77b | 2013-05-08 19:26:57 -0700 | [diff] [blame] | 38 | import android.content.pm.PackageManager; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 39 | import android.content.pm.UserInfo; |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 40 | import android.database.ContentObserver; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 41 | import android.os.Binder; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 42 | import android.os.Handler; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 43 | import android.os.IBinder; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 44 | import android.os.Looper; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 45 | import android.os.Message; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 46 | import android.os.Process; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 47 | import android.os.RemoteCallbackList; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 48 | import android.os.RemoteException; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 49 | import android.os.SystemClock; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 50 | import android.os.UserHandle; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 51 | import android.os.UserManager; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 52 | import android.provider.Settings; |
Wei Wang | 67d8416 | 2015-04-26 17:04:29 -0700 | [diff] [blame] | 53 | import android.provider.Settings.SettingNotFoundException; |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 54 | import android.util.Slog; |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 55 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
Mike Lockwood | 726d4de | 2014-10-28 14:06:28 -0700 | [diff] [blame] | 56 | |
| 57 | import java.io.FileDescriptor; |
| 58 | import java.io.PrintWriter; |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 59 | import java.util.concurrent.ConcurrentHashMap; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 60 | import java.util.HashMap; |
| 61 | import java.util.Map; |
Miao Chou | 658bf2f | 2015-06-26 17:14:35 -0700 | [diff] [blame] | 62 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 63 | class BluetoothManagerService extends IBluetoothManager.Stub { |
| 64 | private static final String TAG = "BluetoothManagerService"; |
Pavlin Radoslavov | 4140111 | 2016-06-27 15:25:18 -0700 | [diff] [blame] | 65 | private static final boolean DBG = true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 66 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 67 | private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN; |
| 68 | private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 69 | private static final String ACTION_SERVICE_STATE_CHANGED="com.android.bluetooth.btservice.action.STATE_CHANGED"; |
| 70 | private static final String EXTRA_ACTION="action"; |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 71 | private static final String SECURE_SETTINGS_BLUETOOTH_ADDR_VALID="bluetooth_addr_valid"; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 72 | private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address"; |
| 73 | private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name"; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 74 | private static final int TIMEOUT_BIND_MS = 3000; //Maximum msec to wait for a bind |
| 75 | private static final int TIMEOUT_SAVE_MS = 500; //Maximum msec to wait for a save |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 76 | //Maximum msec to wait for service restart |
| 77 | private static final int SERVICE_RESTART_TIME_MS = 200; |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 78 | //Maximum msec to wait for restart due to error |
| 79 | private static final int ERROR_RESTART_TIME_MS = 3000; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 80 | //Maximum msec to delay MESSAGE_USER_SWITCHED |
| 81 | private static final int USER_SWITCHED_TIME_MS = 200; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 82 | // Delay for the addProxy function in msec |
| 83 | private static final int ADD_PROXY_DELAY_MS = 100; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 84 | |
| 85 | private static final int MESSAGE_ENABLE = 1; |
| 86 | private static final int MESSAGE_DISABLE = 2; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 87 | private static final int MESSAGE_REGISTER_ADAPTER = 20; |
| 88 | private static final int MESSAGE_UNREGISTER_ADAPTER = 21; |
| 89 | private static final int MESSAGE_REGISTER_STATE_CHANGE_CALLBACK = 30; |
| 90 | private static final int MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK = 31; |
| 91 | private static final int MESSAGE_BLUETOOTH_SERVICE_CONNECTED = 40; |
| 92 | private static final int MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED = 41; |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 93 | private static final int MESSAGE_RESTART_BLUETOOTH_SERVICE = 42; |
Jeff Sharkey | aacb89e | 2016-03-05 14:42:58 -0700 | [diff] [blame] | 94 | private static final int MESSAGE_BLUETOOTH_STATE_CHANGE = 60; |
| 95 | private static final int MESSAGE_TIMEOUT_BIND = 100; |
| 96 | private static final int MESSAGE_TIMEOUT_UNBIND = 101; |
Ajay Panicker | 4bb4830 | 2016-03-31 14:14:27 -0700 | [diff] [blame] | 97 | private static final int MESSAGE_GET_NAME_AND_ADDRESS = 200; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 98 | private static final int MESSAGE_USER_SWITCHED = 300; |
Jeff Sharkey | aacb89e | 2016-03-05 14:42:58 -0700 | [diff] [blame] | 99 | private static final int MESSAGE_USER_UNLOCKED = 301; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 100 | private static final int MESSAGE_ADD_PROXY_DELAYED = 400; |
| 101 | private static final int MESSAGE_BIND_PROFILE_SERVICE = 401; |
Jeff Sharkey | aacb89e | 2016-03-05 14:42:58 -0700 | [diff] [blame] | 102 | private static final int MAX_SAVE_RETRIES = 3; |
| 103 | private static final int MAX_ERROR_RESTART_RETRIES = 6; |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 104 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 105 | // Bluetooth persisted setting is off |
| 106 | private static final int BLUETOOTH_OFF=0; |
| 107 | // Bluetooth persisted setting is on |
| 108 | // and Airplane mode won't affect Bluetooth state at start up |
| 109 | private static final int BLUETOOTH_ON_BLUETOOTH=1; |
| 110 | // Bluetooth persisted setting is on |
| 111 | // but Airplane mode will affect Bluetooth state at start up |
| 112 | // and Airplane mode will have higher priority. |
| 113 | private static final int BLUETOOTH_ON_AIRPLANE=2; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 114 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 115 | private static final int SERVICE_IBLUETOOTH = 1; |
| 116 | private static final int SERVICE_IBLUETOOTHGATT = 2; |
| 117 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 118 | private final Context mContext; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 119 | |
| 120 | // Locks are not provided for mName and mAddress. |
| 121 | // They are accessed in handler or broadcast receiver, same thread context. |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 122 | private String mAddress; |
| 123 | private String mName; |
Matthew Xie | 6fde309 | 2012-07-11 17:10:07 -0700 | [diff] [blame] | 124 | private final ContentResolver mContentResolver; |
| 125 | private final RemoteCallbackList<IBluetoothManagerCallback> mCallbacks; |
| 126 | private final RemoteCallbackList<IBluetoothStateChangeCallback> mStateChangeCallbacks; |
Marie Janssen | 9db28eb | 2016-01-12 16:05:15 -0800 | [diff] [blame] | 127 | private IBinder mBluetoothBinder; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 128 | private IBluetooth mBluetooth; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 129 | private IBluetoothGatt mBluetoothGatt; |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 130 | private final ReentrantReadWriteLock mBluetoothLock = |
| 131 | new ReentrantReadWriteLock(); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 132 | private boolean mBinding; |
| 133 | private boolean mUnbinding; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 134 | // used inside handler thread |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 135 | private boolean mQuietEnable = false; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 136 | // configuarion from external IBinder call which is used to |
| 137 | // synchronize with broadcast receiver. |
| 138 | private boolean mQuietEnableExternal; |
| 139 | // configuarion from external IBinder call which is used to |
| 140 | // synchronize with broadcast receiver. |
| 141 | private boolean mEnableExternal; |
| 142 | // used inside handler thread |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 143 | private boolean mEnable; |
| 144 | private int mState; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 145 | private final BluetoothHandler mHandler; |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 146 | private int mErrorRecoveryRetryCounter; |
Adrian Roos | bd9a9a5 | 2014-08-18 15:31:57 +0200 | [diff] [blame] | 147 | private final int mSystemUiUid; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 148 | |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 149 | // Save a ProfileServiceConnections object for each of the bound |
| 150 | // bluetooth profile services |
| 151 | private final Map <Integer, ProfileServiceConnections> mProfileServices = |
| 152 | new HashMap <Integer, ProfileServiceConnections>(); |
| 153 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 154 | private void registerForAirplaneMode(IntentFilter filter) { |
| 155 | final ContentResolver resolver = mContext.getContentResolver(); |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 156 | final String airplaneModeRadios = Settings.Global.getString(resolver, |
| 157 | Settings.Global.AIRPLANE_MODE_RADIOS); |
| 158 | final String toggleableRadios = Settings.Global.getString(resolver, |
| 159 | Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 160 | boolean mIsAirplaneSensitive = airplaneModeRadios == null ? true : |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 161 | airplaneModeRadios.contains(Settings.Global.RADIO_BLUETOOTH); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 162 | if (mIsAirplaneSensitive) { |
| 163 | filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 164 | } |
| 165 | } |
| 166 | |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 167 | private final IBluetoothCallback mBluetoothCallback = new IBluetoothCallback.Stub() { |
| 168 | @Override |
| 169 | public void onBluetoothStateChange(int prevState, int newState) throws RemoteException { |
| 170 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_STATE_CHANGE,prevState,newState); |
| 171 | mHandler.sendMessage(msg); |
| 172 | } |
| 173 | }; |
| 174 | |
| 175 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 176 | @Override |
| 177 | public void onReceive(Context context, Intent intent) { |
| 178 | String action = intent.getAction(); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 179 | if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action)) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 180 | String newName = intent.getStringExtra(BluetoothAdapter.EXTRA_LOCAL_NAME); |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 181 | if (DBG) Slog.d(TAG, "Bluetooth Adapter name changed to " + newName); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 182 | if (newName != null) { |
| 183 | storeNameAndAddress(newName, null); |
| 184 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 185 | } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) { |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 186 | synchronized(mReceiver) { |
| 187 | if (isBluetoothPersistedStateOn()) { |
| 188 | if (isAirplaneModeOn()) { |
| 189 | persistBluetoothSetting(BLUETOOTH_ON_AIRPLANE); |
| 190 | } else { |
| 191 | persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH); |
| 192 | } |
| 193 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 194 | |
| 195 | int st = BluetoothAdapter.STATE_OFF; |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 196 | try { |
| 197 | mBluetoothLock.readLock().lock(); |
| 198 | if (mBluetooth != null) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 199 | st = mBluetooth.getState(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 200 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 201 | } catch (RemoteException e) { |
| 202 | Slog.e(TAG, "Unable to call getState", e); |
| 203 | } finally { |
| 204 | mBluetoothLock.readLock().unlock(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 205 | } |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 206 | Slog.d(TAG, "state" + st); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 207 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 208 | if (isAirplaneModeOn()) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 209 | // Clear registered LE apps to force shut-off |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 210 | clearBleApps(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 211 | if (st == BluetoothAdapter.STATE_BLE_ON) { |
| 212 | //if state is BLE_ON make sure you trigger disableBLE part |
| 213 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 214 | mBluetoothLock.readLock().lock(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 215 | if (mBluetooth != null) { |
| 216 | mBluetooth.onBrEdrDown(); |
Marie Janssen | 6a383a7 | 2016-10-25 10:47:51 -0700 | [diff] [blame] | 217 | mEnable = false; |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 218 | mEnableExternal = false; |
| 219 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 220 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 221 | Slog.e(TAG,"Unable to call onBrEdrDown", e); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 222 | } finally { |
Pavlin Radoslavov | 7ee53be | 2016-06-09 12:58:07 -0700 | [diff] [blame] | 223 | mBluetoothLock.readLock().unlock(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 224 | } |
| 225 | } else if (st == BluetoothAdapter.STATE_ON){ |
| 226 | // disable without persisting the setting |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 227 | Slog.d(TAG, "Calling disable"); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 228 | sendDisableMsg(); |
| 229 | } |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 230 | } else if (mEnableExternal) { |
| 231 | // enable without persisting the setting |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 232 | Slog.d(TAG, "Calling enable"); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 233 | sendEnableMsg(mQuietEnableExternal); |
| 234 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 235 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | }; |
| 239 | |
| 240 | BluetoothManagerService(Context context) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 241 | mHandler = new BluetoothHandler(IoThread.get().getLooper()); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 242 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 243 | mContext = context; |
| 244 | mBluetooth = null; |
Marie Janssen | 9db28eb | 2016-01-12 16:05:15 -0800 | [diff] [blame] | 245 | mBluetoothBinder = null; |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 246 | mBluetoothGatt = null; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 247 | mBinding = false; |
| 248 | mUnbinding = false; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 249 | mEnable = false; |
| 250 | mState = BluetoothAdapter.STATE_OFF; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 251 | mQuietEnableExternal = false; |
| 252 | mEnableExternal = false; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 253 | mAddress = null; |
| 254 | mName = null; |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 255 | mErrorRecoveryRetryCounter = 0; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 256 | mContentResolver = context.getContentResolver(); |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 257 | // Observe BLE scan only mode settings change. |
| 258 | registerForBleScanModeChange(); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 259 | mCallbacks = new RemoteCallbackList<IBluetoothManagerCallback>(); |
| 260 | mStateChangeCallbacks = new RemoteCallbackList<IBluetoothStateChangeCallback>(); |
Miao Chou | 658bf2f | 2015-06-26 17:14:35 -0700 | [diff] [blame] | 261 | IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED); |
Matthew Xie | 6fde309 | 2012-07-11 17:10:07 -0700 | [diff] [blame] | 262 | registerForAirplaneMode(filter); |
Dianne Hackborn | d83a096 | 2014-05-02 16:28:33 -0700 | [diff] [blame] | 263 | filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
Matthew Xie | 6fde309 | 2012-07-11 17:10:07 -0700 | [diff] [blame] | 264 | mContext.registerReceiver(mReceiver, filter); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 265 | loadStoredNameAndAddress(); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 266 | if (isBluetoothPersistedStateOn()) { |
| 267 | mEnableExternal = true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 268 | } |
Adrian Roos | bd9a9a5 | 2014-08-18 15:31:57 +0200 | [diff] [blame] | 269 | |
| 270 | int sysUiUid = -1; |
| 271 | try { |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 272 | sysUiUid = mContext.getPackageManager().getPackageUidAsUser("com.android.systemui", |
Jeff Sharkey | c5967e9 | 2016-01-07 18:50:29 -0700 | [diff] [blame] | 273 | PackageManager.MATCH_SYSTEM_ONLY, UserHandle.USER_SYSTEM); |
Adrian Roos | bd9a9a5 | 2014-08-18 15:31:57 +0200 | [diff] [blame] | 274 | } catch (PackageManager.NameNotFoundException e) { |
Joe LaPenna | acddf2b | 2015-09-04 12:52:42 -0700 | [diff] [blame] | 275 | // Some platforms, such as wearables do not have a system ui. |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 276 | Slog.w(TAG, "Unable to resolve SystemUI's UID.", e); |
Adrian Roos | bd9a9a5 | 2014-08-18 15:31:57 +0200 | [diff] [blame] | 277 | } |
| 278 | mSystemUiUid = sysUiUid; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 279 | } |
| 280 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 281 | /** |
| 282 | * Returns true if airplane mode is currently on |
| 283 | */ |
| 284 | private final boolean isAirplaneModeOn() { |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 285 | return Settings.Global.getInt(mContext.getContentResolver(), |
| 286 | Settings.Global.AIRPLANE_MODE_ON, 0) == 1; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Returns true if the Bluetooth saved state is "on" |
| 291 | */ |
| 292 | private final boolean isBluetoothPersistedStateOn() { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 293 | return Settings.Global.getInt(mContentResolver, |
Andre Eisenbach | 8c18431 | 2016-09-06 18:03:10 -0700 | [diff] [blame] | 294 | Settings.Global.BLUETOOTH_ON, BLUETOOTH_ON_BLUETOOTH) != BLUETOOTH_OFF; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Returns true if the Bluetooth saved state is BLUETOOTH_ON_BLUETOOTH |
| 299 | */ |
| 300 | private final boolean isBluetoothPersistedStateOnBluetooth() { |
| 301 | return Settings.Global.getInt(mContentResolver, |
Andre Eisenbach | 8c18431 | 2016-09-06 18:03:10 -0700 | [diff] [blame] | 302 | Settings.Global.BLUETOOTH_ON, BLUETOOTH_ON_BLUETOOTH) == BLUETOOTH_ON_BLUETOOTH; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Save the Bluetooth on/off state |
| 307 | * |
| 308 | */ |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 309 | private void persistBluetoothSetting(int value) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 310 | Settings.Global.putInt(mContext.getContentResolver(), |
| 311 | Settings.Global.BLUETOOTH_ON, |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 312 | value); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Returns true if the Bluetooth Adapter's name and address is |
| 317 | * locally cached |
| 318 | * @return |
| 319 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 320 | private boolean isNameAndAddressSet() { |
| 321 | return mName !=null && mAddress!= null && mName.length()>0 && mAddress.length()>0; |
| 322 | } |
| 323 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 324 | /** |
| 325 | * Retrieve the Bluetooth Adapter's name and address and save it in |
| 326 | * in the local cache |
| 327 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 328 | private void loadStoredNameAndAddress() { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 329 | if (DBG) Slog.d(TAG, "Loading stored name and address"); |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 330 | if (mContext.getResources().getBoolean |
| 331 | (com.android.internal.R.bool.config_bluetooth_address_validation) && |
| 332 | Settings.Secure.getInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 0) == 0) { |
| 333 | // if the valid flag is not set, don't load the address and name |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 334 | if (DBG) Slog.d(TAG, "invalid bluetooth name and address stored"); |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 335 | return; |
| 336 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 337 | mName = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME); |
| 338 | mAddress = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS); |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 339 | if (DBG) Slog.d(TAG, "Stored bluetooth Name=" + mName + ",Address=" + mAddress); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 340 | } |
| 341 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 342 | /** |
| 343 | * Save the Bluetooth name and address in the persistent store. |
| 344 | * Only non-null values will be saved. |
| 345 | * @param name |
| 346 | * @param address |
| 347 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 348 | private void storeNameAndAddress(String name, String address) { |
| 349 | if (name != null) { |
| 350 | Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME, name); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 351 | mName = name; |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 352 | if (DBG) Slog.d(TAG,"Stored Bluetooth name: " + |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 353 | Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_NAME)); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | if (address != null) { |
| 357 | Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS, address); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 358 | mAddress=address; |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 359 | if (DBG) Slog.d(TAG,"Stored Bluetoothaddress: " + |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 360 | Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_ADDRESS)); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 361 | } |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 362 | |
| 363 | if ((name != null) && (address != null)) { |
| 364 | Settings.Secure.putInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 1); |
| 365 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | public IBluetooth registerAdapter(IBluetoothManagerCallback callback){ |
Natalie Silvanovich | 55db646 | 2014-05-01 16:12:23 -0700 | [diff] [blame] | 369 | if (callback == null) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 370 | Slog.w(TAG, "Callback is null in registerAdapter"); |
Natalie Silvanovich | 55db646 | 2014-05-01 16:12:23 -0700 | [diff] [blame] | 371 | return null; |
| 372 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 373 | Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_ADAPTER); |
| 374 | msg.obj = callback; |
| 375 | mHandler.sendMessage(msg); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 376 | |
| 377 | return mBluetooth; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | public void unregisterAdapter(IBluetoothManagerCallback callback) { |
Natalie Silvanovich | 55db646 | 2014-05-01 16:12:23 -0700 | [diff] [blame] | 381 | if (callback == null) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 382 | Slog.w(TAG, "Callback is null in unregisterAdapter"); |
Natalie Silvanovich | 55db646 | 2014-05-01 16:12:23 -0700 | [diff] [blame] | 383 | return; |
| 384 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 385 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 386 | "Need BLUETOOTH permission"); |
| 387 | Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_ADAPTER); |
| 388 | msg.obj = callback; |
| 389 | mHandler.sendMessage(msg); |
| 390 | } |
| 391 | |
| 392 | public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) { |
| 393 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 394 | "Need BLUETOOTH permission"); |
| 395 | Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_STATE_CHANGE_CALLBACK); |
| 396 | msg.obj = callback; |
| 397 | mHandler.sendMessage(msg); |
| 398 | } |
| 399 | |
| 400 | public void unregisterStateChangeCallback(IBluetoothStateChangeCallback callback) { |
| 401 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 402 | "Need BLUETOOTH permission"); |
| 403 | Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK); |
| 404 | msg.obj = callback; |
| 405 | mHandler.sendMessage(msg); |
| 406 | } |
| 407 | |
| 408 | public boolean isEnabled() { |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 409 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 410 | (!checkIfCallerIsForegroundUser())) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 411 | Slog.w(TAG,"isEnabled(): not allowed for non-active and non system user"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 412 | return false; |
| 413 | } |
| 414 | |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 415 | try { |
| 416 | mBluetoothLock.readLock().lock(); |
| 417 | if (mBluetooth != null) return mBluetooth.isEnabled(); |
| 418 | } catch (RemoteException e) { |
| 419 | Slog.e(TAG, "isEnabled()", e); |
| 420 | } finally { |
| 421 | mBluetoothLock.readLock().unlock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 422 | } |
| 423 | return false; |
| 424 | } |
| 425 | |
Christine Hallstrom | 995c90a | 2016-05-25 15:49:08 -0700 | [diff] [blame] | 426 | public int getState() { |
| 427 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 428 | (!checkIfCallerIsForegroundUser())) { |
| 429 | Slog.w(TAG, "getState(): not allowed for non-active and non system user"); |
| 430 | return BluetoothAdapter.STATE_OFF; |
| 431 | } |
| 432 | |
| 433 | try { |
| 434 | mBluetoothLock.readLock().lock(); |
| 435 | if (mBluetooth != null) return mBluetooth.getState(); |
| 436 | } catch (RemoteException e) { |
| 437 | Slog.e(TAG, "getState()", e); |
| 438 | } finally { |
| 439 | mBluetoothLock.readLock().unlock(); |
| 440 | } |
| 441 | return BluetoothAdapter.STATE_OFF; |
| 442 | } |
| 443 | |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 444 | class ClientDeathRecipient implements IBinder.DeathRecipient { |
| 445 | public void binderDied() { |
Marie Janssen | 6a383a7 | 2016-10-25 10:47:51 -0700 | [diff] [blame] | 446 | if (DBG) Slog.d(TAG, "Binder is dead - unregister Ble App"); |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 447 | if (isBleAppPresent()) { |
| 448 | // Nothing to do, another app is here. |
| 449 | return; |
| 450 | } |
| 451 | if (DBG) Slog.d(TAG, "Disabling LE only mode after application crash"); |
| 452 | try { |
| 453 | mBluetoothLock.readLock().lock(); |
| 454 | if (mBluetooth != null && |
| 455 | mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON) { |
| 456 | mEnable = false; |
| 457 | mBluetooth.onBrEdrDown(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 458 | } |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 459 | } catch (RemoteException e) { |
| 460 | Slog.e(TAG,"Unable to call onBrEdrDown", e); |
| 461 | } finally { |
| 462 | mBluetoothLock.readLock().unlock(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | /** Internal death rec list */ |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 468 | Map<IBinder, ClientDeathRecipient> mBleApps = new ConcurrentHashMap<IBinder, ClientDeathRecipient>(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 469 | |
Wei Wang | 67d8416 | 2015-04-26 17:04:29 -0700 | [diff] [blame] | 470 | @Override |
| 471 | public boolean isBleScanAlwaysAvailable() { |
Marie Janssen | 6a383a7 | 2016-10-25 10:47:51 -0700 | [diff] [blame] | 472 | if (isAirplaneModeOn() && !mEnable) { |
| 473 | return false; |
| 474 | } |
Wei Wang | 67d8416 | 2015-04-26 17:04:29 -0700 | [diff] [blame] | 475 | try { |
| 476 | return (Settings.Global.getInt(mContentResolver, |
| 477 | Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE)) != 0; |
| 478 | } catch (SettingNotFoundException e) { |
| 479 | } |
| 480 | return false; |
| 481 | } |
| 482 | |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 483 | // Monitor change of BLE scan only mode settings. |
| 484 | private void registerForBleScanModeChange() { |
| 485 | ContentObserver contentObserver = new ContentObserver(null) { |
| 486 | @Override |
| 487 | public void onChange(boolean selfChange) { |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 488 | if (isBleScanAlwaysAvailable()) { |
| 489 | // Nothing to do |
| 490 | return; |
| 491 | } |
| 492 | // BLE scan is not available. |
| 493 | disableBleScanMode(); |
| 494 | clearBleApps(); |
| 495 | try { |
| 496 | mBluetoothLock.readLock().lock(); |
| 497 | if (mBluetooth != null) mBluetooth.onBrEdrDown(); |
| 498 | } catch (RemoteException e) { |
| 499 | Slog.e(TAG, "error when disabling bluetooth", e); |
| 500 | } finally { |
| 501 | mBluetoothLock.readLock().unlock(); |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | }; |
| 505 | |
| 506 | mContentResolver.registerContentObserver( |
| 507 | Settings.Global.getUriFor(Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE), |
| 508 | false, contentObserver); |
| 509 | } |
| 510 | |
| 511 | // Disable ble scan only mode. |
| 512 | private void disableBleScanMode() { |
| 513 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 514 | mBluetoothLock.writeLock().lock(); |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 515 | if (mBluetooth != null && (mBluetooth.getState() != BluetoothAdapter.STATE_ON)) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 516 | if (DBG) Slog.d(TAG, "Reseting the mEnable flag for clean disable"); |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 517 | mEnable = false; |
| 518 | } |
| 519 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 520 | Slog.e(TAG, "getState()", e); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 521 | } finally { |
| 522 | mBluetoothLock.writeLock().unlock(); |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 526 | public int updateBleAppCount(IBinder token, boolean enable) { |
| 527 | if (enable) { |
| 528 | ClientDeathRecipient r = mBleApps.get(token); |
| 529 | if (r == null) { |
| 530 | ClientDeathRecipient deathRec = new ClientDeathRecipient(); |
| 531 | try { |
| 532 | token.linkToDeath(deathRec, 0); |
| 533 | } catch (RemoteException ex) { |
| 534 | throw new IllegalArgumentException("Wake lock is already dead."); |
| 535 | } |
| 536 | mBleApps.put(token, deathRec); |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 537 | if (DBG) Slog.d(TAG, "Registered for death Notification"); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | } else { |
| 541 | ClientDeathRecipient r = mBleApps.get(token); |
| 542 | if (r != null) { |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 543 | // Unregister death recipient as the app goes away. |
| 544 | token.unlinkToDeath(r, 0); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 545 | mBleApps.remove(token); |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 546 | if (DBG) Slog.d(TAG, "Unregistered for death Notification"); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 547 | } |
| 548 | } |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 549 | int appCount = mBleApps.size(); |
| 550 | if (DBG) Slog.d(TAG, appCount + " registered Ble Apps"); |
| 551 | if (appCount == 0 && mEnable) { |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 552 | disableBleScanMode(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 553 | } |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 554 | return appCount; |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 555 | } |
| 556 | |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 557 | // Clear all apps using BLE scan only mode. |
| 558 | private void clearBleApps() { |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 559 | mBleApps.clear(); |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 562 | /** @hide*/ |
| 563 | public boolean isBleAppPresent() { |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 564 | if (DBG) Slog.d(TAG, "isBleAppPresent() count: " + mBleApps.size()); |
| 565 | return mBleApps.size() > 0; |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /** |
| 569 | * Action taken when GattService is turned off |
| 570 | */ |
| 571 | private void onBluetoothGattServiceUp() { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 572 | if (DBG) Slog.d(TAG,"BluetoothGatt Service is Up"); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 573 | try { |
| 574 | mBluetoothLock.readLock().lock(); |
Nitin Arora | bdfaa7f | 2015-04-29 12:35:03 -0700 | [diff] [blame] | 575 | if (isBleAppPresent() == false && mBluetooth != null |
| 576 | && mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 577 | mBluetooth.onLeServiceUp(); |
| 578 | |
| 579 | // waive WRITE_SECURE_SETTINGS permission check |
| 580 | long callingIdentity = Binder.clearCallingIdentity(); |
| 581 | persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH); |
| 582 | Binder.restoreCallingIdentity(callingIdentity); |
| 583 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 584 | } catch (RemoteException e) { |
| 585 | Slog.e(TAG,"Unable to call onServiceUp", e); |
| 586 | } finally { |
| 587 | mBluetoothLock.readLock().unlock(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * Inform BluetoothAdapter instances that BREDR part is down |
| 593 | * and turn off all service and stack if no LE app needs it |
| 594 | */ |
| 595 | private void sendBrEdrDownCallback() { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 596 | if (DBG) Slog.d(TAG,"Calling sendBrEdrDownCallback callbacks"); |
Nitin Arora | bdfaa7f | 2015-04-29 12:35:03 -0700 | [diff] [blame] | 597 | |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 598 | if (mBluetooth == null) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 599 | Slog.w(TAG, "Bluetooth handle is null"); |
Nitin Arora | bdfaa7f | 2015-04-29 12:35:03 -0700 | [diff] [blame] | 600 | return; |
| 601 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 602 | |
| 603 | if (isBleAppPresent() == false) { |
| 604 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 605 | mBluetoothLock.readLock().lock(); |
| 606 | if (mBluetooth != null) mBluetooth.onBrEdrDown(); |
| 607 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 608 | Slog.e(TAG, "Call to onBrEdrDown() failed.", e); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 609 | } finally { |
| 610 | mBluetoothLock.readLock().unlock(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 611 | } |
Nitin Arora | bdfaa7f | 2015-04-29 12:35:03 -0700 | [diff] [blame] | 612 | } else { |
| 613 | // Need to stay at BLE ON. Disconnect all Gatt connections |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 614 | try { |
Nitin Arora | bdfaa7f | 2015-04-29 12:35:03 -0700 | [diff] [blame] | 615 | mBluetoothGatt.unregAll(); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 616 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 617 | Slog.e(TAG, "Unable to disconnect all apps.", e); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 618 | } |
| 619 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 620 | } |
| 621 | |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 622 | public boolean enableNoAutoConnect() |
| 623 | { |
| 624 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 625 | "Need BLUETOOTH ADMIN permission"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 626 | |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 627 | if (DBG) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 628 | Slog.d(TAG,"enableNoAutoConnect(): mBluetooth =" + mBluetooth + |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 629 | " mBinding = " + mBinding); |
| 630 | } |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 631 | int callingAppId = UserHandle.getAppId(Binder.getCallingUid()); |
| 632 | |
| 633 | if (callingAppId != Process.NFC_UID) { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 634 | throw new SecurityException("no permission to enable Bluetooth quietly"); |
| 635 | } |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 636 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 637 | synchronized(mReceiver) { |
| 638 | mQuietEnableExternal = true; |
| 639 | mEnableExternal = true; |
| 640 | sendEnableMsg(true); |
| 641 | } |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 642 | return true; |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 643 | } |
Ajay Panicker | 4bb4830 | 2016-03-31 14:14:27 -0700 | [diff] [blame] | 644 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 645 | public boolean enable() { |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 646 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 647 | (!checkIfCallerIsForegroundUser())) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 648 | Slog.w(TAG,"enable(): not allowed for non-active and non system user"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 649 | return false; |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 652 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 653 | "Need BLUETOOTH ADMIN permission"); |
| 654 | if (DBG) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 655 | Slog.d(TAG,"enable(): mBluetooth =" + mBluetooth + |
Sanket Agarwal | 090bf55 | 2016-04-21 14:10:55 -0700 | [diff] [blame] | 656 | " mBinding = " + mBinding + " mState = " + mState); |
| 657 | } |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 658 | |
| 659 | synchronized(mReceiver) { |
| 660 | mQuietEnableExternal = false; |
| 661 | mEnableExternal = true; |
| 662 | // waive WRITE_SECURE_SETTINGS permission check |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 663 | sendEnableMsg(false); |
| 664 | } |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 665 | if (DBG) Slog.d(TAG, "enable returning"); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 666 | return true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | public boolean disable(boolean persist) { |
| 670 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 671 | "Need BLUETOOTH ADMIN permissicacheNameAndAddresson"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 672 | |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 673 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 674 | (!checkIfCallerIsForegroundUser())) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 675 | Slog.w(TAG,"disable(): not allowed for non-active and non system user"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 676 | return false; |
| 677 | } |
| 678 | |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 679 | if (DBG) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 680 | Slog.d(TAG,"disable(): mBluetooth = " + mBluetooth + |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 681 | " mBinding = " + mBinding); |
| 682 | } |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 683 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 684 | synchronized(mReceiver) { |
| 685 | if (persist) { |
| 686 | // waive WRITE_SECURE_SETTINGS permission check |
| 687 | long callingIdentity = Binder.clearCallingIdentity(); |
| 688 | persistBluetoothSetting(BLUETOOTH_OFF); |
| 689 | Binder.restoreCallingIdentity(callingIdentity); |
| 690 | } |
| 691 | mEnableExternal = false; |
| 692 | sendDisableMsg(); |
| 693 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 694 | return true; |
| 695 | } |
| 696 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 697 | public void unbindAndFinish() { |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 698 | if (DBG) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 699 | Slog.d(TAG,"unbindAndFinish(): " + mBluetooth + |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 700 | " mBinding = " + mBinding); |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 703 | try { |
| 704 | mBluetoothLock.writeLock().lock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 705 | if (mUnbinding) return; |
| 706 | mUnbinding = true; |
Pavlin Radoslavov | e47ec14 | 2016-06-01 22:25:18 -0700 | [diff] [blame] | 707 | mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE); |
Pavlin Radoslavov | 74f60c0 | 2016-09-21 17:28:11 -0700 | [diff] [blame] | 708 | mHandler.removeMessages(MESSAGE_BIND_PROFILE_SERVICE); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 709 | if (mBluetooth != null) { |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 710 | //Unregister callback object |
| 711 | try { |
| 712 | mBluetooth.unregisterCallback(mBluetoothCallback); |
| 713 | } catch (RemoteException re) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 714 | Slog.e(TAG, "Unable to unregister BluetoothCallback",re); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 715 | } |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 716 | |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 717 | if (DBG) Slog.d(TAG, "Sending unbind request."); |
Marie Janssen | 9db28eb | 2016-01-12 16:05:15 -0800 | [diff] [blame] | 718 | mBluetoothBinder = null; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 719 | mBluetooth = null; |
| 720 | //Unbind |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 721 | mContext.unbindService(mConnection); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 722 | mUnbinding = false; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 723 | mBinding = false; |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 724 | } else { |
| 725 | mUnbinding=false; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 726 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 727 | mBluetoothGatt = null; |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 728 | } finally { |
| 729 | mBluetoothLock.writeLock().unlock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 733 | public IBluetoothGatt getBluetoothGatt() { |
| 734 | // sync protection |
| 735 | return mBluetoothGatt; |
| 736 | } |
| 737 | |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 738 | @Override |
| 739 | public boolean bindBluetoothProfileService(int bluetoothProfile, |
| 740 | IBluetoothProfileServiceConnection proxy) { |
| 741 | if (!mEnable) { |
| 742 | if (DBG) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 743 | Slog.d(TAG, "Trying to bind to profile: " + bluetoothProfile + |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 744 | ", while Bluetooth was disabled"); |
| 745 | } |
| 746 | return false; |
| 747 | } |
| 748 | synchronized (mProfileServices) { |
| 749 | ProfileServiceConnections psc = mProfileServices.get(new Integer(bluetoothProfile)); |
| 750 | if (psc == null) { |
| 751 | if (DBG) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 752 | Slog.d(TAG, "Creating new ProfileServiceConnections object for" |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 753 | + " profile: " + bluetoothProfile); |
| 754 | } |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 755 | |
| 756 | if (bluetoothProfile != BluetoothProfile.HEADSET) return false; |
| 757 | |
| 758 | Intent intent = new Intent(IBluetoothHeadset.class.getName()); |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 759 | psc = new ProfileServiceConnections(intent); |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 760 | if (!psc.bindService()) return false; |
| 761 | |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 762 | mProfileServices.put(new Integer(bluetoothProfile), psc); |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | |
| 766 | // Introducing a delay to give the client app time to prepare |
| 767 | Message addProxyMsg = mHandler.obtainMessage(MESSAGE_ADD_PROXY_DELAYED); |
| 768 | addProxyMsg.arg1 = bluetoothProfile; |
| 769 | addProxyMsg.obj = proxy; |
| 770 | mHandler.sendMessageDelayed(addProxyMsg, ADD_PROXY_DELAY_MS); |
| 771 | return true; |
| 772 | } |
| 773 | |
| 774 | @Override |
| 775 | public void unbindBluetoothProfileService(int bluetoothProfile, |
| 776 | IBluetoothProfileServiceConnection proxy) { |
| 777 | synchronized (mProfileServices) { |
| 778 | ProfileServiceConnections psc = mProfileServices.get(new Integer(bluetoothProfile)); |
| 779 | if (psc == null) { |
| 780 | return; |
| 781 | } |
| 782 | psc.removeProxy(proxy); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | private void unbindAllBluetoothProfileServices() { |
| 787 | synchronized (mProfileServices) { |
| 788 | for (Integer i : mProfileServices.keySet()) { |
| 789 | ProfileServiceConnections psc = mProfileServices.get(i); |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 790 | try { |
| 791 | mContext.unbindService(psc); |
| 792 | } catch (IllegalArgumentException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 793 | Slog.e(TAG, "Unable to unbind service with intent: " + psc.mIntent, e); |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 794 | } |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 795 | psc.removeAllProxies(); |
| 796 | } |
| 797 | mProfileServices.clear(); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | /** |
Miao Chou | 658bf2f | 2015-06-26 17:14:35 -0700 | [diff] [blame] | 802 | * Send enable message and set adapter name and address. Called when the boot phase becomes |
| 803 | * PHASE_SYSTEM_SERVICES_READY. |
| 804 | */ |
| 805 | public void handleOnBootPhase() { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 806 | if (DBG) Slog.d(TAG, "Bluetooth boot completed"); |
Miao Chou | 658bf2f | 2015-06-26 17:14:35 -0700 | [diff] [blame] | 807 | if (mEnableExternal && isBluetoothPersistedStateOnBluetooth()) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 808 | if (DBG) Slog.d(TAG, "Auto-enabling Bluetooth."); |
Miao Chou | 658bf2f | 2015-06-26 17:14:35 -0700 | [diff] [blame] | 809 | sendEnableMsg(mQuietEnableExternal); |
Ajay Panicker | bf796d8 | 2016-03-11 13:47:20 -0800 | [diff] [blame] | 810 | } else if (!isNameAndAddressSet()) { |
| 811 | if (DBG) Slog.d(TAG, "Getting adapter name and address"); |
Ajay Panicker | 4bb4830 | 2016-03-31 14:14:27 -0700 | [diff] [blame] | 812 | Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS); |
| 813 | mHandler.sendMessage(getMsg); |
Miao Chou | 658bf2f | 2015-06-26 17:14:35 -0700 | [diff] [blame] | 814 | } |
Miao Chou | 658bf2f | 2015-06-26 17:14:35 -0700 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | /** |
| 818 | * Called when switching to a different foreground user. |
| 819 | */ |
| 820 | public void handleOnSwitchUser(int userHandle) { |
Jeff Sharkey | aacb89e | 2016-03-05 14:42:58 -0700 | [diff] [blame] | 821 | if (DBG) Slog.d(TAG, "User " + userHandle + " switched"); |
| 822 | mHandler.obtainMessage(MESSAGE_USER_SWITCHED, userHandle, 0).sendToTarget(); |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * Called when user is unlocked. |
| 827 | */ |
| 828 | public void handleOnUnlockUser(int userHandle) { |
| 829 | if (DBG) Slog.d(TAG, "User " + userHandle + " unlocked"); |
| 830 | mHandler.obtainMessage(MESSAGE_USER_UNLOCKED, userHandle, 0).sendToTarget(); |
Miao Chou | 658bf2f | 2015-06-26 17:14:35 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | /** |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 834 | * This class manages the clients connected to a given ProfileService |
| 835 | * and maintains the connection with that service. |
| 836 | */ |
| 837 | final private class ProfileServiceConnections implements ServiceConnection, |
| 838 | IBinder.DeathRecipient { |
| 839 | final RemoteCallbackList<IBluetoothProfileServiceConnection> mProxies = |
| 840 | new RemoteCallbackList <IBluetoothProfileServiceConnection>(); |
| 841 | IBinder mService; |
| 842 | ComponentName mClassName; |
| 843 | Intent mIntent; |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 844 | boolean mInvokingProxyCallbacks = false; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 845 | |
| 846 | ProfileServiceConnections(Intent intent) { |
| 847 | mService = null; |
| 848 | mClassName = null; |
| 849 | mIntent = intent; |
| 850 | } |
| 851 | |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 852 | private boolean bindService() { |
| 853 | if (mIntent != null && mService == null && |
| 854 | doBind(mIntent, this, 0, UserHandle.CURRENT_OR_SELF)) { |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 855 | Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE); |
| 856 | msg.obj = this; |
| 857 | mHandler.sendMessageDelayed(msg, TIMEOUT_BIND_MS); |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 858 | return true; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 859 | } |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 860 | Slog.w(TAG, "Unable to bind with intent: " + mIntent); |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 861 | return false; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | private void addProxy(IBluetoothProfileServiceConnection proxy) { |
| 865 | mProxies.register(proxy); |
| 866 | if (mService != null) { |
| 867 | try{ |
| 868 | proxy.onServiceConnected(mClassName, mService); |
| 869 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 870 | Slog.e(TAG, "Unable to connect to proxy", e); |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 871 | } |
| 872 | } else { |
| 873 | if (!mHandler.hasMessages(MESSAGE_BIND_PROFILE_SERVICE, this)) { |
| 874 | Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE); |
| 875 | msg.obj = this; |
| 876 | mHandler.sendMessage(msg); |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | private void removeProxy(IBluetoothProfileServiceConnection proxy) { |
| 882 | if (proxy != null) { |
| 883 | if (mProxies.unregister(proxy)) { |
| 884 | try { |
| 885 | proxy.onServiceDisconnected(mClassName); |
| 886 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 887 | Slog.e(TAG, "Unable to disconnect proxy", e); |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | } else { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 891 | Slog.w(TAG, "Trying to remove a null proxy"); |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 892 | } |
| 893 | } |
| 894 | |
| 895 | private void removeAllProxies() { |
| 896 | onServiceDisconnected(mClassName); |
| 897 | mProxies.kill(); |
| 898 | } |
| 899 | |
| 900 | @Override |
| 901 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 902 | // remove timeout message |
| 903 | mHandler.removeMessages(MESSAGE_BIND_PROFILE_SERVICE, this); |
| 904 | mService = service; |
| 905 | mClassName = className; |
| 906 | try { |
| 907 | mService.linkToDeath(this, 0); |
| 908 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 909 | Slog.e(TAG, "Unable to linkToDeath", e); |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 910 | } |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 911 | |
| 912 | if (mInvokingProxyCallbacks) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 913 | Slog.e(TAG, "Proxy callbacks already in progress."); |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 914 | return; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 915 | } |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 916 | mInvokingProxyCallbacks = true; |
| 917 | |
| 918 | final int n = mProxies.beginBroadcast(); |
| 919 | try { |
| 920 | for (int i = 0; i < n; i++) { |
| 921 | try { |
| 922 | mProxies.getBroadcastItem(i).onServiceConnected(className, service); |
| 923 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 924 | Slog.e(TAG, "Unable to connect to proxy", e); |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | } finally { |
| 928 | mProxies.finishBroadcast(); |
| 929 | mInvokingProxyCallbacks = false; |
| 930 | } |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | @Override |
| 934 | public void onServiceDisconnected(ComponentName className) { |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 935 | if (mService == null) return; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 936 | mService.unlinkToDeath(this, 0); |
| 937 | mService = null; |
| 938 | mClassName = null; |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 939 | |
| 940 | if (mInvokingProxyCallbacks) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 941 | Slog.e(TAG, "Proxy callbacks already in progress."); |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 942 | return; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 943 | } |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 944 | mInvokingProxyCallbacks = true; |
| 945 | |
| 946 | final int n = mProxies.beginBroadcast(); |
| 947 | try { |
| 948 | for (int i = 0; i < n; i++) { |
| 949 | try { |
| 950 | mProxies.getBroadcastItem(i).onServiceDisconnected(className); |
| 951 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 952 | Slog.e(TAG, "Unable to disconnect from proxy", e); |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | } finally { |
| 956 | mProxies.finishBroadcast(); |
| 957 | mInvokingProxyCallbacks = false; |
| 958 | } |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | @Override |
| 962 | public void binderDied() { |
| 963 | if (DBG) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 964 | Slog.w(TAG, "Profile service for profile: " + mClassName |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 965 | + " died."); |
| 966 | } |
| 967 | onServiceDisconnected(mClassName); |
| 968 | // Trigger rebind |
| 969 | Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE); |
| 970 | msg.obj = this; |
| 971 | mHandler.sendMessageDelayed(msg, TIMEOUT_BIND_MS); |
| 972 | } |
| 973 | } |
| 974 | |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 975 | private void sendBluetoothStateCallback(boolean isUp) { |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 976 | try { |
| 977 | int n = mStateChangeCallbacks.beginBroadcast(); |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 978 | if (DBG) Slog.d(TAG,"Broadcasting onBluetoothStateChange("+isUp+") to " + n + " receivers."); |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 979 | for (int i=0; i <n;i++) { |
| 980 | try { |
| 981 | mStateChangeCallbacks.getBroadcastItem(i).onBluetoothStateChange(isUp); |
| 982 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 983 | Slog.e(TAG, "Unable to call onBluetoothStateChange() on callback #" + i , e); |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 984 | } |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 985 | } |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 986 | } finally { |
| 987 | mStateChangeCallbacks.finishBroadcast(); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 988 | } |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | /** |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 992 | * Inform BluetoothAdapter instances that Adapter service is up |
| 993 | */ |
| 994 | private void sendBluetoothServiceUpCallback() { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 995 | if (DBG) Slog.d(TAG,"Calling onBluetoothServiceUp callbacks"); |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 996 | try { |
| 997 | int n = mCallbacks.beginBroadcast(); |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 998 | Slog.d(TAG,"Broadcasting onBluetoothServiceUp() to " + n + " receivers."); |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 999 | for (int i=0; i <n;i++) { |
| 1000 | try { |
| 1001 | mCallbacks.getBroadcastItem(i).onBluetoothServiceUp(mBluetooth); |
| 1002 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1003 | Slog.e(TAG, "Unable to call onBluetoothServiceUp() on callback #" + i, e); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1004 | } |
| 1005 | } |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1006 | } finally { |
| 1007 | mCallbacks.finishBroadcast(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | /** |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1011 | * Inform BluetoothAdapter instances that Adapter service is down |
| 1012 | */ |
| 1013 | private void sendBluetoothServiceDownCallback() { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1014 | if (DBG) Slog.d(TAG,"Calling onBluetoothServiceDown callbacks"); |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1015 | try { |
| 1016 | int n = mCallbacks.beginBroadcast(); |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1017 | Slog.d(TAG,"Broadcasting onBluetoothServiceDown() to " + n + " receivers."); |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1018 | for (int i=0; i <n;i++) { |
| 1019 | try { |
| 1020 | mCallbacks.getBroadcastItem(i).onBluetoothServiceDown(); |
| 1021 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1022 | Slog.e(TAG, "Unable to call onBluetoothServiceDown() on callback #" + i, e); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 1023 | } |
| 1024 | } |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1025 | } finally { |
| 1026 | mCallbacks.finishBroadcast(); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 1027 | } |
| 1028 | } |
Svet Ganov | 408abf7 | 2015-05-12 19:13:36 -0700 | [diff] [blame] | 1029 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1030 | public String getAddress() { |
Matthew Xie | af5ddbf | 2012-12-04 10:47:43 -0800 | [diff] [blame] | 1031 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
Svet Ganov | 408abf7 | 2015-05-12 19:13:36 -0700 | [diff] [blame] | 1032 | "Need BLUETOOTH permission"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1033 | |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 1034 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
Svet Ganov | 408abf7 | 2015-05-12 19:13:36 -0700 | [diff] [blame] | 1035 | (!checkIfCallerIsForegroundUser())) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1036 | Slog.w(TAG,"getAddress(): not allowed for non-active and non system user"); |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 1037 | return null; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
Svet Ganov | 408abf7 | 2015-05-12 19:13:36 -0700 | [diff] [blame] | 1040 | if (mContext.checkCallingOrSelfPermission(Manifest.permission.LOCAL_MAC_ADDRESS) |
| 1041 | != PackageManager.PERMISSION_GRANTED) { |
| 1042 | return BluetoothAdapter.DEFAULT_MAC_ADDRESS; |
| 1043 | } |
| 1044 | |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1045 | try { |
| 1046 | mBluetoothLock.readLock().lock(); |
| 1047 | if (mBluetooth != null) return mBluetooth.getAddress(); |
| 1048 | } catch (RemoteException e) { |
| 1049 | Slog.e(TAG, "getAddress(): Unable to retrieve address remotely. Returning cached address", e); |
| 1050 | } finally { |
| 1051 | mBluetoothLock.readLock().unlock(); |
fredc | 116d1d46 | 2012-04-20 14:47:08 -0700 | [diff] [blame] | 1052 | } |
Ajay Panicker | bf796d8 | 2016-03-11 13:47:20 -0800 | [diff] [blame] | 1053 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1054 | // mAddress is accessed from outside. |
| 1055 | // It is alright without a lock. Here, bluetooth is off, no other thread is |
| 1056 | // changing mAddress |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1057 | return mAddress; |
| 1058 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1059 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1060 | public String getName() { |
Matthew Xie | af5ddbf | 2012-12-04 10:47:43 -0800 | [diff] [blame] | 1061 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 1062 | "Need BLUETOOTH permission"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1063 | |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 1064 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 1065 | (!checkIfCallerIsForegroundUser())) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1066 | Slog.w(TAG,"getName(): not allowed for non-active and non system user"); |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 1067 | return null; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1070 | try { |
| 1071 | mBluetoothLock.readLock().lock(); |
| 1072 | if (mBluetooth != null) return mBluetooth.getName(); |
| 1073 | } catch (RemoteException e) { |
| 1074 | Slog.e(TAG, "getName(): Unable to retrieve name remotely. Returning cached name", e); |
| 1075 | } finally { |
| 1076 | mBluetoothLock.readLock().unlock(); |
fredc | 116d1d46 | 2012-04-20 14:47:08 -0700 | [diff] [blame] | 1077 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1078 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1079 | // mName is accessed from outside. |
| 1080 | // It alright without a lock. Here, bluetooth is off, no other thread is |
| 1081 | // changing mName |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1082 | return mName; |
| 1083 | } |
| 1084 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1085 | private class BluetoothServiceConnection implements ServiceConnection { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1086 | public void onServiceConnected(ComponentName className, IBinder service) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1087 | if (DBG) Slog.d(TAG, "BluetoothServiceConnection: " + className.getClassName()); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1088 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1089 | // TBD if (className.getClassName().equals(IBluetooth.class.getName())) { |
| 1090 | if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) { |
| 1091 | msg.arg1 = SERVICE_IBLUETOOTH; |
| 1092 | // } else if (className.getClassName().equals(IBluetoothGatt.class.getName())) { |
| 1093 | } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) { |
| 1094 | msg.arg1 = SERVICE_IBLUETOOTHGATT; |
| 1095 | } else { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1096 | Slog.e(TAG, "Unknown service connected: " + className.getClassName()); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1097 | return; |
| 1098 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1099 | msg.obj = service; |
| 1100 | mHandler.sendMessage(msg); |
| 1101 | } |
| 1102 | |
| 1103 | public void onServiceDisconnected(ComponentName className) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1104 | // Called if we unexpected disconnected. |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1105 | if (DBG) Slog.d(TAG, "BluetoothServiceConnection, disconnected: " + |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1106 | className.getClassName()); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1107 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1108 | if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) { |
| 1109 | msg.arg1 = SERVICE_IBLUETOOTH; |
| 1110 | } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) { |
| 1111 | msg.arg1 = SERVICE_IBLUETOOTHGATT; |
| 1112 | } else { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1113 | Slog.e(TAG, "Unknown service disconnected: " + className.getClassName()); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1114 | return; |
| 1115 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1116 | mHandler.sendMessage(msg); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | private BluetoothServiceConnection mConnection = new BluetoothServiceConnection(); |
| 1121 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1122 | private class BluetoothHandler extends Handler { |
Ajay Panicker | 4bb4830 | 2016-03-31 14:14:27 -0700 | [diff] [blame] | 1123 | boolean mGetNameAddressOnly = false; |
| 1124 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1125 | public BluetoothHandler(Looper looper) { |
| 1126 | super(looper); |
| 1127 | } |
| 1128 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1129 | @Override |
| 1130 | public void handleMessage(Message msg) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1131 | if (DBG) Slog.d (TAG, "Message: " + msg.what); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1132 | switch (msg.what) { |
Ajay Panicker | 4bb4830 | 2016-03-31 14:14:27 -0700 | [diff] [blame] | 1133 | case MESSAGE_GET_NAME_AND_ADDRESS: |
| 1134 | if (DBG) Slog.d(TAG, "MESSAGE_GET_NAME_AND_ADDRESS"); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1135 | try { |
| 1136 | mBluetoothLock.writeLock().lock(); |
Ajay Panicker | 4bb4830 | 2016-03-31 14:14:27 -0700 | [diff] [blame] | 1137 | if ((mBluetooth == null) && (!mBinding)) { |
| 1138 | if (DBG) Slog.d(TAG, "Binding to service to get name and address"); |
| 1139 | mGetNameAddressOnly = true; |
| 1140 | Message timeoutMsg = mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND); |
| 1141 | mHandler.sendMessageDelayed(timeoutMsg, TIMEOUT_BIND_MS); |
| 1142 | Intent i = new Intent(IBluetooth.class.getName()); |
| 1143 | if (!doBind(i, mConnection, |
| 1144 | Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, |
| 1145 | UserHandle.CURRENT)) { |
| 1146 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
| 1147 | } else { |
| 1148 | mBinding = true; |
| 1149 | } |
| 1150 | } else if (mBluetooth != null) { |
| 1151 | try { |
| 1152 | storeNameAndAddress(mBluetooth.getName(), |
| 1153 | mBluetooth.getAddress()); |
| 1154 | } catch (RemoteException re) { |
| 1155 | Slog.e(TAG, "Unable to grab names", re); |
| 1156 | } |
| 1157 | if (mGetNameAddressOnly && !mEnable) { |
| 1158 | unbindAndFinish(); |
| 1159 | } |
| 1160 | mGetNameAddressOnly = false; |
| 1161 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1162 | } finally { |
| 1163 | mBluetoothLock.writeLock().unlock(); |
Ajay Panicker | 4bb4830 | 2016-03-31 14:14:27 -0700 | [diff] [blame] | 1164 | } |
| 1165 | break; |
| 1166 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1167 | case MESSAGE_ENABLE: |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 1168 | if (DBG) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1169 | Slog.d(TAG, "MESSAGE_ENABLE: mBluetooth = " + mBluetooth); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1170 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1171 | mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 1172 | mEnable = true; |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1173 | |
| 1174 | // Use service interface to get the exact state |
| 1175 | try { |
| 1176 | mBluetoothLock.readLock().lock(); |
| 1177 | if (mBluetooth != null) { |
| 1178 | int state = mBluetooth.getState(); |
| 1179 | if (state == BluetoothAdapter.STATE_BLE_ON) { |
| 1180 | Slog.w(TAG, "BT is in BLE_ON State"); |
| 1181 | mBluetooth.onLeServiceUp(); |
| 1182 | break; |
| 1183 | } |
| 1184 | } |
| 1185 | } catch (RemoteException e) { |
| 1186 | Slog.e(TAG, "", e); |
| 1187 | } finally { |
| 1188 | mBluetoothLock.readLock().unlock(); |
| 1189 | } |
| 1190 | |
| 1191 | mQuietEnable = (msg.arg1 == 1); |
Pavlin Radoslavov | e47ec14 | 2016-06-01 22:25:18 -0700 | [diff] [blame] | 1192 | if (mBluetooth == null) { |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1193 | handleEnable(mQuietEnable); |
Pavlin Radoslavov | e47ec14 | 2016-06-01 22:25:18 -0700 | [diff] [blame] | 1194 | } else { |
| 1195 | // |
| 1196 | // We need to wait until transitioned to STATE_OFF and |
| 1197 | // the previous Bluetooth process has exited. The |
| 1198 | // waiting period has three components: |
| 1199 | // (a) Wait until the local state is STATE_OFF. This |
| 1200 | // is accomplished by "waitForOnOff(false, true)". |
| 1201 | // (b) Wait until the STATE_OFF state is updated to |
| 1202 | // all components. |
| 1203 | // (c) Wait until the Bluetooth process exits, and |
| 1204 | // ActivityManager detects it. |
| 1205 | // The waiting for (b) and (c) is accomplished by |
| 1206 | // delaying the MESSAGE_RESTART_BLUETOOTH_SERVICE |
| 1207 | // message. On slower devices, that delay needs to be |
| 1208 | // on the order of (2 * SERVICE_RESTART_TIME_MS). |
| 1209 | // |
| 1210 | waitForOnOff(false, true); |
Pavlin Radoslavov | e47ec14 | 2016-06-01 22:25:18 -0700 | [diff] [blame] | 1211 | Message restartMsg = mHandler.obtainMessage( |
| 1212 | MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 1213 | mHandler.sendMessageDelayed(restartMsg, |
| 1214 | 2 * SERVICE_RESTART_TIME_MS); |
| 1215 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1216 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1217 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1218 | case MESSAGE_DISABLE: |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1219 | mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 1220 | if (mEnable && mBluetooth != null) { |
| 1221 | waitForOnOff(true, false); |
| 1222 | mEnable = false; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1223 | handleDisable(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1224 | waitForOnOff(false, false); |
| 1225 | } else { |
| 1226 | mEnable = false; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1227 | handleDisable(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1228 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1229 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1230 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1231 | case MESSAGE_REGISTER_ADAPTER: |
| 1232 | { |
| 1233 | IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 1234 | boolean added = mCallbacks.register(callback); |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1235 | Slog.d(TAG,"Added callback: " + (callback == null? "null": callback) +":" +added ); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1236 | } |
| 1237 | break; |
| 1238 | case MESSAGE_UNREGISTER_ADAPTER: |
| 1239 | { |
| 1240 | IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 1241 | boolean removed = mCallbacks.unregister(callback); |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1242 | Slog.d(TAG,"Removed callback: " + (callback == null? "null": callback) +":" + removed); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1243 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1244 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1245 | case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK: |
| 1246 | { |
| 1247 | IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj; |
Matthew Xie | 9b69399 | 2013-10-10 11:21:40 -0700 | [diff] [blame] | 1248 | if (callback != null) { |
| 1249 | mStateChangeCallbacks.register(callback); |
| 1250 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1251 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1252 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1253 | case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK: |
| 1254 | { |
| 1255 | IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj; |
Matthew Xie | 9b69399 | 2013-10-10 11:21:40 -0700 | [diff] [blame] | 1256 | if (callback != null) { |
| 1257 | mStateChangeCallbacks.unregister(callback); |
| 1258 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1259 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1260 | } |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1261 | case MESSAGE_ADD_PROXY_DELAYED: |
| 1262 | { |
| 1263 | ProfileServiceConnections psc = mProfileServices.get( |
| 1264 | new Integer(msg.arg1)); |
| 1265 | if (psc == null) { |
| 1266 | break; |
| 1267 | } |
| 1268 | IBluetoothProfileServiceConnection proxy = |
| 1269 | (IBluetoothProfileServiceConnection) msg.obj; |
| 1270 | psc.addProxy(proxy); |
| 1271 | break; |
| 1272 | } |
| 1273 | case MESSAGE_BIND_PROFILE_SERVICE: |
| 1274 | { |
| 1275 | ProfileServiceConnections psc = (ProfileServiceConnections) msg.obj; |
| 1276 | removeMessages(MESSAGE_BIND_PROFILE_SERVICE, msg.obj); |
| 1277 | if (psc == null) { |
| 1278 | break; |
| 1279 | } |
| 1280 | psc.bindService(); |
| 1281 | break; |
| 1282 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1283 | case MESSAGE_BLUETOOTH_SERVICE_CONNECTED: |
| 1284 | { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1285 | if (DBG) Slog.d(TAG,"MESSAGE_BLUETOOTH_SERVICE_CONNECTED: " + msg.arg1); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1286 | |
| 1287 | IBinder service = (IBinder) msg.obj; |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1288 | try { |
| 1289 | mBluetoothLock.writeLock().lock(); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1290 | if (msg.arg1 == SERVICE_IBLUETOOTHGATT) { |
| 1291 | mBluetoothGatt = IBluetoothGatt.Stub.asInterface(service); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1292 | onBluetoothGattServiceUp(); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1293 | break; |
| 1294 | } // else must be SERVICE_IBLUETOOTH |
| 1295 | |
| 1296 | //Remove timeout |
Zhihai Xu | af5971e | 2013-06-10 20:28:31 -0700 | [diff] [blame] | 1297 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1298 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1299 | mBinding = false; |
Marie Janssen | 9db28eb | 2016-01-12 16:05:15 -0800 | [diff] [blame] | 1300 | mBluetoothBinder = service; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1301 | mBluetooth = IBluetooth.Stub.asInterface(service); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1302 | |
Ajay Panicker | 4bb4830 | 2016-03-31 14:14:27 -0700 | [diff] [blame] | 1303 | if (!isNameAndAddressSet()) { |
| 1304 | Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS); |
| 1305 | mHandler.sendMessage(getMsg); |
| 1306 | if (mGetNameAddressOnly) return; |
| 1307 | } |
| 1308 | |
Zhihai Xu | af5971e | 2013-06-10 20:28:31 -0700 | [diff] [blame] | 1309 | try { |
| 1310 | boolean enableHciSnoopLog = (Settings.Secure.getInt(mContentResolver, |
| 1311 | Settings.Secure.BLUETOOTH_HCI_LOG, 0) == 1); |
| 1312 | if (!mBluetooth.configHciSnoopLog(enableHciSnoopLog)) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1313 | Slog.e(TAG,"IBluetooth.configHciSnoopLog return false"); |
Zhihai Xu | af5971e | 2013-06-10 20:28:31 -0700 | [diff] [blame] | 1314 | } |
| 1315 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1316 | Slog.e(TAG,"Unable to call configHciSnoopLog", e); |
Zhihai Xu | af5971e | 2013-06-10 20:28:31 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1319 | //Register callback object |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1320 | try { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1321 | mBluetooth.registerCallback(mBluetoothCallback); |
| 1322 | } catch (RemoteException re) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1323 | Slog.e(TAG, "Unable to register BluetoothCallback",re); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1324 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1325 | //Inform BluetoothAdapter instances that service is up |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1326 | sendBluetoothServiceUpCallback(); |
| 1327 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1328 | //Do enable request |
| 1329 | try { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1330 | if (mQuietEnable == false) { |
| 1331 | if(!mBluetooth.enable()) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1332 | Slog.e(TAG,"IBluetooth.enable() returned false"); |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1333 | } |
| 1334 | } |
| 1335 | else |
| 1336 | { |
| 1337 | if(!mBluetooth.enableNoAutoConnect()) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1338 | Slog.e(TAG,"IBluetooth.enableNoAutoConnect() returned false"); |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1339 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1340 | } |
| 1341 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1342 | Slog.e(TAG,"Unable to call enable()",e); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1343 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1344 | } finally { |
| 1345 | mBluetoothLock.writeLock().unlock(); |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 1346 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1347 | |
| 1348 | if (!mEnable) { |
| 1349 | waitForOnOff(true, false); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1350 | handleDisable(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1351 | waitForOnOff(false, false); |
| 1352 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1353 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1354 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1355 | case MESSAGE_TIMEOUT_BIND: { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1356 | Slog.e(TAG, "MESSAGE_TIMEOUT_BIND"); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1357 | mBluetoothLock.writeLock().lock(); |
| 1358 | mBinding = false; |
| 1359 | mBluetoothLock.writeLock().unlock(); |
| 1360 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1361 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1362 | } |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1363 | case MESSAGE_BLUETOOTH_STATE_CHANGE: |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1364 | { |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1365 | int prevState = msg.arg1; |
| 1366 | int newState = msg.arg2; |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1367 | if (DBG) Slog.d(TAG, "MESSAGE_BLUETOOTH_STATE_CHANGE: prevState = " + prevState + ", newState=" + newState); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1368 | mState = newState; |
| 1369 | bluetoothStateChangeHandler(prevState, newState); |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 1370 | // handle error state transition case from TURNING_ON to OFF |
| 1371 | // unbind and rebind bluetooth service and enable bluetooth |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1372 | if ((prevState == BluetoothAdapter.STATE_BLE_TURNING_ON) && |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1373 | (newState == BluetoothAdapter.STATE_OFF) && |
| 1374 | (mBluetooth != null) && mEnable) { |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 1375 | recoverBluetoothServiceFromError(false); |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 1376 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1377 | if ((prevState == BluetoothAdapter.STATE_TURNING_ON) && |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1378 | (newState == BluetoothAdapter.STATE_BLE_ON) && |
| 1379 | (mBluetooth != null) && mEnable) { |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 1380 | recoverBluetoothServiceFromError(true); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1381 | } |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1382 | // If we tried to enable BT while BT was in the process of shutting down, |
| 1383 | // wait for the BT process to fully tear down and then force a restart |
| 1384 | // here. This is a bit of a hack (b/29363429). |
| 1385 | if ((prevState == BluetoothAdapter.STATE_BLE_TURNING_OFF) && |
| 1386 | (newState == BluetoothAdapter.STATE_OFF)) { |
| 1387 | if (mEnable) { |
| 1388 | Slog.d(TAG, "Entering STATE_OFF but mEnabled is true; restarting."); |
| 1389 | waitForOnOff(false, true); |
| 1390 | Message restartMsg = mHandler.obtainMessage( |
| 1391 | MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 1392 | mHandler.sendMessageDelayed(restartMsg, 2 * SERVICE_RESTART_TIME_MS); |
| 1393 | } |
| 1394 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1395 | if (newState == BluetoothAdapter.STATE_ON || |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1396 | newState == BluetoothAdapter.STATE_BLE_ON) { |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 1397 | // bluetooth is working, reset the counter |
| 1398 | if (mErrorRecoveryRetryCounter != 0) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1399 | Slog.w(TAG, "bluetooth is recovered from error"); |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 1400 | mErrorRecoveryRetryCounter = 0; |
| 1401 | } |
| 1402 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1403 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1404 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1405 | case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: |
| 1406 | { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1407 | Slog.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: " + msg.arg1); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1408 | try { |
| 1409 | mBluetoothLock.writeLock().lock(); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1410 | if (msg.arg1 == SERVICE_IBLUETOOTH) { |
| 1411 | // if service is unbinded already, do nothing and return |
| 1412 | if (mBluetooth == null) break; |
| 1413 | mBluetooth = null; |
| 1414 | } else if (msg.arg1 == SERVICE_IBLUETOOTHGATT) { |
| 1415 | mBluetoothGatt = null; |
| 1416 | break; |
| 1417 | } else { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1418 | Slog.e(TAG, "Bad msg.arg1: " + msg.arg1); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1419 | break; |
| 1420 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1421 | } finally { |
| 1422 | mBluetoothLock.writeLock().unlock(); |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 1423 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1424 | |
| 1425 | if (mEnable) { |
| 1426 | mEnable = false; |
| 1427 | // Send a Bluetooth Restart message |
| 1428 | Message restartMsg = mHandler.obtainMessage( |
| 1429 | MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 1430 | mHandler.sendMessageDelayed(restartMsg, |
| 1431 | SERVICE_RESTART_TIME_MS); |
| 1432 | } |
| 1433 | |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1434 | sendBluetoothServiceDownCallback(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1435 | |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1436 | // Send BT state broadcast to update |
| 1437 | // the BT icon correctly |
| 1438 | if ((mState == BluetoothAdapter.STATE_TURNING_ON) || |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1439 | (mState == BluetoothAdapter.STATE_ON)) { |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1440 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON, |
| 1441 | BluetoothAdapter.STATE_TURNING_OFF); |
| 1442 | mState = BluetoothAdapter.STATE_TURNING_OFF; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1443 | } |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1444 | if (mState == BluetoothAdapter.STATE_TURNING_OFF) { |
| 1445 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF, |
| 1446 | BluetoothAdapter.STATE_OFF); |
| 1447 | } |
| 1448 | |
| 1449 | mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE); |
| 1450 | mState = BluetoothAdapter.STATE_OFF; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1451 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1452 | } |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 1453 | case MESSAGE_RESTART_BLUETOOTH_SERVICE: |
| 1454 | { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1455 | Slog.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE:" |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 1456 | +" Restart IBluetooth service"); |
| 1457 | /* Enable without persisting the setting as |
| 1458 | it doesnt change when IBluetooth |
| 1459 | service restarts */ |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1460 | mEnable = true; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1461 | handleEnable(mQuietEnable); |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 1462 | break; |
| 1463 | } |
| 1464 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1465 | case MESSAGE_TIMEOUT_UNBIND: |
| 1466 | { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1467 | Slog.e(TAG, "MESSAGE_TIMEOUT_UNBIND"); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1468 | mBluetoothLock.writeLock().lock(); |
| 1469 | mUnbinding = false; |
| 1470 | mBluetoothLock.writeLock().unlock(); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1471 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1472 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1473 | |
Jeff Sharkey | aacb89e | 2016-03-05 14:42:58 -0700 | [diff] [blame] | 1474 | case MESSAGE_USER_SWITCHED: { |
| 1475 | if (DBG) Slog.d(TAG, "MESSAGE_USER_SWITCHED"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1476 | mHandler.removeMessages(MESSAGE_USER_SWITCHED); |
Jeff Sharkey | aacb89e | 2016-03-05 14:42:58 -0700 | [diff] [blame] | 1477 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1478 | /* disable and enable BT when detect a user switch */ |
| 1479 | if (mEnable && mBluetooth != null) { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1480 | try { |
| 1481 | mBluetoothLock.readLock().lock(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1482 | if (mBluetooth != null) { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1483 | mBluetooth.unregisterCallback(mBluetoothCallback); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1484 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1485 | } catch (RemoteException re) { |
| 1486 | Slog.e(TAG, "Unable to unregister", re); |
| 1487 | } finally { |
| 1488 | mBluetoothLock.readLock().unlock(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1489 | } |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 1490 | |
| 1491 | if (mState == BluetoothAdapter.STATE_TURNING_OFF) { |
| 1492 | // MESSAGE_USER_SWITCHED happened right after MESSAGE_ENABLE |
| 1493 | bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_OFF); |
| 1494 | mState = BluetoothAdapter.STATE_OFF; |
| 1495 | } |
| 1496 | if (mState == BluetoothAdapter.STATE_OFF) { |
| 1497 | bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_TURNING_ON); |
| 1498 | mState = BluetoothAdapter.STATE_TURNING_ON; |
| 1499 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1500 | |
| 1501 | waitForOnOff(true, false); |
| 1502 | |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 1503 | if (mState == BluetoothAdapter.STATE_TURNING_ON) { |
| 1504 | bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_ON); |
| 1505 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1506 | |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1507 | unbindAllBluetoothProfileServices(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1508 | // disable |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1509 | handleDisable(); |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 1510 | // Pbap service need receive STATE_TURNING_OFF intent to close |
| 1511 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON, |
| 1512 | BluetoothAdapter.STATE_TURNING_OFF); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1513 | |
Pavlin Radoslavov | 4140111 | 2016-06-27 15:25:18 -0700 | [diff] [blame] | 1514 | boolean didDisableTimeout = !waitForOnOff(false, true); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1515 | |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 1516 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF, |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1517 | BluetoothAdapter.STATE_OFF); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1518 | sendBluetoothServiceDownCallback(); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1519 | |
Pavlin Radoslavov | e957a8a | 2016-05-24 15:28:41 -0700 | [diff] [blame] | 1520 | try { |
| 1521 | mBluetoothLock.writeLock().lock(); |
| 1522 | if (mBluetooth != null) { |
| 1523 | mBluetooth = null; |
| 1524 | // Unbind |
| 1525 | mContext.unbindService(mConnection); |
| 1526 | } |
| 1527 | mBluetoothGatt = null; |
| 1528 | } finally { |
| 1529 | mBluetoothLock.writeLock().unlock(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1530 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1531 | |
Pavlin Radoslavov | 4140111 | 2016-06-27 15:25:18 -0700 | [diff] [blame] | 1532 | // |
| 1533 | // If disabling Bluetooth times out, wait for an |
| 1534 | // additional amount of time to ensure the process is |
| 1535 | // shut down completely before attempting to restart. |
| 1536 | // |
| 1537 | if (didDisableTimeout) { |
| 1538 | SystemClock.sleep(3000); |
| 1539 | } else { |
| 1540 | SystemClock.sleep(100); |
| 1541 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1542 | |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 1543 | mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE); |
| 1544 | mState = BluetoothAdapter.STATE_OFF; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1545 | // enable |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1546 | handleEnable(mQuietEnable); |
John Spurlock | 8a985d2 | 2014-02-25 09:40:05 -0500 | [diff] [blame] | 1547 | } else if (mBinding || mBluetooth != null) { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1548 | Message userMsg = mHandler.obtainMessage(MESSAGE_USER_SWITCHED); |
| 1549 | userMsg.arg2 = 1 + msg.arg2; |
| 1550 | // if user is switched when service is being binding |
| 1551 | // delay sending MESSAGE_USER_SWITCHED |
| 1552 | mHandler.sendMessageDelayed(userMsg, USER_SWITCHED_TIME_MS); |
| 1553 | if (DBG) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1554 | Slog.d(TAG, "delay MESSAGE_USER_SWITCHED " + userMsg.arg2); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1555 | } |
John Spurlock | 8a985d2 | 2014-02-25 09:40:05 -0500 | [diff] [blame] | 1556 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1557 | break; |
| 1558 | } |
Jeff Sharkey | aacb89e | 2016-03-05 14:42:58 -0700 | [diff] [blame] | 1559 | case MESSAGE_USER_UNLOCKED: { |
| 1560 | if (DBG) Slog.d(TAG, "MESSAGE_USER_UNLOCKED"); |
| 1561 | mHandler.removeMessages(MESSAGE_USER_SWITCHED); |
| 1562 | |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1563 | if (mEnable && !mBinding && (mBluetooth == null)) { |
| 1564 | // We should be connected, but we gave up for some |
| 1565 | // reason; maybe the Bluetooth service wasn't encryption |
| 1566 | // aware, so try binding again. |
| 1567 | if (DBG) Slog.d(TAG, "Enabled but not bound; retrying after unlock"); |
| 1568 | handleEnable(mQuietEnable); |
Jeff Sharkey | aacb89e | 2016-03-05 14:42:58 -0700 | [diff] [blame] | 1569 | } |
| 1570 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1571 | } |
| 1572 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1573 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1574 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1575 | private void handleEnable(boolean quietMode) { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1576 | mQuietEnable = quietMode; |
| 1577 | |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1578 | try { |
| 1579 | mBluetoothLock.writeLock().lock(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1580 | if ((mBluetooth == null) && (!mBinding)) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1581 | //Start bind timeout and bind |
| 1582 | Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND); |
| 1583 | mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1584 | Intent i = new Intent(IBluetooth.class.getName()); |
Dianne Hackborn | ce09f5a | 2014-10-10 15:03:13 -0700 | [diff] [blame] | 1585 | if (!doBind(i, mConnection,Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, |
| 1586 | UserHandle.CURRENT)) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1587 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1588 | } else { |
| 1589 | mBinding = true; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1590 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1591 | } else if (mBluetooth != null) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1592 | //Enable bluetooth |
| 1593 | try { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1594 | if (!mQuietEnable) { |
| 1595 | if(!mBluetooth.enable()) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1596 | Slog.e(TAG,"IBluetooth.enable() returned false"); |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | else { |
| 1600 | if(!mBluetooth.enableNoAutoConnect()) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1601 | Slog.e(TAG,"IBluetooth.enableNoAutoConnect() returned false"); |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1602 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1603 | } |
| 1604 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1605 | Slog.e(TAG,"Unable to call enable()",e); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1606 | } |
| 1607 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1608 | } finally { |
| 1609 | mBluetoothLock.writeLock().unlock(); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1610 | } |
| 1611 | } |
| 1612 | |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 1613 | boolean doBind(Intent intent, ServiceConnection conn, int flags, UserHandle user) { |
| 1614 | ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); |
| 1615 | intent.setComponent(comp); |
| 1616 | if (comp == null || !mContext.bindServiceAsUser(intent, conn, flags, user)) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1617 | Slog.e(TAG, "Fail to bind to: " + intent); |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 1618 | return false; |
| 1619 | } |
| 1620 | return true; |
| 1621 | } |
| 1622 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1623 | private void handleDisable() { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1624 | try { |
| 1625 | mBluetoothLock.readLock().lock(); |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1626 | if (mBluetooth != null) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1627 | if (DBG) Slog.d(TAG,"Sending off request."); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1628 | if (!mBluetooth.disable()) { |
| 1629 | Slog.e(TAG,"IBluetooth.disable() returned false"); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1630 | } |
| 1631 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1632 | } catch (RemoteException e) { |
| 1633 | Slog.e(TAG,"Unable to call disable()",e); |
| 1634 | } finally { |
| 1635 | mBluetoothLock.readLock().unlock(); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1636 | } |
| 1637 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1638 | |
| 1639 | private boolean checkIfCallerIsForegroundUser() { |
| 1640 | int foregroundUser; |
| 1641 | int callingUser = UserHandle.getCallingUserId(); |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 1642 | int callingUid = Binder.getCallingUid(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1643 | long callingIdentity = Binder.clearCallingIdentity(); |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1644 | UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 1645 | UserInfo ui = um.getProfileParent(callingUser); |
| 1646 | int parentUser = (ui != null) ? ui.id : UserHandle.USER_NULL; |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 1647 | int callingAppId = UserHandle.getAppId(callingUid); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1648 | boolean valid = false; |
| 1649 | try { |
| 1650 | foregroundUser = ActivityManager.getCurrentUser(); |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 1651 | valid = (callingUser == foregroundUser) || |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1652 | parentUser == foregroundUser || |
Adrian Roos | bd9a9a5 | 2014-08-18 15:31:57 +0200 | [diff] [blame] | 1653 | callingAppId == Process.NFC_UID || |
| 1654 | callingAppId == mSystemUiUid; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1655 | if (DBG) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1656 | Slog.d(TAG, "checkIfCallerIsForegroundUser: valid=" + valid |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1657 | + " callingUser=" + callingUser |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1658 | + " parentUser=" + parentUser |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1659 | + " foregroundUser=" + foregroundUser); |
| 1660 | } |
| 1661 | } finally { |
| 1662 | Binder.restoreCallingIdentity(callingIdentity); |
| 1663 | } |
| 1664 | return valid; |
| 1665 | } |
| 1666 | |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1667 | private void sendBleStateChanged(int prevState, int newState) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1668 | if (DBG) Slog.d(TAG,"BLE State Change Intent: " + prevState + " -> " + newState); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1669 | // Send broadcast message to everyone else |
| 1670 | Intent intent = new Intent(BluetoothAdapter.ACTION_BLE_STATE_CHANGED); |
| 1671 | intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState); |
| 1672 | intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState); |
| 1673 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 1674 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM); |
| 1675 | } |
| 1676 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1677 | private void bluetoothStateChangeHandler(int prevState, int newState) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1678 | boolean isStandardBroadcast = true; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1679 | if (prevState != newState) { |
| 1680 | //Notify all proxy objects first of adapter state change |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1681 | if (newState == BluetoothAdapter.STATE_BLE_ON || |
| 1682 | newState == BluetoothAdapter.STATE_OFF) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1683 | boolean intermediate_off = (prevState == BluetoothAdapter.STATE_TURNING_OFF |
| 1684 | && newState == BluetoothAdapter.STATE_BLE_ON); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1685 | |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1686 | if (newState == BluetoothAdapter.STATE_OFF) { |
| 1687 | // If Bluetooth is off, send service down event to proxy objects, and unbind |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1688 | if (DBG) Slog.d(TAG, "Bluetooth is complete turn off"); |
Pavlin Radoslavov | e47ec14 | 2016-06-01 22:25:18 -0700 | [diff] [blame] | 1689 | sendBluetoothServiceDownCallback(); |
| 1690 | unbindAndFinish(); |
| 1691 | sendBleStateChanged(prevState, newState); |
| 1692 | // Don't broadcast as it has already been broadcast before |
| 1693 | isStandardBroadcast = false; |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1694 | |
| 1695 | } else if (!intermediate_off) { |
| 1696 | // connect to GattService |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1697 | if (DBG) Slog.d(TAG, "Bluetooth is in LE only mode"); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1698 | if (mBluetoothGatt != null) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1699 | if (DBG) Slog.d(TAG, "Calling BluetoothGattServiceUp"); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1700 | onBluetoothGattServiceUp(); |
| 1701 | } else { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1702 | if (DBG) Slog.d(TAG, "Binding Bluetooth GATT service"); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1703 | if (mContext.getPackageManager().hasSystemFeature( |
| 1704 | PackageManager.FEATURE_BLUETOOTH_LE)) { |
| 1705 | Intent i = new Intent(IBluetoothGatt.class.getName()); |
| 1706 | doBind(i, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, UserHandle.CURRENT); |
| 1707 | } |
| 1708 | } |
| 1709 | sendBleStateChanged(prevState, newState); |
| 1710 | //Don't broadcase this as std intent |
| 1711 | isStandardBroadcast = false; |
| 1712 | |
| 1713 | } else if (intermediate_off){ |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1714 | if (DBG) Slog.d(TAG, "Intermediate off, back to LE only mode"); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1715 | // For LE only mode, broadcast as is |
| 1716 | sendBleStateChanged(prevState, newState); |
| 1717 | sendBluetoothStateCallback(false); // BT is OFF for general users |
| 1718 | // Broadcast as STATE_OFF |
| 1719 | newState = BluetoothAdapter.STATE_OFF; |
| 1720 | sendBrEdrDownCallback(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1721 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1722 | } else if (newState == BluetoothAdapter.STATE_ON) { |
| 1723 | boolean isUp = (newState==BluetoothAdapter.STATE_ON); |
| 1724 | sendBluetoothStateCallback(isUp); |
| 1725 | sendBleStateChanged(prevState, newState); |
| 1726 | |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1727 | } else if (newState == BluetoothAdapter.STATE_BLE_TURNING_ON || |
| 1728 | newState == BluetoothAdapter.STATE_BLE_TURNING_OFF ) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1729 | sendBleStateChanged(prevState, newState); |
| 1730 | isStandardBroadcast = false; |
| 1731 | |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1732 | } else if (newState == BluetoothAdapter.STATE_TURNING_ON || |
| 1733 | newState == BluetoothAdapter.STATE_TURNING_OFF) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1734 | sendBleStateChanged(prevState, newState); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1735 | } |
| 1736 | |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1737 | if (isStandardBroadcast) { |
| 1738 | if (prevState == BluetoothAdapter.STATE_BLE_ON) { |
| 1739 | // Show prevState of BLE_ON as OFF to standard users |
| 1740 | prevState = BluetoothAdapter.STATE_OFF; |
| 1741 | } |
| 1742 | Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED); |
| 1743 | intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState); |
| 1744 | intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState); |
| 1745 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 1746 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM); |
| 1747 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | /** |
| 1752 | * if on is true, wait for state become ON |
| 1753 | * if off is true, wait for state become OFF |
| 1754 | * if both on and off are false, wait for state not ON |
| 1755 | */ |
| 1756 | private boolean waitForOnOff(boolean on, boolean off) { |
| 1757 | int i = 0; |
| 1758 | while (i < 10) { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1759 | try { |
| 1760 | mBluetoothLock.readLock().lock(); |
| 1761 | if (mBluetooth == null) break; |
| 1762 | if (on) { |
| 1763 | if (mBluetooth.getState() == BluetoothAdapter.STATE_ON) return true; |
| 1764 | } else if (off) { |
| 1765 | if (mBluetooth.getState() == BluetoothAdapter.STATE_OFF) return true; |
| 1766 | } else { |
| 1767 | if (mBluetooth.getState() != BluetoothAdapter.STATE_ON) return true; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1768 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1769 | } catch (RemoteException e) { |
| 1770 | Slog.e(TAG, "getState()", e); |
| 1771 | break; |
| 1772 | } finally { |
| 1773 | mBluetoothLock.readLock().unlock(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1774 | } |
| 1775 | if (on || off) { |
| 1776 | SystemClock.sleep(300); |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 1777 | } else { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1778 | SystemClock.sleep(50); |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 1779 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1780 | i++; |
| 1781 | } |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1782 | Slog.e(TAG,"waitForOnOff time out"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1783 | return false; |
| 1784 | } |
Zhihai Xu | 681ae7f | 2012-11-12 15:14:18 -0800 | [diff] [blame] | 1785 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1786 | private void sendDisableMsg() { |
| 1787 | mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISABLE)); |
| 1788 | } |
| 1789 | |
| 1790 | private void sendEnableMsg(boolean quietMode) { |
| 1791 | mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_ENABLE, |
| 1792 | quietMode ? 1 : 0, 0)); |
| 1793 | } |
| 1794 | |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 1795 | private void recoverBluetoothServiceFromError(boolean clearBle) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1796 | Slog.e(TAG,"recoverBluetoothServiceFromError"); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1797 | try { |
| 1798 | mBluetoothLock.readLock().lock(); |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 1799 | if (mBluetooth != null) { |
| 1800 | //Unregister callback object |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1801 | mBluetooth.unregisterCallback(mBluetoothCallback); |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 1802 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1803 | } catch (RemoteException re) { |
| 1804 | Slog.e(TAG, "Unable to unregister", re); |
| 1805 | } finally { |
| 1806 | mBluetoothLock.readLock().unlock(); |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | SystemClock.sleep(500); |
| 1810 | |
| 1811 | // disable |
| 1812 | handleDisable(); |
| 1813 | |
| 1814 | waitForOnOff(false, true); |
| 1815 | |
| 1816 | sendBluetoothServiceDownCallback(); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1817 | |
Pavlin Radoslavov | e957a8a | 2016-05-24 15:28:41 -0700 | [diff] [blame] | 1818 | try { |
| 1819 | mBluetoothLock.writeLock().lock(); |
| 1820 | if (mBluetooth != null) { |
| 1821 | mBluetooth = null; |
| 1822 | // Unbind |
| 1823 | mContext.unbindService(mConnection); |
| 1824 | } |
| 1825 | mBluetoothGatt = null; |
| 1826 | } finally { |
| 1827 | mBluetoothLock.writeLock().unlock(); |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE); |
| 1831 | mState = BluetoothAdapter.STATE_OFF; |
| 1832 | |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 1833 | if (clearBle) { |
| 1834 | clearBleApps(); |
| 1835 | } |
| 1836 | |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 1837 | mEnable = false; |
| 1838 | |
| 1839 | if (mErrorRecoveryRetryCounter++ < MAX_ERROR_RESTART_RETRIES) { |
| 1840 | // Send a Bluetooth Restart message to reenable bluetooth |
| 1841 | Message restartMsg = mHandler.obtainMessage( |
| 1842 | MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 1843 | mHandler.sendMessageDelayed(restartMsg, ERROR_RESTART_TIME_MS); |
| 1844 | } else { |
| 1845 | // todo: notify user to power down and power up phone to make bluetooth work. |
| 1846 | } |
| 1847 | } |
Mike Lockwood | 726d4de | 2014-10-28 14:06:28 -0700 | [diff] [blame] | 1848 | |
| 1849 | @Override |
Pavlin Radoslavov | 6e8faff | 2016-02-23 11:54:37 -0800 | [diff] [blame] | 1850 | public void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 1851 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
| 1852 | String errorMsg = null; |
| 1853 | if (mBluetoothBinder == null) { |
| 1854 | errorMsg = "Bluetooth Service not connected"; |
| 1855 | } else { |
| 1856 | try { |
| 1857 | mBluetoothBinder.dump(fd, args); |
| 1858 | } catch (RemoteException re) { |
| 1859 | errorMsg = "RemoteException while calling Bluetooth Service"; |
| 1860 | } |
Mike Lockwood | 726d4de | 2014-10-28 14:06:28 -0700 | [diff] [blame] | 1861 | } |
Pavlin Radoslavov | 6e8faff | 2016-02-23 11:54:37 -0800 | [diff] [blame] | 1862 | if (errorMsg != null) { |
| 1863 | // Silently return if we are extracting metrics in Protobuf format |
| 1864 | if ((args.length > 0) && args[0].startsWith("--proto")) |
| 1865 | return; |
| 1866 | writer.println(errorMsg); |
| 1867 | } |
Mike Lockwood | 726d4de | 2014-10-28 14:06:28 -0700 | [diff] [blame] | 1868 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1869 | } |