blob: 0f8db2e656594d475974255eb728d20eee872109 [file] [log] [blame]
fredc0f420372012-04-12 00:02:00 -07001/*
Zhihai Xufa0fd392012-10-23 17:31:56 -07002 * 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.
fredc0f420372012-04-12 00:02:00 -070015 */
16
17package com.android.server;
18
Svet Ganov408abf72015-05-12 19:13:36 -070019import android.Manifest;
Zhihai Xu40874a02012-10-08 17:57:03 -070020import android.app.ActivityManager;
fredc0f420372012-04-12 00:02:00 -070021import android.bluetooth.BluetoothAdapter;
Benjamin Franze8b98922014-11-12 15:57:54 +000022import android.bluetooth.BluetoothProfile;
fredc0f420372012-04-12 00:02:00 -070023import android.bluetooth.IBluetooth;
fredcbf072a72012-05-09 16:52:50 -070024import android.bluetooth.IBluetoothCallback;
Wei Wange4a744b2015-06-11 17:50:29 -070025import android.bluetooth.IBluetoothGatt;
Benjamin Franze8b98922014-11-12 15:57:54 +000026import android.bluetooth.IBluetoothHeadset;
fredc0f420372012-04-12 00:02:00 -070027import android.bluetooth.IBluetoothManager;
28import android.bluetooth.IBluetoothManagerCallback;
Benjamin Franze8b98922014-11-12 15:57:54 +000029import android.bluetooth.IBluetoothProfileServiceConnection;
fredc0f420372012-04-12 00:02:00 -070030import android.bluetooth.IBluetoothStateChangeCallback;
fredc0f420372012-04-12 00:02:00 -070031import android.content.BroadcastReceiver;
32import android.content.ComponentName;
33import android.content.ContentResolver;
34import android.content.Context;
35import android.content.Intent;
36import android.content.IntentFilter;
37import android.content.ServiceConnection;
Matthew Xie32ab77b2013-05-08 19:26:57 -070038import android.content.pm.PackageManager;
Benjamin Franze8b98922014-11-12 15:57:54 +000039import android.content.pm.UserInfo;
Wei Wange4a744b2015-06-11 17:50:29 -070040import android.database.ContentObserver;
Zhihai Xu40874a02012-10-08 17:57:03 -070041import android.os.Binder;
fredc0f420372012-04-12 00:02:00 -070042import android.os.Handler;
fredc0f420372012-04-12 00:02:00 -070043import android.os.IBinder;
Zhihai Xu40874a02012-10-08 17:57:03 -070044import android.os.Looper;
fredc0f420372012-04-12 00:02:00 -070045import android.os.Message;
Zhihai Xu40874a02012-10-08 17:57:03 -070046import android.os.Process;
fredcd6883532012-04-25 17:46:13 -070047import android.os.RemoteCallbackList;
fredc0f420372012-04-12 00:02:00 -070048import android.os.RemoteException;
Zhihai Xu40874a02012-10-08 17:57:03 -070049import android.os.SystemClock;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070050import android.os.UserHandle;
Benjamin Franze8b98922014-11-12 15:57:54 +000051import android.os.UserManager;
fredc0f420372012-04-12 00:02:00 -070052import android.provider.Settings;
Wei Wang67d84162015-04-26 17:04:29 -070053import android.provider.Settings.SettingNotFoundException;
Jeff Sharkey67609c72016-03-05 14:29:13 -070054import android.util.Slog;
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -070055import java.util.concurrent.locks.ReentrantReadWriteLock;
Mike Lockwood726d4de2014-10-28 14:06:28 -070056
57import java.io.FileDescriptor;
58import java.io.PrintWriter;
Benjamin Franze8b98922014-11-12 15:57:54 +000059import java.util.HashMap;
60import java.util.Map;
Miao Chou658bf2f2015-06-26 17:14:35 -070061
fredc0f420372012-04-12 00:02:00 -070062class BluetoothManagerService extends IBluetoothManager.Stub {
63 private static final String TAG = "BluetoothManagerService";
Pavlin Radoslavov41401112016-06-27 15:25:18 -070064 private static final boolean DBG = true;
fredc0f420372012-04-12 00:02:00 -070065
fredc0f420372012-04-12 00:02:00 -070066 private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN;
67 private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
fredc0f420372012-04-12 00:02:00 -070068 private static final String ACTION_SERVICE_STATE_CHANGED="com.android.bluetooth.btservice.action.STATE_CHANGED";
69 private static final String EXTRA_ACTION="action";
Zhihai Xud31c3222012-10-31 16:08:57 -070070 private static final String SECURE_SETTINGS_BLUETOOTH_ADDR_VALID="bluetooth_addr_valid";
fredc0f420372012-04-12 00:02:00 -070071 private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address";
72 private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name";
fredc0f420372012-04-12 00:02:00 -070073 private static final int TIMEOUT_BIND_MS = 3000; //Maximum msec to wait for a bind
74 private static final int TIMEOUT_SAVE_MS = 500; //Maximum msec to wait for a save
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +053075 //Maximum msec to wait for service restart
76 private static final int SERVICE_RESTART_TIME_MS = 200;
Zhihai Xudd9d17d2013-01-08 17:05:58 -080077 //Maximum msec to wait for restart due to error
78 private static final int ERROR_RESTART_TIME_MS = 3000;
Zhihai Xu40874a02012-10-08 17:57:03 -070079 //Maximum msec to delay MESSAGE_USER_SWITCHED
80 private static final int USER_SWITCHED_TIME_MS = 200;
Benjamin Franze8b98922014-11-12 15:57:54 +000081 // Delay for the addProxy function in msec
82 private static final int ADD_PROXY_DELAY_MS = 100;
fredc0f420372012-04-12 00:02:00 -070083
84 private static final int MESSAGE_ENABLE = 1;
85 private static final int MESSAGE_DISABLE = 2;
fredc649fe492012-04-19 01:07:18 -070086 private static final int MESSAGE_REGISTER_ADAPTER = 20;
87 private static final int MESSAGE_UNREGISTER_ADAPTER = 21;
88 private static final int MESSAGE_REGISTER_STATE_CHANGE_CALLBACK = 30;
89 private static final int MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK = 31;
90 private static final int MESSAGE_BLUETOOTH_SERVICE_CONNECTED = 40;
91 private static final int MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED = 41;
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +053092 private static final int MESSAGE_RESTART_BLUETOOTH_SERVICE = 42;
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -070093 private static final int MESSAGE_BLUETOOTH_STATE_CHANGE = 60;
94 private static final int MESSAGE_TIMEOUT_BIND = 100;
95 private static final int MESSAGE_TIMEOUT_UNBIND = 101;
Ajay Panicker4bb48302016-03-31 14:14:27 -070096 private static final int MESSAGE_GET_NAME_AND_ADDRESS = 200;
Zhihai Xu40874a02012-10-08 17:57:03 -070097 private static final int MESSAGE_USER_SWITCHED = 300;
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -070098 private static final int MESSAGE_USER_UNLOCKED = 301;
Benjamin Franze8b98922014-11-12 15:57:54 +000099 private static final int MESSAGE_ADD_PROXY_DELAYED = 400;
100 private static final int MESSAGE_BIND_PROFILE_SERVICE = 401;
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -0700101 private static final int MAX_SAVE_RETRIES = 3;
102 private static final int MAX_ERROR_RESTART_RETRIES = 6;
Zhihai Xudd9d17d2013-01-08 17:05:58 -0800103
Zhihai Xu401202b2012-12-03 11:36:21 -0800104 // Bluetooth persisted setting is off
105 private static final int BLUETOOTH_OFF=0;
106 // Bluetooth persisted setting is on
107 // and Airplane mode won't affect Bluetooth state at start up
108 private static final int BLUETOOTH_ON_BLUETOOTH=1;
109 // Bluetooth persisted setting is on
110 // but Airplane mode will affect Bluetooth state at start up
111 // and Airplane mode will have higher priority.
112 private static final int BLUETOOTH_ON_AIRPLANE=2;
fredc0f420372012-04-12 00:02:00 -0700113
Matthew Xieddf7e472013-03-01 18:41:02 -0800114 private static final int SERVICE_IBLUETOOTH = 1;
115 private static final int SERVICE_IBLUETOOTHGATT = 2;
116
fredc0f420372012-04-12 00:02:00 -0700117 private final Context mContext;
Nitin Arorad055adb2015-03-02 15:03:51 -0800118 private static int mBleAppCount = 0;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700119
120 // Locks are not provided for mName and mAddress.
121 // They are accessed in handler or broadcast receiver, same thread context.
fredc0f420372012-04-12 00:02:00 -0700122 private String mAddress;
123 private String mName;
Matthew Xie6fde3092012-07-11 17:10:07 -0700124 private final ContentResolver mContentResolver;
125 private final RemoteCallbackList<IBluetoothManagerCallback> mCallbacks;
126 private final RemoteCallbackList<IBluetoothStateChangeCallback> mStateChangeCallbacks;
Marie Janssen9db28eb2016-01-12 16:05:15 -0800127 private IBinder mBluetoothBinder;
fredc649fe492012-04-19 01:07:18 -0700128 private IBluetooth mBluetooth;
Matthew Xieddf7e472013-03-01 18:41:02 -0800129 private IBluetoothGatt mBluetoothGatt;
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700130 private final ReentrantReadWriteLock mBluetoothLock =
131 new ReentrantReadWriteLock();
fredc649fe492012-04-19 01:07:18 -0700132 private boolean mBinding;
133 private boolean mUnbinding;
Zhihai Xu401202b2012-12-03 11:36:21 -0800134 // used inside handler thread
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700135 private boolean mQuietEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -0800136 // 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 Xu40874a02012-10-08 17:57:03 -0700143 private boolean mEnable;
144 private int mState;
Zhihai Xu40874a02012-10-08 17:57:03 -0700145 private final BluetoothHandler mHandler;
Zhihai Xudd9d17d2013-01-08 17:05:58 -0800146 private int mErrorRecoveryRetryCounter;
Adrian Roosbd9a9a52014-08-18 15:31:57 +0200147 private final int mSystemUiUid;
fredc0f420372012-04-12 00:02:00 -0700148
Benjamin Franze8b98922014-11-12 15:57:54 +0000149 // 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
fredc649fe492012-04-19 01:07:18 -0700154 private void registerForAirplaneMode(IntentFilter filter) {
155 final ContentResolver resolver = mContext.getContentResolver();
Christopher Tatec09cdce2012-09-10 16:50:14 -0700156 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);
fredc649fe492012-04-19 01:07:18 -0700160 boolean mIsAirplaneSensitive = airplaneModeRadios == null ? true :
Christopher Tatec09cdce2012-09-10 16:50:14 -0700161 airplaneModeRadios.contains(Settings.Global.RADIO_BLUETOOTH);
fredc649fe492012-04-19 01:07:18 -0700162 if (mIsAirplaneSensitive) {
163 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
164 }
165 }
166
fredcbf072a72012-05-09 16:52:50 -0700167 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() {
fredc0f420372012-04-12 00:02:00 -0700176 @Override
177 public void onReceive(Context context, Intent intent) {
178 String action = intent.getAction();
fredcbf072a72012-05-09 16:52:50 -0700179 if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action)) {
fredc0f420372012-04-12 00:02:00 -0700180 String newName = intent.getStringExtra(BluetoothAdapter.EXTRA_LOCAL_NAME);
Jeff Sharkey67609c72016-03-05 14:29:13 -0700181 if (DBG) Slog.d(TAG, "Bluetooth Adapter name changed to " + newName);
fredc0f420372012-04-12 00:02:00 -0700182 if (newName != null) {
183 storeNameAndAddress(newName, null);
184 }
fredc649fe492012-04-19 01:07:18 -0700185 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
Zhihai Xu401202b2012-12-03 11:36:21 -0800186 synchronized(mReceiver) {
187 if (isBluetoothPersistedStateOn()) {
188 if (isAirplaneModeOn()) {
189 persistBluetoothSetting(BLUETOOTH_ON_AIRPLANE);
190 } else {
191 persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);
192 }
193 }
Nitin Arorad055adb2015-03-02 15:03:51 -0800194
195 int st = BluetoothAdapter.STATE_OFF;
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700196 try {
197 mBluetoothLock.readLock().lock();
198 if (mBluetooth != null) {
Nitin Arorad055adb2015-03-02 15:03:51 -0800199 st = mBluetooth.getState();
Nitin Arorad055adb2015-03-02 15:03:51 -0800200 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700201 } catch (RemoteException e) {
202 Slog.e(TAG, "Unable to call getState", e);
203 } finally {
204 mBluetoothLock.readLock().unlock();
Nitin Arorad055adb2015-03-02 15:03:51 -0800205 }
Jeff Sharkey67609c72016-03-05 14:29:13 -0700206 Slog.d(TAG, "state" + st);
Nitin Arorad055adb2015-03-02 15:03:51 -0800207
Zhihai Xu401202b2012-12-03 11:36:21 -0800208 if (isAirplaneModeOn()) {
Nitin Arorad055adb2015-03-02 15:03:51 -0800209 // Clear registered LE apps to force shut-off
210 synchronized (this) {
211 mBleAppCount = 0;
Nitin Arora11f83882015-05-07 18:45:44 -0700212 mBleApps.clear();
Nitin Arorad055adb2015-03-02 15:03:51 -0800213 }
214 if (st == BluetoothAdapter.STATE_BLE_ON) {
215 //if state is BLE_ON make sure you trigger disableBLE part
216 try {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700217 mBluetoothLock.readLock().lock();
Nitin Arorad055adb2015-03-02 15:03:51 -0800218 if (mBluetooth != null) {
219 mBluetooth.onBrEdrDown();
220 mEnableExternal = false;
221 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700222 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700223 Slog.e(TAG,"Unable to call onBrEdrDown", e);
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700224 } finally {
Pavlin Radoslavov7ee53be2016-06-09 12:58:07 -0700225 mBluetoothLock.readLock().unlock();
Nitin Arorad055adb2015-03-02 15:03:51 -0800226 }
227 } else if (st == BluetoothAdapter.STATE_ON){
228 // disable without persisting the setting
Jeff Sharkey67609c72016-03-05 14:29:13 -0700229 Slog.d(TAG, "Calling disable");
Nitin Arorad055adb2015-03-02 15:03:51 -0800230 sendDisableMsg();
231 }
Zhihai Xu401202b2012-12-03 11:36:21 -0800232 } else if (mEnableExternal) {
233 // enable without persisting the setting
Jeff Sharkey67609c72016-03-05 14:29:13 -0700234 Slog.d(TAG, "Calling enable");
Zhihai Xu401202b2012-12-03 11:36:21 -0800235 sendEnableMsg(mQuietEnableExternal);
236 }
fredc649fe492012-04-19 01:07:18 -0700237 }
fredc0f420372012-04-12 00:02:00 -0700238 }
239 }
240 };
241
242 BluetoothManagerService(Context context) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700243 mHandler = new BluetoothHandler(IoThread.get().getLooper());
Zhihai Xu40874a02012-10-08 17:57:03 -0700244
fredc0f420372012-04-12 00:02:00 -0700245 mContext = context;
246 mBluetooth = null;
Marie Janssen9db28eb2016-01-12 16:05:15 -0800247 mBluetoothBinder = null;
Nitin Arorad055adb2015-03-02 15:03:51 -0800248 mBluetoothGatt = null;
fredc0f420372012-04-12 00:02:00 -0700249 mBinding = false;
250 mUnbinding = false;
Zhihai Xu40874a02012-10-08 17:57:03 -0700251 mEnable = false;
252 mState = BluetoothAdapter.STATE_OFF;
Zhihai Xu401202b2012-12-03 11:36:21 -0800253 mQuietEnableExternal = false;
254 mEnableExternal = false;
fredc0f420372012-04-12 00:02:00 -0700255 mAddress = null;
256 mName = null;
Zhihai Xudd9d17d2013-01-08 17:05:58 -0800257 mErrorRecoveryRetryCounter = 0;
fredc0f420372012-04-12 00:02:00 -0700258 mContentResolver = context.getContentResolver();
Wei Wange4a744b2015-06-11 17:50:29 -0700259 // Observe BLE scan only mode settings change.
260 registerForBleScanModeChange();
fredcd6883532012-04-25 17:46:13 -0700261 mCallbacks = new RemoteCallbackList<IBluetoothManagerCallback>();
262 mStateChangeCallbacks = new RemoteCallbackList<IBluetoothStateChangeCallback>();
Miao Chou658bf2f2015-06-26 17:14:35 -0700263 IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
Matthew Xie6fde3092012-07-11 17:10:07 -0700264 registerForAirplaneMode(filter);
Dianne Hackbornd83a0962014-05-02 16:28:33 -0700265 filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
Matthew Xie6fde3092012-07-11 17:10:07 -0700266 mContext.registerReceiver(mReceiver, filter);
fredc0f420372012-04-12 00:02:00 -0700267 loadStoredNameAndAddress();
Zhihai Xu401202b2012-12-03 11:36:21 -0800268 if (isBluetoothPersistedStateOn()) {
269 mEnableExternal = true;
fredc0f420372012-04-12 00:02:00 -0700270 }
Adrian Roosbd9a9a52014-08-18 15:31:57 +0200271
272 int sysUiUid = -1;
273 try {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700274 sysUiUid = mContext.getPackageManager().getPackageUidAsUser("com.android.systemui",
Jeff Sharkeyc5967e92016-01-07 18:50:29 -0700275 PackageManager.MATCH_SYSTEM_ONLY, UserHandle.USER_SYSTEM);
Adrian Roosbd9a9a52014-08-18 15:31:57 +0200276 } catch (PackageManager.NameNotFoundException e) {
Joe LaPennaacddf2b2015-09-04 12:52:42 -0700277 // Some platforms, such as wearables do not have a system ui.
Jeff Sharkey67609c72016-03-05 14:29:13 -0700278 Slog.w(TAG, "Unable to resolve SystemUI's UID.", e);
Adrian Roosbd9a9a52014-08-18 15:31:57 +0200279 }
280 mSystemUiUid = sysUiUid;
fredc0f420372012-04-12 00:02:00 -0700281 }
282
fredc649fe492012-04-19 01:07:18 -0700283 /**
284 * Returns true if airplane mode is currently on
285 */
286 private final boolean isAirplaneModeOn() {
Christopher Tatec09cdce2012-09-10 16:50:14 -0700287 return Settings.Global.getInt(mContext.getContentResolver(),
288 Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
fredc649fe492012-04-19 01:07:18 -0700289 }
290
291 /**
292 * Returns true if the Bluetooth saved state is "on"
293 */
294 private final boolean isBluetoothPersistedStateOn() {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700295 return Settings.Global.getInt(mContentResolver,
Andre Eisenbach8c184312016-09-06 18:03:10 -0700296 Settings.Global.BLUETOOTH_ON, BLUETOOTH_ON_BLUETOOTH) != BLUETOOTH_OFF;
Zhihai Xu401202b2012-12-03 11:36:21 -0800297 }
298
299 /**
300 * Returns true if the Bluetooth saved state is BLUETOOTH_ON_BLUETOOTH
301 */
302 private final boolean isBluetoothPersistedStateOnBluetooth() {
303 return Settings.Global.getInt(mContentResolver,
Andre Eisenbach8c184312016-09-06 18:03:10 -0700304 Settings.Global.BLUETOOTH_ON, BLUETOOTH_ON_BLUETOOTH) == BLUETOOTH_ON_BLUETOOTH;
fredc649fe492012-04-19 01:07:18 -0700305 }
306
307 /**
308 * Save the Bluetooth on/off state
309 *
310 */
Zhihai Xu401202b2012-12-03 11:36:21 -0800311 private void persistBluetoothSetting(int value) {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700312 Settings.Global.putInt(mContext.getContentResolver(),
313 Settings.Global.BLUETOOTH_ON,
Zhihai Xu401202b2012-12-03 11:36:21 -0800314 value);
fredc649fe492012-04-19 01:07:18 -0700315 }
316
317 /**
318 * Returns true if the Bluetooth Adapter's name and address is
319 * locally cached
320 * @return
321 */
fredc0f420372012-04-12 00:02:00 -0700322 private boolean isNameAndAddressSet() {
323 return mName !=null && mAddress!= null && mName.length()>0 && mAddress.length()>0;
324 }
325
fredc649fe492012-04-19 01:07:18 -0700326 /**
327 * Retrieve the Bluetooth Adapter's name and address and save it in
328 * in the local cache
329 */
fredc0f420372012-04-12 00:02:00 -0700330 private void loadStoredNameAndAddress() {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700331 if (DBG) Slog.d(TAG, "Loading stored name and address");
Zhihai Xud31c3222012-10-31 16:08:57 -0700332 if (mContext.getResources().getBoolean
333 (com.android.internal.R.bool.config_bluetooth_address_validation) &&
334 Settings.Secure.getInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 0) == 0) {
335 // if the valid flag is not set, don't load the address and name
Jeff Sharkey67609c72016-03-05 14:29:13 -0700336 if (DBG) Slog.d(TAG, "invalid bluetooth name and address stored");
Zhihai Xud31c3222012-10-31 16:08:57 -0700337 return;
338 }
fredc0f420372012-04-12 00:02:00 -0700339 mName = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME);
340 mAddress = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS);
Jeff Sharkey67609c72016-03-05 14:29:13 -0700341 if (DBG) Slog.d(TAG, "Stored bluetooth Name=" + mName + ",Address=" + mAddress);
fredc0f420372012-04-12 00:02:00 -0700342 }
343
fredc649fe492012-04-19 01:07:18 -0700344 /**
345 * Save the Bluetooth name and address in the persistent store.
346 * Only non-null values will be saved.
347 * @param name
348 * @param address
349 */
fredc0f420372012-04-12 00:02:00 -0700350 private void storeNameAndAddress(String name, String address) {
351 if (name != null) {
352 Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME, name);
fredc0f420372012-04-12 00:02:00 -0700353 mName = name;
Jeff Sharkey67609c72016-03-05 14:29:13 -0700354 if (DBG) Slog.d(TAG,"Stored Bluetooth name: " +
fredc649fe492012-04-19 01:07:18 -0700355 Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_NAME));
fredc0f420372012-04-12 00:02:00 -0700356 }
357
358 if (address != null) {
359 Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS, address);
fredc0f420372012-04-12 00:02:00 -0700360 mAddress=address;
Jeff Sharkey67609c72016-03-05 14:29:13 -0700361 if (DBG) Slog.d(TAG,"Stored Bluetoothaddress: " +
fredc649fe492012-04-19 01:07:18 -0700362 Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_ADDRESS));
fredc0f420372012-04-12 00:02:00 -0700363 }
Zhihai Xud31c3222012-10-31 16:08:57 -0700364
365 if ((name != null) && (address != null)) {
366 Settings.Secure.putInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 1);
367 }
fredc0f420372012-04-12 00:02:00 -0700368 }
369
370 public IBluetooth registerAdapter(IBluetoothManagerCallback callback){
Natalie Silvanovich55db6462014-05-01 16:12:23 -0700371 if (callback == null) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700372 Slog.w(TAG, "Callback is null in registerAdapter");
Natalie Silvanovich55db6462014-05-01 16:12:23 -0700373 return null;
374 }
fredc0f420372012-04-12 00:02:00 -0700375 Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_ADAPTER);
376 msg.obj = callback;
377 mHandler.sendMessage(msg);
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700378
379 return mBluetooth;
fredc0f420372012-04-12 00:02:00 -0700380 }
381
382 public void unregisterAdapter(IBluetoothManagerCallback callback) {
Natalie Silvanovich55db6462014-05-01 16:12:23 -0700383 if (callback == null) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700384 Slog.w(TAG, "Callback is null in unregisterAdapter");
Natalie Silvanovich55db6462014-05-01 16:12:23 -0700385 return;
386 }
fredc0f420372012-04-12 00:02:00 -0700387 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
388 "Need BLUETOOTH permission");
389 Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_ADAPTER);
390 msg.obj = callback;
391 mHandler.sendMessage(msg);
392 }
393
394 public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) {
395 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
396 "Need BLUETOOTH permission");
397 Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_STATE_CHANGE_CALLBACK);
398 msg.obj = callback;
399 mHandler.sendMessage(msg);
400 }
401
402 public void unregisterStateChangeCallback(IBluetoothStateChangeCallback callback) {
403 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
404 "Need BLUETOOTH permission");
405 Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK);
406 msg.obj = callback;
407 mHandler.sendMessage(msg);
408 }
409
410 public boolean isEnabled() {
Zhihai Xu6eb76522012-11-29 15:41:04 -0800411 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
412 (!checkIfCallerIsForegroundUser())) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700413 Slog.w(TAG,"isEnabled(): not allowed for non-active and non system user");
Zhihai Xu40874a02012-10-08 17:57:03 -0700414 return false;
415 }
416
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700417 try {
418 mBluetoothLock.readLock().lock();
419 if (mBluetooth != null) return mBluetooth.isEnabled();
420 } catch (RemoteException e) {
421 Slog.e(TAG, "isEnabled()", e);
422 } finally {
423 mBluetoothLock.readLock().unlock();
fredc0f420372012-04-12 00:02:00 -0700424 }
425 return false;
426 }
427
Christine Hallstrom995c90a2016-05-25 15:49:08 -0700428 public int getState() {
429 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
430 (!checkIfCallerIsForegroundUser())) {
431 Slog.w(TAG, "getState(): not allowed for non-active and non system user");
432 return BluetoothAdapter.STATE_OFF;
433 }
434
435 try {
436 mBluetoothLock.readLock().lock();
437 if (mBluetooth != null) return mBluetooth.getState();
438 } catch (RemoteException e) {
439 Slog.e(TAG, "getState()", e);
440 } finally {
441 mBluetoothLock.readLock().unlock();
442 }
443 return BluetoothAdapter.STATE_OFF;
444 }
445
Nitin Arorad055adb2015-03-02 15:03:51 -0800446 class ClientDeathRecipient implements IBinder.DeathRecipient {
447 public void binderDied() {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700448 if (DBG) Slog.d(TAG, "Binder is dead - unregister Ble App");
Nitin Arorad055adb2015-03-02 15:03:51 -0800449 if (mBleAppCount > 0) --mBleAppCount;
450
451 if (mBleAppCount == 0) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700452 if (DBG) Slog.d(TAG, "Disabling LE only mode after application crash");
Nitin Arorad055adb2015-03-02 15:03:51 -0800453 try {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700454 mBluetoothLock.readLock().lock();
Nitin Arorad055adb2015-03-02 15:03:51 -0800455 if (mBluetooth != null) {
456 mBluetooth.onBrEdrDown();
457 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700458 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700459 Slog.e(TAG,"Unable to call onBrEdrDown", e);
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700460 } finally {
461 mBluetoothLock.readLock().unlock();
Nitin Arorad055adb2015-03-02 15:03:51 -0800462 }
463 }
464 }
465 }
466
467 /** Internal death rec list */
468 Map<IBinder, ClientDeathRecipient> mBleApps = new HashMap<IBinder, ClientDeathRecipient>();
469
Wei Wang67d84162015-04-26 17:04:29 -0700470 @Override
471 public boolean isBleScanAlwaysAvailable() {
472 try {
473 return (Settings.Global.getInt(mContentResolver,
474 Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE)) != 0;
475 } catch (SettingNotFoundException e) {
476 }
477 return false;
478 }
479
Wei Wange4a744b2015-06-11 17:50:29 -0700480 // Monitor change of BLE scan only mode settings.
481 private void registerForBleScanModeChange() {
482 ContentObserver contentObserver = new ContentObserver(null) {
483 @Override
484 public void onChange(boolean selfChange) {
485 if (!isBleScanAlwaysAvailable()) {
486 disableBleScanMode();
487 clearBleApps();
488 try {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700489 mBluetoothLock.readLock().lock();
Wei Wange4a744b2015-06-11 17:50:29 -0700490 if (mBluetooth != null) mBluetooth.onBrEdrDown();
491 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700492 Slog.e(TAG, "error when disabling bluetooth", e);
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700493 } finally {
494 mBluetoothLock.readLock().unlock();
Wei Wange4a744b2015-06-11 17:50:29 -0700495 }
496 }
497 }
498 };
499
500 mContentResolver.registerContentObserver(
501 Settings.Global.getUriFor(Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE),
502 false, contentObserver);
503 }
504
505 // Disable ble scan only mode.
506 private void disableBleScanMode() {
507 try {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700508 mBluetoothLock.writeLock().lock();
Wei Wange4a744b2015-06-11 17:50:29 -0700509 if (mBluetooth != null && (mBluetooth.getState() != BluetoothAdapter.STATE_ON)) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700510 if (DBG) Slog.d(TAG, "Reseting the mEnable flag for clean disable");
Wei Wange4a744b2015-06-11 17:50:29 -0700511 mEnable = false;
512 }
513 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700514 Slog.e(TAG, "getState()", e);
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700515 } finally {
516 mBluetoothLock.writeLock().unlock();
Wei Wange4a744b2015-06-11 17:50:29 -0700517 }
518 }
519
Nitin Arorad055adb2015-03-02 15:03:51 -0800520 public int updateBleAppCount(IBinder token, boolean enable) {
521 if (enable) {
522 ClientDeathRecipient r = mBleApps.get(token);
523 if (r == null) {
524 ClientDeathRecipient deathRec = new ClientDeathRecipient();
525 try {
526 token.linkToDeath(deathRec, 0);
527 } catch (RemoteException ex) {
528 throw new IllegalArgumentException("Wake lock is already dead.");
529 }
530 mBleApps.put(token, deathRec);
531 synchronized (this) {
532 ++mBleAppCount;
533 }
Jeff Sharkey67609c72016-03-05 14:29:13 -0700534 if (DBG) Slog.d(TAG, "Registered for death Notification");
Nitin Arorad055adb2015-03-02 15:03:51 -0800535 }
536
537 } else {
538 ClientDeathRecipient r = mBleApps.get(token);
539 if (r != null) {
Wei Wange4a744b2015-06-11 17:50:29 -0700540 // Unregister death recipient as the app goes away.
541 token.unlinkToDeath(r, 0);
Nitin Arorad055adb2015-03-02 15:03:51 -0800542 mBleApps.remove(token);
543 synchronized (this) {
544 if (mBleAppCount > 0) --mBleAppCount;
545 }
Jeff Sharkey67609c72016-03-05 14:29:13 -0700546 if (DBG) Slog.d(TAG, "Unregistered for death Notification");
Nitin Arorad055adb2015-03-02 15:03:51 -0800547 }
548 }
Jeff Sharkey67609c72016-03-05 14:29:13 -0700549 if (DBG) Slog.d(TAG, "Updated BleAppCount" + mBleAppCount);
Nitin Arorad055adb2015-03-02 15:03:51 -0800550 if (mBleAppCount == 0 && mEnable) {
Wei Wange4a744b2015-06-11 17:50:29 -0700551 disableBleScanMode();
Nitin Arorad055adb2015-03-02 15:03:51 -0800552 }
553 return mBleAppCount;
554 }
555
Wei Wange4a744b2015-06-11 17:50:29 -0700556 // Clear all apps using BLE scan only mode.
557 private void clearBleApps() {
558 synchronized (this) {
559 mBleApps.clear();
560 mBleAppCount = 0;
561 }
562 }
563
Nitin Arorad055adb2015-03-02 15:03:51 -0800564 /** @hide*/
565 public boolean isBleAppPresent() {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700566 if (DBG) Slog.d(TAG, "isBleAppPresent() count: " + mBleAppCount);
Nitin Arorad055adb2015-03-02 15:03:51 -0800567 return (mBleAppCount > 0);
568 }
569
570 /**
571 * Action taken when GattService is turned off
572 */
573 private void onBluetoothGattServiceUp() {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700574 if (DBG) Slog.d(TAG,"BluetoothGatt Service is Up");
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700575 try {
576 mBluetoothLock.readLock().lock();
Nitin Arorabdfaa7f2015-04-29 12:35:03 -0700577 if (isBleAppPresent() == false && mBluetooth != null
578 && mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON) {
Nitin Arorad055adb2015-03-02 15:03:51 -0800579 mBluetooth.onLeServiceUp();
580
581 // waive WRITE_SECURE_SETTINGS permission check
582 long callingIdentity = Binder.clearCallingIdentity();
583 persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);
584 Binder.restoreCallingIdentity(callingIdentity);
585 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700586 } catch (RemoteException e) {
587 Slog.e(TAG,"Unable to call onServiceUp", e);
588 } finally {
589 mBluetoothLock.readLock().unlock();
Nitin Arorad055adb2015-03-02 15:03:51 -0800590 }
591 }
592
593 /**
594 * Inform BluetoothAdapter instances that BREDR part is down
595 * and turn off all service and stack if no LE app needs it
596 */
597 private void sendBrEdrDownCallback() {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700598 if (DBG) Slog.d(TAG,"Calling sendBrEdrDownCallback callbacks");
Nitin Arorabdfaa7f2015-04-29 12:35:03 -0700599
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700600 if (mBluetooth == null) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700601 Slog.w(TAG, "Bluetooth handle is null");
Nitin Arorabdfaa7f2015-04-29 12:35:03 -0700602 return;
603 }
Nitin Arorad055adb2015-03-02 15:03:51 -0800604
605 if (isBleAppPresent() == false) {
606 try {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700607 mBluetoothLock.readLock().lock();
608 if (mBluetooth != null) mBluetooth.onBrEdrDown();
609 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700610 Slog.e(TAG, "Call to onBrEdrDown() failed.", e);
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700611 } finally {
612 mBluetoothLock.readLock().unlock();
Nitin Arorad055adb2015-03-02 15:03:51 -0800613 }
Nitin Arorabdfaa7f2015-04-29 12:35:03 -0700614 } else {
615 // Need to stay at BLE ON. Disconnect all Gatt connections
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700616 try {
Nitin Arorabdfaa7f2015-04-29 12:35:03 -0700617 mBluetoothGatt.unregAll();
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700618 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700619 Slog.e(TAG, "Unable to disconnect all apps.", e);
Nitin Arorad055adb2015-03-02 15:03:51 -0800620 }
621 }
Nitin Arorad055adb2015-03-02 15:03:51 -0800622 }
623
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700624 public boolean enableNoAutoConnect()
625 {
626 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
627 "Need BLUETOOTH ADMIN permission");
Zhihai Xu40874a02012-10-08 17:57:03 -0700628
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700629 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700630 Slog.d(TAG,"enableNoAutoConnect(): mBluetooth =" + mBluetooth +
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700631 " mBinding = " + mBinding);
632 }
Martijn Coenen8385c5a2012-11-29 10:14:16 -0800633 int callingAppId = UserHandle.getAppId(Binder.getCallingUid());
634
635 if (callingAppId != Process.NFC_UID) {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700636 throw new SecurityException("no permission to enable Bluetooth quietly");
637 }
Martijn Coenen8385c5a2012-11-29 10:14:16 -0800638
Zhihai Xu401202b2012-12-03 11:36:21 -0800639 synchronized(mReceiver) {
640 mQuietEnableExternal = true;
641 mEnableExternal = true;
642 sendEnableMsg(true);
643 }
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700644 return true;
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700645 }
Ajay Panicker4bb48302016-03-31 14:14:27 -0700646
fredc0f420372012-04-12 00:02:00 -0700647 public boolean enable() {
Zhihai Xu6eb76522012-11-29 15:41:04 -0800648 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
649 (!checkIfCallerIsForegroundUser())) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700650 Slog.w(TAG,"enable(): not allowed for non-active and non system user");
Zhihai Xu40874a02012-10-08 17:57:03 -0700651 return false;
fredcf2458862012-04-16 15:18:27 -0700652 }
653
Zhihai Xu401202b2012-12-03 11:36:21 -0800654 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
655 "Need BLUETOOTH ADMIN permission");
656 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700657 Slog.d(TAG,"enable(): mBluetooth =" + mBluetooth +
Sanket Agarwal090bf552016-04-21 14:10:55 -0700658 " mBinding = " + mBinding + " mState = " + mState);
659 }
Zhihai Xu401202b2012-12-03 11:36:21 -0800660
661 synchronized(mReceiver) {
662 mQuietEnableExternal = false;
663 mEnableExternal = true;
664 // waive WRITE_SECURE_SETTINGS permission check
Zhihai Xu401202b2012-12-03 11:36:21 -0800665 sendEnableMsg(false);
666 }
Jeff Sharkey67609c72016-03-05 14:29:13 -0700667 if (DBG) Slog.d(TAG, "enable returning");
Zhihai Xu401202b2012-12-03 11:36:21 -0800668 return true;
fredc0f420372012-04-12 00:02:00 -0700669 }
670
671 public boolean disable(boolean persist) {
672 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
673 "Need BLUETOOTH ADMIN permissicacheNameAndAddresson");
Zhihai Xu40874a02012-10-08 17:57:03 -0700674
Zhihai Xu6eb76522012-11-29 15:41:04 -0800675 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
676 (!checkIfCallerIsForegroundUser())) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700677 Slog.w(TAG,"disable(): not allowed for non-active and non system user");
Zhihai Xu40874a02012-10-08 17:57:03 -0700678 return false;
679 }
680
fredcf2458862012-04-16 15:18:27 -0700681 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700682 Slog.d(TAG,"disable(): mBluetooth = " + mBluetooth +
Matthew Xiecdce0b92012-07-12 19:06:15 -0700683 " mBinding = " + mBinding);
684 }
fredcf2458862012-04-16 15:18:27 -0700685
Zhihai Xu401202b2012-12-03 11:36:21 -0800686 synchronized(mReceiver) {
687 if (persist) {
688 // waive WRITE_SECURE_SETTINGS permission check
689 long callingIdentity = Binder.clearCallingIdentity();
690 persistBluetoothSetting(BLUETOOTH_OFF);
691 Binder.restoreCallingIdentity(callingIdentity);
692 }
693 mEnableExternal = false;
694 sendDisableMsg();
695 }
fredc0f420372012-04-12 00:02:00 -0700696 return true;
697 }
698
fredc649fe492012-04-19 01:07:18 -0700699 public void unbindAndFinish() {
fredcf2458862012-04-16 15:18:27 -0700700 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700701 Slog.d(TAG,"unbindAndFinish(): " + mBluetooth +
Matthew Xiecdce0b92012-07-12 19:06:15 -0700702 " mBinding = " + mBinding);
fredcf2458862012-04-16 15:18:27 -0700703 }
704
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700705 try {
706 mBluetoothLock.writeLock().lock();
fredc0f420372012-04-12 00:02:00 -0700707 if (mUnbinding) return;
708 mUnbinding = true;
Pavlin Radoslavove47ec142016-06-01 22:25:18 -0700709 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
Pavlin Radoslavovc7dda102016-09-21 17:28:11 -0700710 mHandler.removeMessages(MESSAGE_BIND_PROFILE_SERVICE);
Zhihai Xu40874a02012-10-08 17:57:03 -0700711 if (mBluetooth != null) {
Andre Eisenbach305fdab2015-11-11 21:43:26 -0800712 //Unregister callback object
713 try {
714 mBluetooth.unregisterCallback(mBluetoothCallback);
715 } catch (RemoteException re) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700716 Slog.e(TAG, "Unable to unregister BluetoothCallback",re);
fredcbf072a72012-05-09 16:52:50 -0700717 }
Andre Eisenbach305fdab2015-11-11 21:43:26 -0800718
Jeff Sharkey67609c72016-03-05 14:29:13 -0700719 if (DBG) Slog.d(TAG, "Sending unbind request.");
Marie Janssen9db28eb2016-01-12 16:05:15 -0800720 mBluetoothBinder = null;
fredcd6883532012-04-25 17:46:13 -0700721 mBluetooth = null;
722 //Unbind
fredc0f420372012-04-12 00:02:00 -0700723 mContext.unbindService(mConnection);
fredcd6883532012-04-25 17:46:13 -0700724 mUnbinding = false;
Zhihai Xu40874a02012-10-08 17:57:03 -0700725 mBinding = false;
fredcf2458862012-04-16 15:18:27 -0700726 } else {
727 mUnbinding=false;
fredc0f420372012-04-12 00:02:00 -0700728 }
Nitin Arorad055adb2015-03-02 15:03:51 -0800729 mBluetoothGatt = null;
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700730 } finally {
731 mBluetoothLock.writeLock().unlock();
fredc0f420372012-04-12 00:02:00 -0700732 }
733 }
734
Matthew Xieddf7e472013-03-01 18:41:02 -0800735 public IBluetoothGatt getBluetoothGatt() {
736 // sync protection
737 return mBluetoothGatt;
738 }
739
Benjamin Franze8b98922014-11-12 15:57:54 +0000740 @Override
741 public boolean bindBluetoothProfileService(int bluetoothProfile,
742 IBluetoothProfileServiceConnection proxy) {
743 if (!mEnable) {
744 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700745 Slog.d(TAG, "Trying to bind to profile: " + bluetoothProfile +
Benjamin Franze8b98922014-11-12 15:57:54 +0000746 ", while Bluetooth was disabled");
747 }
748 return false;
749 }
750 synchronized (mProfileServices) {
751 ProfileServiceConnections psc = mProfileServices.get(new Integer(bluetoothProfile));
752 if (psc == null) {
753 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700754 Slog.d(TAG, "Creating new ProfileServiceConnections object for"
Benjamin Franze8b98922014-11-12 15:57:54 +0000755 + " profile: " + bluetoothProfile);
756 }
Benjamin Franz5b614592014-12-09 18:58:45 +0000757
758 if (bluetoothProfile != BluetoothProfile.HEADSET) return false;
759
760 Intent intent = new Intent(IBluetoothHeadset.class.getName());
Benjamin Franze8b98922014-11-12 15:57:54 +0000761 psc = new ProfileServiceConnections(intent);
Benjamin Franz5b614592014-12-09 18:58:45 +0000762 if (!psc.bindService()) return false;
763
Benjamin Franze8b98922014-11-12 15:57:54 +0000764 mProfileServices.put(new Integer(bluetoothProfile), psc);
Benjamin Franze8b98922014-11-12 15:57:54 +0000765 }
766 }
767
768 // Introducing a delay to give the client app time to prepare
769 Message addProxyMsg = mHandler.obtainMessage(MESSAGE_ADD_PROXY_DELAYED);
770 addProxyMsg.arg1 = bluetoothProfile;
771 addProxyMsg.obj = proxy;
772 mHandler.sendMessageDelayed(addProxyMsg, ADD_PROXY_DELAY_MS);
773 return true;
774 }
775
776 @Override
777 public void unbindBluetoothProfileService(int bluetoothProfile,
778 IBluetoothProfileServiceConnection proxy) {
779 synchronized (mProfileServices) {
780 ProfileServiceConnections psc = mProfileServices.get(new Integer(bluetoothProfile));
781 if (psc == null) {
782 return;
783 }
784 psc.removeProxy(proxy);
785 }
786 }
787
788 private void unbindAllBluetoothProfileServices() {
789 synchronized (mProfileServices) {
790 for (Integer i : mProfileServices.keySet()) {
791 ProfileServiceConnections psc = mProfileServices.get(i);
Benjamin Franz5b614592014-12-09 18:58:45 +0000792 try {
793 mContext.unbindService(psc);
794 } catch (IllegalArgumentException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700795 Slog.e(TAG, "Unable to unbind service with intent: " + psc.mIntent, e);
Benjamin Franz5b614592014-12-09 18:58:45 +0000796 }
Benjamin Franze8b98922014-11-12 15:57:54 +0000797 psc.removeAllProxies();
798 }
799 mProfileServices.clear();
800 }
801 }
802
803 /**
Miao Chou658bf2f2015-06-26 17:14:35 -0700804 * Send enable message and set adapter name and address. Called when the boot phase becomes
805 * PHASE_SYSTEM_SERVICES_READY.
806 */
807 public void handleOnBootPhase() {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700808 if (DBG) Slog.d(TAG, "Bluetooth boot completed");
Miao Chou658bf2f2015-06-26 17:14:35 -0700809 if (mEnableExternal && isBluetoothPersistedStateOnBluetooth()) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700810 if (DBG) Slog.d(TAG, "Auto-enabling Bluetooth.");
Miao Chou658bf2f2015-06-26 17:14:35 -0700811 sendEnableMsg(mQuietEnableExternal);
Ajay Panickerbf796d82016-03-11 13:47:20 -0800812 } else if (!isNameAndAddressSet()) {
813 if (DBG) Slog.d(TAG, "Getting adapter name and address");
Ajay Panicker4bb48302016-03-31 14:14:27 -0700814 Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
815 mHandler.sendMessage(getMsg);
Miao Chou658bf2f2015-06-26 17:14:35 -0700816 }
Miao Chou658bf2f2015-06-26 17:14:35 -0700817 }
818
819 /**
820 * Called when switching to a different foreground user.
821 */
822 public void handleOnSwitchUser(int userHandle) {
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -0700823 if (DBG) Slog.d(TAG, "User " + userHandle + " switched");
824 mHandler.obtainMessage(MESSAGE_USER_SWITCHED, userHandle, 0).sendToTarget();
825 }
826
827 /**
828 * Called when user is unlocked.
829 */
830 public void handleOnUnlockUser(int userHandle) {
831 if (DBG) Slog.d(TAG, "User " + userHandle + " unlocked");
832 mHandler.obtainMessage(MESSAGE_USER_UNLOCKED, userHandle, 0).sendToTarget();
Miao Chou658bf2f2015-06-26 17:14:35 -0700833 }
834
835 /**
Benjamin Franze8b98922014-11-12 15:57:54 +0000836 * This class manages the clients connected to a given ProfileService
837 * and maintains the connection with that service.
838 */
839 final private class ProfileServiceConnections implements ServiceConnection,
840 IBinder.DeathRecipient {
841 final RemoteCallbackList<IBluetoothProfileServiceConnection> mProxies =
842 new RemoteCallbackList <IBluetoothProfileServiceConnection>();
843 IBinder mService;
844 ComponentName mClassName;
845 Intent mIntent;
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700846 boolean mInvokingProxyCallbacks = false;
Benjamin Franze8b98922014-11-12 15:57:54 +0000847
848 ProfileServiceConnections(Intent intent) {
849 mService = null;
850 mClassName = null;
851 mIntent = intent;
852 }
853
Benjamin Franz5b614592014-12-09 18:58:45 +0000854 private boolean bindService() {
855 if (mIntent != null && mService == null &&
856 doBind(mIntent, this, 0, UserHandle.CURRENT_OR_SELF)) {
Benjamin Franze8b98922014-11-12 15:57:54 +0000857 Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE);
858 msg.obj = this;
859 mHandler.sendMessageDelayed(msg, TIMEOUT_BIND_MS);
Benjamin Franz5b614592014-12-09 18:58:45 +0000860 return true;
Benjamin Franze8b98922014-11-12 15:57:54 +0000861 }
Jeff Sharkey67609c72016-03-05 14:29:13 -0700862 Slog.w(TAG, "Unable to bind with intent: " + mIntent);
Benjamin Franz5b614592014-12-09 18:58:45 +0000863 return false;
Benjamin Franze8b98922014-11-12 15:57:54 +0000864 }
865
866 private void addProxy(IBluetoothProfileServiceConnection proxy) {
867 mProxies.register(proxy);
868 if (mService != null) {
869 try{
870 proxy.onServiceConnected(mClassName, mService);
871 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700872 Slog.e(TAG, "Unable to connect to proxy", e);
Benjamin Franze8b98922014-11-12 15:57:54 +0000873 }
874 } else {
875 if (!mHandler.hasMessages(MESSAGE_BIND_PROFILE_SERVICE, this)) {
876 Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE);
877 msg.obj = this;
878 mHandler.sendMessage(msg);
879 }
880 }
881 }
882
883 private void removeProxy(IBluetoothProfileServiceConnection proxy) {
884 if (proxy != null) {
885 if (mProxies.unregister(proxy)) {
886 try {
887 proxy.onServiceDisconnected(mClassName);
888 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700889 Slog.e(TAG, "Unable to disconnect proxy", e);
Benjamin Franze8b98922014-11-12 15:57:54 +0000890 }
891 }
892 } else {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700893 Slog.w(TAG, "Trying to remove a null proxy");
Benjamin Franze8b98922014-11-12 15:57:54 +0000894 }
895 }
896
897 private void removeAllProxies() {
898 onServiceDisconnected(mClassName);
899 mProxies.kill();
900 }
901
902 @Override
903 public void onServiceConnected(ComponentName className, IBinder service) {
904 // remove timeout message
905 mHandler.removeMessages(MESSAGE_BIND_PROFILE_SERVICE, this);
906 mService = service;
907 mClassName = className;
908 try {
909 mService.linkToDeath(this, 0);
910 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700911 Slog.e(TAG, "Unable to linkToDeath", e);
Benjamin Franze8b98922014-11-12 15:57:54 +0000912 }
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700913
914 if (mInvokingProxyCallbacks) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700915 Slog.e(TAG, "Proxy callbacks already in progress.");
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700916 return;
Benjamin Franze8b98922014-11-12 15:57:54 +0000917 }
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700918 mInvokingProxyCallbacks = true;
919
920 final int n = mProxies.beginBroadcast();
921 try {
922 for (int i = 0; i < n; i++) {
923 try {
924 mProxies.getBroadcastItem(i).onServiceConnected(className, service);
925 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700926 Slog.e(TAG, "Unable to connect to proxy", e);
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700927 }
928 }
929 } finally {
930 mProxies.finishBroadcast();
931 mInvokingProxyCallbacks = false;
932 }
Benjamin Franze8b98922014-11-12 15:57:54 +0000933 }
934
935 @Override
936 public void onServiceDisconnected(ComponentName className) {
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700937 if (mService == null) return;
Benjamin Franze8b98922014-11-12 15:57:54 +0000938 mService.unlinkToDeath(this, 0);
939 mService = null;
940 mClassName = null;
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700941
942 if (mInvokingProxyCallbacks) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700943 Slog.e(TAG, "Proxy callbacks already in progress.");
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700944 return;
Benjamin Franze8b98922014-11-12 15:57:54 +0000945 }
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700946 mInvokingProxyCallbacks = true;
947
948 final int n = mProxies.beginBroadcast();
949 try {
950 for (int i = 0; i < n; i++) {
951 try {
952 mProxies.getBroadcastItem(i).onServiceDisconnected(className);
953 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700954 Slog.e(TAG, "Unable to disconnect from proxy", e);
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700955 }
956 }
957 } finally {
958 mProxies.finishBroadcast();
959 mInvokingProxyCallbacks = false;
960 }
Benjamin Franze8b98922014-11-12 15:57:54 +0000961 }
962
963 @Override
964 public void binderDied() {
965 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700966 Slog.w(TAG, "Profile service for profile: " + mClassName
Benjamin Franze8b98922014-11-12 15:57:54 +0000967 + " died.");
968 }
969 onServiceDisconnected(mClassName);
970 // Trigger rebind
971 Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE);
972 msg.obj = this;
973 mHandler.sendMessageDelayed(msg, TIMEOUT_BIND_MS);
974 }
975 }
976
fredcbf072a72012-05-09 16:52:50 -0700977 private void sendBluetoothStateCallback(boolean isUp) {
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700978 try {
979 int n = mStateChangeCallbacks.beginBroadcast();
Jeff Sharkey67609c72016-03-05 14:29:13 -0700980 if (DBG) Slog.d(TAG,"Broadcasting onBluetoothStateChange("+isUp+") to " + n + " receivers.");
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700981 for (int i=0; i <n;i++) {
982 try {
983 mStateChangeCallbacks.getBroadcastItem(i).onBluetoothStateChange(isUp);
984 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700985 Slog.e(TAG, "Unable to call onBluetoothStateChange() on callback #" + i , e);
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700986 }
fredcbf072a72012-05-09 16:52:50 -0700987 }
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700988 } finally {
989 mStateChangeCallbacks.finishBroadcast();
fredcbf072a72012-05-09 16:52:50 -0700990 }
fredcbf072a72012-05-09 16:52:50 -0700991 }
992
993 /**
Zhihai Xu40874a02012-10-08 17:57:03 -0700994 * Inform BluetoothAdapter instances that Adapter service is up
995 */
996 private void sendBluetoothServiceUpCallback() {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700997 if (DBG) Slog.d(TAG,"Calling onBluetoothServiceUp callbacks");
Andre Eisenbach305fdab2015-11-11 21:43:26 -0800998 try {
999 int n = mCallbacks.beginBroadcast();
Jeff Sharkey67609c72016-03-05 14:29:13 -07001000 Slog.d(TAG,"Broadcasting onBluetoothServiceUp() to " + n + " receivers.");
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001001 for (int i=0; i <n;i++) {
1002 try {
1003 mCallbacks.getBroadcastItem(i).onBluetoothServiceUp(mBluetooth);
1004 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001005 Slog.e(TAG, "Unable to call onBluetoothServiceUp() on callback #" + i, e);
Zhihai Xu40874a02012-10-08 17:57:03 -07001006 }
1007 }
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001008 } finally {
1009 mCallbacks.finishBroadcast();
Zhihai Xu40874a02012-10-08 17:57:03 -07001010 }
1011 }
1012 /**
fredcbf072a72012-05-09 16:52:50 -07001013 * Inform BluetoothAdapter instances that Adapter service is down
1014 */
1015 private void sendBluetoothServiceDownCallback() {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001016 if (DBG) Slog.d(TAG,"Calling onBluetoothServiceDown callbacks");
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001017 try {
1018 int n = mCallbacks.beginBroadcast();
Jeff Sharkey67609c72016-03-05 14:29:13 -07001019 Slog.d(TAG,"Broadcasting onBluetoothServiceDown() to " + n + " receivers.");
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001020 for (int i=0; i <n;i++) {
1021 try {
1022 mCallbacks.getBroadcastItem(i).onBluetoothServiceDown();
1023 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001024 Slog.e(TAG, "Unable to call onBluetoothServiceDown() on callback #" + i, e);
fredcd6883532012-04-25 17:46:13 -07001025 }
1026 }
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001027 } finally {
1028 mCallbacks.finishBroadcast();
fredcd6883532012-04-25 17:46:13 -07001029 }
1030 }
Svet Ganov408abf72015-05-12 19:13:36 -07001031
fredc0f420372012-04-12 00:02:00 -07001032 public String getAddress() {
Matthew Xieaf5ddbf2012-12-04 10:47:43 -08001033 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
Svet Ganov408abf72015-05-12 19:13:36 -07001034 "Need BLUETOOTH permission");
Zhihai Xu40874a02012-10-08 17:57:03 -07001035
Zhihai Xu6eb76522012-11-29 15:41:04 -08001036 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
Svet Ganov408abf72015-05-12 19:13:36 -07001037 (!checkIfCallerIsForegroundUser())) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001038 Slog.w(TAG,"getAddress(): not allowed for non-active and non system user");
Zhihai Xu6eb76522012-11-29 15:41:04 -08001039 return null;
Zhihai Xu40874a02012-10-08 17:57:03 -07001040 }
1041
Svet Ganov408abf72015-05-12 19:13:36 -07001042 if (mContext.checkCallingOrSelfPermission(Manifest.permission.LOCAL_MAC_ADDRESS)
1043 != PackageManager.PERMISSION_GRANTED) {
1044 return BluetoothAdapter.DEFAULT_MAC_ADDRESS;
1045 }
1046
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001047 try {
1048 mBluetoothLock.readLock().lock();
1049 if (mBluetooth != null) return mBluetooth.getAddress();
1050 } catch (RemoteException e) {
1051 Slog.e(TAG, "getAddress(): Unable to retrieve address remotely. Returning cached address", e);
1052 } finally {
1053 mBluetoothLock.readLock().unlock();
fredc116d1d462012-04-20 14:47:08 -07001054 }
Ajay Panickerbf796d82016-03-11 13:47:20 -08001055
Matthew Xiecdce0b92012-07-12 19:06:15 -07001056 // mAddress is accessed from outside.
1057 // It is alright without a lock. Here, bluetooth is off, no other thread is
1058 // changing mAddress
fredc0f420372012-04-12 00:02:00 -07001059 return mAddress;
1060 }
fredc649fe492012-04-19 01:07:18 -07001061
fredc0f420372012-04-12 00:02:00 -07001062 public String getName() {
Matthew Xieaf5ddbf2012-12-04 10:47:43 -08001063 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
1064 "Need BLUETOOTH permission");
Zhihai Xu40874a02012-10-08 17:57:03 -07001065
Zhihai Xu6eb76522012-11-29 15:41:04 -08001066 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
1067 (!checkIfCallerIsForegroundUser())) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001068 Slog.w(TAG,"getName(): not allowed for non-active and non system user");
Zhihai Xu6eb76522012-11-29 15:41:04 -08001069 return null;
Zhihai Xu40874a02012-10-08 17:57:03 -07001070 }
1071
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001072 try {
1073 mBluetoothLock.readLock().lock();
1074 if (mBluetooth != null) return mBluetooth.getName();
1075 } catch (RemoteException e) {
1076 Slog.e(TAG, "getName(): Unable to retrieve name remotely. Returning cached name", e);
1077 } finally {
1078 mBluetoothLock.readLock().unlock();
fredc116d1d462012-04-20 14:47:08 -07001079 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001080
Matthew Xiecdce0b92012-07-12 19:06:15 -07001081 // mName is accessed from outside.
1082 // It alright without a lock. Here, bluetooth is off, no other thread is
1083 // changing mName
fredc0f420372012-04-12 00:02:00 -07001084 return mName;
1085 }
1086
fredc0f420372012-04-12 00:02:00 -07001087 private class BluetoothServiceConnection implements ServiceConnection {
fredc0f420372012-04-12 00:02:00 -07001088 public void onServiceConnected(ComponentName className, IBinder service) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001089 if (DBG) Slog.d(TAG, "BluetoothServiceConnection: " + className.getClassName());
fredc0f420372012-04-12 00:02:00 -07001090 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED);
Matthew Xieddf7e472013-03-01 18:41:02 -08001091 // TBD if (className.getClassName().equals(IBluetooth.class.getName())) {
1092 if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) {
1093 msg.arg1 = SERVICE_IBLUETOOTH;
1094 // } else if (className.getClassName().equals(IBluetoothGatt.class.getName())) {
1095 } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) {
1096 msg.arg1 = SERVICE_IBLUETOOTHGATT;
1097 } else {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001098 Slog.e(TAG, "Unknown service connected: " + className.getClassName());
Matthew Xieddf7e472013-03-01 18:41:02 -08001099 return;
1100 }
fredc0f420372012-04-12 00:02:00 -07001101 msg.obj = service;
1102 mHandler.sendMessage(msg);
1103 }
1104
1105 public void onServiceDisconnected(ComponentName className) {
fredc0f420372012-04-12 00:02:00 -07001106 // Called if we unexpected disconnected.
Jeff Sharkey67609c72016-03-05 14:29:13 -07001107 if (DBG) Slog.d(TAG, "BluetoothServiceConnection, disconnected: " +
Matthew Xieddf7e472013-03-01 18:41:02 -08001108 className.getClassName());
fredc0f420372012-04-12 00:02:00 -07001109 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED);
Matthew Xieddf7e472013-03-01 18:41:02 -08001110 if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) {
1111 msg.arg1 = SERVICE_IBLUETOOTH;
1112 } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) {
1113 msg.arg1 = SERVICE_IBLUETOOTHGATT;
1114 } else {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001115 Slog.e(TAG, "Unknown service disconnected: " + className.getClassName());
Matthew Xieddf7e472013-03-01 18:41:02 -08001116 return;
1117 }
fredc0f420372012-04-12 00:02:00 -07001118 mHandler.sendMessage(msg);
1119 }
1120 }
1121
1122 private BluetoothServiceConnection mConnection = new BluetoothServiceConnection();
1123
Zhihai Xu40874a02012-10-08 17:57:03 -07001124 private class BluetoothHandler extends Handler {
Ajay Panicker4bb48302016-03-31 14:14:27 -07001125 boolean mGetNameAddressOnly = false;
1126
Zhihai Xu40874a02012-10-08 17:57:03 -07001127 public BluetoothHandler(Looper looper) {
1128 super(looper);
1129 }
1130
fredc0f420372012-04-12 00:02:00 -07001131 @Override
1132 public void handleMessage(Message msg) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001133 if (DBG) Slog.d (TAG, "Message: " + msg.what);
fredc0f420372012-04-12 00:02:00 -07001134 switch (msg.what) {
Ajay Panicker4bb48302016-03-31 14:14:27 -07001135 case MESSAGE_GET_NAME_AND_ADDRESS:
1136 if (DBG) Slog.d(TAG, "MESSAGE_GET_NAME_AND_ADDRESS");
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001137 try {
1138 mBluetoothLock.writeLock().lock();
Ajay Panicker4bb48302016-03-31 14:14:27 -07001139 if ((mBluetooth == null) && (!mBinding)) {
1140 if (DBG) Slog.d(TAG, "Binding to service to get name and address");
1141 mGetNameAddressOnly = true;
1142 Message timeoutMsg = mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
1143 mHandler.sendMessageDelayed(timeoutMsg, TIMEOUT_BIND_MS);
1144 Intent i = new Intent(IBluetooth.class.getName());
1145 if (!doBind(i, mConnection,
1146 Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT,
1147 UserHandle.CURRENT)) {
1148 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
1149 } else {
1150 mBinding = true;
1151 }
1152 } else if (mBluetooth != null) {
1153 try {
1154 storeNameAndAddress(mBluetooth.getName(),
1155 mBluetooth.getAddress());
1156 } catch (RemoteException re) {
1157 Slog.e(TAG, "Unable to grab names", re);
1158 }
1159 if (mGetNameAddressOnly && !mEnable) {
1160 unbindAndFinish();
1161 }
1162 mGetNameAddressOnly = false;
1163 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001164 } finally {
1165 mBluetoothLock.writeLock().unlock();
Ajay Panicker4bb48302016-03-31 14:14:27 -07001166 }
1167 break;
1168
Matthew Xiecdce0b92012-07-12 19:06:15 -07001169 case MESSAGE_ENABLE:
fredcf2458862012-04-16 15:18:27 -07001170 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001171 Slog.d(TAG, "MESSAGE_ENABLE: mBluetooth = " + mBluetooth);
fredc649fe492012-04-19 01:07:18 -07001172 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001173 mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
1174 mEnable = true;
Calvin Ona0b91d72016-06-15 17:58:23 -07001175
1176 // Use service interface to get the exact state
1177 try {
1178 mBluetoothLock.readLock().lock();
1179 if (mBluetooth != null) {
1180 int state = mBluetooth.getState();
1181 if (state == BluetoothAdapter.STATE_BLE_ON) {
1182 Slog.w(TAG, "BT is in BLE_ON State");
1183 mBluetooth.onLeServiceUp();
1184 break;
1185 }
1186 }
1187 } catch (RemoteException e) {
1188 Slog.e(TAG, "", e);
1189 } finally {
1190 mBluetoothLock.readLock().unlock();
1191 }
1192
1193 mQuietEnable = (msg.arg1 == 1);
Pavlin Radoslavove47ec142016-06-01 22:25:18 -07001194 if (mBluetooth == null) {
Calvin Ona0b91d72016-06-15 17:58:23 -07001195 handleEnable(mQuietEnable);
Pavlin Radoslavove47ec142016-06-01 22:25:18 -07001196 } else {
1197 //
1198 // We need to wait until transitioned to STATE_OFF and
1199 // the previous Bluetooth process has exited. The
1200 // waiting period has three components:
1201 // (a) Wait until the local state is STATE_OFF. This
1202 // is accomplished by "waitForOnOff(false, true)".
1203 // (b) Wait until the STATE_OFF state is updated to
1204 // all components.
1205 // (c) Wait until the Bluetooth process exits, and
1206 // ActivityManager detects it.
1207 // The waiting for (b) and (c) is accomplished by
1208 // delaying the MESSAGE_RESTART_BLUETOOTH_SERVICE
1209 // message. On slower devices, that delay needs to be
1210 // on the order of (2 * SERVICE_RESTART_TIME_MS).
1211 //
1212 waitForOnOff(false, true);
Pavlin Radoslavove47ec142016-06-01 22:25:18 -07001213 Message restartMsg = mHandler.obtainMessage(
1214 MESSAGE_RESTART_BLUETOOTH_SERVICE);
1215 mHandler.sendMessageDelayed(restartMsg,
1216 2 * SERVICE_RESTART_TIME_MS);
1217 }
fredc649fe492012-04-19 01:07:18 -07001218 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001219
fredc0f420372012-04-12 00:02:00 -07001220 case MESSAGE_DISABLE:
Zhihai Xu40874a02012-10-08 17:57:03 -07001221 mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
1222 if (mEnable && mBluetooth != null) {
1223 waitForOnOff(true, false);
1224 mEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -08001225 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -07001226 waitForOnOff(false, false);
1227 } else {
1228 mEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -08001229 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -07001230 }
fredc0f420372012-04-12 00:02:00 -07001231 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001232
fredc0f420372012-04-12 00:02:00 -07001233 case MESSAGE_REGISTER_ADAPTER:
1234 {
1235 IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -07001236 boolean added = mCallbacks.register(callback);
Jeff Sharkey67609c72016-03-05 14:29:13 -07001237 Slog.d(TAG,"Added callback: " + (callback == null? "null": callback) +":" +added );
fredc0f420372012-04-12 00:02:00 -07001238 }
1239 break;
1240 case MESSAGE_UNREGISTER_ADAPTER:
1241 {
1242 IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -07001243 boolean removed = mCallbacks.unregister(callback);
Jeff Sharkey67609c72016-03-05 14:29:13 -07001244 Slog.d(TAG,"Removed callback: " + (callback == null? "null": callback) +":" + removed);
fredc0f420372012-04-12 00:02:00 -07001245 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001246 }
fredc0f420372012-04-12 00:02:00 -07001247 case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK:
1248 {
1249 IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj;
Matthew Xie9b693992013-10-10 11:21:40 -07001250 if (callback != null) {
1251 mStateChangeCallbacks.register(callback);
1252 }
fredc0f420372012-04-12 00:02:00 -07001253 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001254 }
fredc0f420372012-04-12 00:02:00 -07001255 case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK:
1256 {
1257 IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj;
Matthew Xie9b693992013-10-10 11:21:40 -07001258 if (callback != null) {
1259 mStateChangeCallbacks.unregister(callback);
1260 }
fredc0f420372012-04-12 00:02:00 -07001261 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001262 }
Benjamin Franze8b98922014-11-12 15:57:54 +00001263 case MESSAGE_ADD_PROXY_DELAYED:
1264 {
1265 ProfileServiceConnections psc = mProfileServices.get(
1266 new Integer(msg.arg1));
1267 if (psc == null) {
1268 break;
1269 }
1270 IBluetoothProfileServiceConnection proxy =
1271 (IBluetoothProfileServiceConnection) msg.obj;
1272 psc.addProxy(proxy);
1273 break;
1274 }
1275 case MESSAGE_BIND_PROFILE_SERVICE:
1276 {
1277 ProfileServiceConnections psc = (ProfileServiceConnections) msg.obj;
1278 removeMessages(MESSAGE_BIND_PROFILE_SERVICE, msg.obj);
1279 if (psc == null) {
1280 break;
1281 }
1282 psc.bindService();
1283 break;
1284 }
fredc0f420372012-04-12 00:02:00 -07001285 case MESSAGE_BLUETOOTH_SERVICE_CONNECTED:
1286 {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001287 if (DBG) Slog.d(TAG,"MESSAGE_BLUETOOTH_SERVICE_CONNECTED: " + msg.arg1);
fredc0f420372012-04-12 00:02:00 -07001288
1289 IBinder service = (IBinder) msg.obj;
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001290 try {
1291 mBluetoothLock.writeLock().lock();
Matthew Xieddf7e472013-03-01 18:41:02 -08001292 if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
1293 mBluetoothGatt = IBluetoothGatt.Stub.asInterface(service);
Nitin Arorad055adb2015-03-02 15:03:51 -08001294 onBluetoothGattServiceUp();
Matthew Xieddf7e472013-03-01 18:41:02 -08001295 break;
1296 } // else must be SERVICE_IBLUETOOTH
1297
1298 //Remove timeout
Zhihai Xuaf5971e2013-06-10 20:28:31 -07001299 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
Matthew Xieddf7e472013-03-01 18:41:02 -08001300
fredc0f420372012-04-12 00:02:00 -07001301 mBinding = false;
Marie Janssen9db28eb2016-01-12 16:05:15 -08001302 mBluetoothBinder = service;
fredc0f420372012-04-12 00:02:00 -07001303 mBluetooth = IBluetooth.Stub.asInterface(service);
fredc0f420372012-04-12 00:02:00 -07001304
Ajay Panicker4bb48302016-03-31 14:14:27 -07001305 if (!isNameAndAddressSet()) {
1306 Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
1307 mHandler.sendMessage(getMsg);
1308 if (mGetNameAddressOnly) return;
1309 }
1310
Zhihai Xuaf5971e2013-06-10 20:28:31 -07001311 try {
1312 boolean enableHciSnoopLog = (Settings.Secure.getInt(mContentResolver,
1313 Settings.Secure.BLUETOOTH_HCI_LOG, 0) == 1);
1314 if (!mBluetooth.configHciSnoopLog(enableHciSnoopLog)) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001315 Slog.e(TAG,"IBluetooth.configHciSnoopLog return false");
Zhihai Xuaf5971e2013-06-10 20:28:31 -07001316 }
1317 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001318 Slog.e(TAG,"Unable to call configHciSnoopLog", e);
Zhihai Xuaf5971e2013-06-10 20:28:31 -07001319 }
1320
Matthew Xiecdce0b92012-07-12 19:06:15 -07001321 //Register callback object
fredcbf072a72012-05-09 16:52:50 -07001322 try {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001323 mBluetooth.registerCallback(mBluetoothCallback);
1324 } catch (RemoteException re) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001325 Slog.e(TAG, "Unable to register BluetoothCallback",re);
fredcbf072a72012-05-09 16:52:50 -07001326 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001327 //Inform BluetoothAdapter instances that service is up
Zhihai Xu40874a02012-10-08 17:57:03 -07001328 sendBluetoothServiceUpCallback();
1329
Matthew Xiecdce0b92012-07-12 19:06:15 -07001330 //Do enable request
1331 try {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001332 if (mQuietEnable == false) {
1333 if(!mBluetooth.enable()) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001334 Slog.e(TAG,"IBluetooth.enable() returned false");
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001335 }
1336 }
1337 else
1338 {
1339 if(!mBluetooth.enableNoAutoConnect()) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001340 Slog.e(TAG,"IBluetooth.enableNoAutoConnect() returned false");
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001341 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001342 }
1343 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001344 Slog.e(TAG,"Unable to call enable()",e);
Matthew Xiecdce0b92012-07-12 19:06:15 -07001345 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001346 } finally {
1347 mBluetoothLock.writeLock().unlock();
Freda8c6df02012-07-11 10:25:23 -07001348 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001349
1350 if (!mEnable) {
1351 waitForOnOff(true, false);
Zhihai Xu401202b2012-12-03 11:36:21 -08001352 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -07001353 waitForOnOff(false, false);
1354 }
fredc649fe492012-04-19 01:07:18 -07001355 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001356 }
fredc649fe492012-04-19 01:07:18 -07001357 case MESSAGE_TIMEOUT_BIND: {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001358 Slog.e(TAG, "MESSAGE_TIMEOUT_BIND");
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001359 mBluetoothLock.writeLock().lock();
1360 mBinding = false;
1361 mBluetoothLock.writeLock().unlock();
1362
fredc649fe492012-04-19 01:07:18 -07001363 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001364 }
fredcbf072a72012-05-09 16:52:50 -07001365 case MESSAGE_BLUETOOTH_STATE_CHANGE:
fredc0f420372012-04-12 00:02:00 -07001366 {
fredcbf072a72012-05-09 16:52:50 -07001367 int prevState = msg.arg1;
1368 int newState = msg.arg2;
Jeff Sharkey67609c72016-03-05 14:29:13 -07001369 if (DBG) Slog.d(TAG, "MESSAGE_BLUETOOTH_STATE_CHANGE: prevState = " + prevState + ", newState=" + newState);
Zhihai Xu40874a02012-10-08 17:57:03 -07001370 mState = newState;
1371 bluetoothStateChangeHandler(prevState, newState);
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001372 // handle error state transition case from TURNING_ON to OFF
1373 // unbind and rebind bluetooth service and enable bluetooth
Nitin Arorad055adb2015-03-02 15:03:51 -08001374 if ((prevState == BluetoothAdapter.STATE_BLE_TURNING_ON) &&
Calvin Ona0b91d72016-06-15 17:58:23 -07001375 (newState == BluetoothAdapter.STATE_OFF) &&
1376 (mBluetooth != null) && mEnable) {
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001377 recoverBluetoothServiceFromError();
1378 }
Nitin Arorad055adb2015-03-02 15:03:51 -08001379 if ((prevState == BluetoothAdapter.STATE_TURNING_ON) &&
Calvin Ona0b91d72016-06-15 17:58:23 -07001380 (newState == BluetoothAdapter.STATE_BLE_ON) &&
1381 (mBluetooth != null) && mEnable) {
Nitin Arorad055adb2015-03-02 15:03:51 -08001382 recoverBluetoothServiceFromError();
1383 }
Calvin Ona0b91d72016-06-15 17:58:23 -07001384 // If we tried to enable BT while BT was in the process of shutting down,
1385 // wait for the BT process to fully tear down and then force a restart
1386 // here. This is a bit of a hack (b/29363429).
1387 if ((prevState == BluetoothAdapter.STATE_BLE_TURNING_OFF) &&
1388 (newState == BluetoothAdapter.STATE_OFF)) {
1389 if (mEnable) {
1390 Slog.d(TAG, "Entering STATE_OFF but mEnabled is true; restarting.");
1391 waitForOnOff(false, true);
1392 Message restartMsg = mHandler.obtainMessage(
1393 MESSAGE_RESTART_BLUETOOTH_SERVICE);
1394 mHandler.sendMessageDelayed(restartMsg, 2 * SERVICE_RESTART_TIME_MS);
1395 }
1396 }
Nitin Arorad055adb2015-03-02 15:03:51 -08001397 if (newState == BluetoothAdapter.STATE_ON ||
Calvin Ona0b91d72016-06-15 17:58:23 -07001398 newState == BluetoothAdapter.STATE_BLE_ON) {
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001399 // bluetooth is working, reset the counter
1400 if (mErrorRecoveryRetryCounter != 0) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001401 Slog.w(TAG, "bluetooth is recovered from error");
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001402 mErrorRecoveryRetryCounter = 0;
1403 }
1404 }
fredc649fe492012-04-19 01:07:18 -07001405 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001406 }
fredc0f420372012-04-12 00:02:00 -07001407 case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED:
1408 {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001409 Slog.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: " + msg.arg1);
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001410 try {
1411 mBluetoothLock.writeLock().lock();
Matthew Xieddf7e472013-03-01 18:41:02 -08001412 if (msg.arg1 == SERVICE_IBLUETOOTH) {
1413 // if service is unbinded already, do nothing and return
1414 if (mBluetooth == null) break;
1415 mBluetooth = null;
1416 } else if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
1417 mBluetoothGatt = null;
1418 break;
1419 } else {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001420 Slog.e(TAG, "Bad msg.arg1: " + msg.arg1);
Matthew Xieddf7e472013-03-01 18:41:02 -08001421 break;
1422 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001423 } finally {
1424 mBluetoothLock.writeLock().unlock();
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301425 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001426
1427 if (mEnable) {
1428 mEnable = false;
1429 // Send a Bluetooth Restart message
1430 Message restartMsg = mHandler.obtainMessage(
1431 MESSAGE_RESTART_BLUETOOTH_SERVICE);
1432 mHandler.sendMessageDelayed(restartMsg,
1433 SERVICE_RESTART_TIME_MS);
1434 }
1435
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001436 sendBluetoothServiceDownCallback();
Zhihai Xu40874a02012-10-08 17:57:03 -07001437
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001438 // Send BT state broadcast to update
1439 // the BT icon correctly
1440 if ((mState == BluetoothAdapter.STATE_TURNING_ON) ||
Calvin Ona0b91d72016-06-15 17:58:23 -07001441 (mState == BluetoothAdapter.STATE_ON)) {
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001442 bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,
1443 BluetoothAdapter.STATE_TURNING_OFF);
1444 mState = BluetoothAdapter.STATE_TURNING_OFF;
Zhihai Xu40874a02012-10-08 17:57:03 -07001445 }
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001446 if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
1447 bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
1448 BluetoothAdapter.STATE_OFF);
1449 }
1450
1451 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
1452 mState = BluetoothAdapter.STATE_OFF;
fredc649fe492012-04-19 01:07:18 -07001453 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001454 }
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301455 case MESSAGE_RESTART_BLUETOOTH_SERVICE:
1456 {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001457 Slog.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE:"
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301458 +" Restart IBluetooth service");
1459 /* Enable without persisting the setting as
1460 it doesnt change when IBluetooth
1461 service restarts */
Zhihai Xu40874a02012-10-08 17:57:03 -07001462 mEnable = true;
Zhihai Xu401202b2012-12-03 11:36:21 -08001463 handleEnable(mQuietEnable);
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301464 break;
1465 }
1466
fredc0f420372012-04-12 00:02:00 -07001467 case MESSAGE_TIMEOUT_UNBIND:
1468 {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001469 Slog.e(TAG, "MESSAGE_TIMEOUT_UNBIND");
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001470 mBluetoothLock.writeLock().lock();
1471 mUnbinding = false;
1472 mBluetoothLock.writeLock().unlock();
fredc649fe492012-04-19 01:07:18 -07001473 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001474 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001475
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -07001476 case MESSAGE_USER_SWITCHED: {
1477 if (DBG) Slog.d(TAG, "MESSAGE_USER_SWITCHED");
Zhihai Xu40874a02012-10-08 17:57:03 -07001478 mHandler.removeMessages(MESSAGE_USER_SWITCHED);
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -07001479
Zhihai Xu40874a02012-10-08 17:57:03 -07001480 /* disable and enable BT when detect a user switch */
1481 if (mEnable && mBluetooth != null) {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001482 try {
1483 mBluetoothLock.readLock().lock();
Zhihai Xu40874a02012-10-08 17:57:03 -07001484 if (mBluetooth != null) {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001485 mBluetooth.unregisterCallback(mBluetoothCallback);
Zhihai Xu40874a02012-10-08 17:57:03 -07001486 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001487 } catch (RemoteException re) {
1488 Slog.e(TAG, "Unable to unregister", re);
1489 } finally {
1490 mBluetoothLock.readLock().unlock();
Zhihai Xu40874a02012-10-08 17:57:03 -07001491 }
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001492
1493 if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
1494 // MESSAGE_USER_SWITCHED happened right after MESSAGE_ENABLE
1495 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_OFF);
1496 mState = BluetoothAdapter.STATE_OFF;
1497 }
1498 if (mState == BluetoothAdapter.STATE_OFF) {
1499 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_TURNING_ON);
1500 mState = BluetoothAdapter.STATE_TURNING_ON;
1501 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001502
1503 waitForOnOff(true, false);
1504
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001505 if (mState == BluetoothAdapter.STATE_TURNING_ON) {
1506 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_ON);
1507 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001508
Benjamin Franze8b98922014-11-12 15:57:54 +00001509 unbindAllBluetoothProfileServices();
Zhihai Xu40874a02012-10-08 17:57:03 -07001510 // disable
Zhihai Xu401202b2012-12-03 11:36:21 -08001511 handleDisable();
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001512 // Pbap service need receive STATE_TURNING_OFF intent to close
1513 bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,
1514 BluetoothAdapter.STATE_TURNING_OFF);
Zhihai Xu40874a02012-10-08 17:57:03 -07001515
Pavlin Radoslavov41401112016-06-27 15:25:18 -07001516 boolean didDisableTimeout = !waitForOnOff(false, true);
Zhihai Xu40874a02012-10-08 17:57:03 -07001517
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001518 bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
Zhihai Xu40874a02012-10-08 17:57:03 -07001519 BluetoothAdapter.STATE_OFF);
Zhihai Xu40874a02012-10-08 17:57:03 -07001520 sendBluetoothServiceDownCallback();
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001521
Pavlin Radoslavove957a8a2016-05-24 15:28:41 -07001522 try {
1523 mBluetoothLock.writeLock().lock();
1524 if (mBluetooth != null) {
1525 mBluetooth = null;
1526 // Unbind
1527 mContext.unbindService(mConnection);
1528 }
1529 mBluetoothGatt = null;
1530 } finally {
1531 mBluetoothLock.writeLock().unlock();
Zhihai Xu40874a02012-10-08 17:57:03 -07001532 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001533
Pavlin Radoslavov41401112016-06-27 15:25:18 -07001534 //
1535 // If disabling Bluetooth times out, wait for an
1536 // additional amount of time to ensure the process is
1537 // shut down completely before attempting to restart.
1538 //
1539 if (didDisableTimeout) {
1540 SystemClock.sleep(3000);
1541 } else {
1542 SystemClock.sleep(100);
1543 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001544
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001545 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
1546 mState = BluetoothAdapter.STATE_OFF;
Zhihai Xu40874a02012-10-08 17:57:03 -07001547 // enable
Zhihai Xu401202b2012-12-03 11:36:21 -08001548 handleEnable(mQuietEnable);
John Spurlock8a985d22014-02-25 09:40:05 -05001549 } else if (mBinding || mBluetooth != null) {
Zhihai Xu40874a02012-10-08 17:57:03 -07001550 Message userMsg = mHandler.obtainMessage(MESSAGE_USER_SWITCHED);
1551 userMsg.arg2 = 1 + msg.arg2;
1552 // if user is switched when service is being binding
1553 // delay sending MESSAGE_USER_SWITCHED
1554 mHandler.sendMessageDelayed(userMsg, USER_SWITCHED_TIME_MS);
1555 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001556 Slog.d(TAG, "delay MESSAGE_USER_SWITCHED " + userMsg.arg2);
Zhihai Xu40874a02012-10-08 17:57:03 -07001557 }
John Spurlock8a985d22014-02-25 09:40:05 -05001558 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001559 break;
1560 }
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -07001561 case MESSAGE_USER_UNLOCKED: {
1562 if (DBG) Slog.d(TAG, "MESSAGE_USER_UNLOCKED");
1563 mHandler.removeMessages(MESSAGE_USER_SWITCHED);
1564
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001565 if (mEnable && !mBinding && (mBluetooth == null)) {
1566 // We should be connected, but we gave up for some
1567 // reason; maybe the Bluetooth service wasn't encryption
1568 // aware, so try binding again.
1569 if (DBG) Slog.d(TAG, "Enabled but not bound; retrying after unlock");
1570 handleEnable(mQuietEnable);
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -07001571 }
1572 }
fredc0f420372012-04-12 00:02:00 -07001573 }
1574 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001575 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001576
Zhihai Xu401202b2012-12-03 11:36:21 -08001577 private void handleEnable(boolean quietMode) {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001578 mQuietEnable = quietMode;
1579
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001580 try {
1581 mBluetoothLock.writeLock().lock();
Zhihai Xu40874a02012-10-08 17:57:03 -07001582 if ((mBluetooth == null) && (!mBinding)) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001583 //Start bind timeout and bind
1584 Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
1585 mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS);
Matthew Xiecdce0b92012-07-12 19:06:15 -07001586 Intent i = new Intent(IBluetooth.class.getName());
Dianne Hackbornce09f5a2014-10-10 15:03:13 -07001587 if (!doBind(i, mConnection,Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT,
1588 UserHandle.CURRENT)) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001589 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
Zhihai Xu40874a02012-10-08 17:57:03 -07001590 } else {
1591 mBinding = true;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001592 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001593 } else if (mBluetooth != null) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001594 //Enable bluetooth
1595 try {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001596 if (!mQuietEnable) {
1597 if(!mBluetooth.enable()) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001598 Slog.e(TAG,"IBluetooth.enable() returned false");
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001599 }
1600 }
1601 else {
1602 if(!mBluetooth.enableNoAutoConnect()) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001603 Slog.e(TAG,"IBluetooth.enableNoAutoConnect() returned false");
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001604 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001605 }
1606 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001607 Slog.e(TAG,"Unable to call enable()",e);
Matthew Xiecdce0b92012-07-12 19:06:15 -07001608 }
1609 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001610 } finally {
1611 mBluetoothLock.writeLock().unlock();
Matthew Xiecdce0b92012-07-12 19:06:15 -07001612 }
1613 }
1614
Dianne Hackborn221ea892013-08-04 16:50:16 -07001615 boolean doBind(Intent intent, ServiceConnection conn, int flags, UserHandle user) {
1616 ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
1617 intent.setComponent(comp);
1618 if (comp == null || !mContext.bindServiceAsUser(intent, conn, flags, user)) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001619 Slog.e(TAG, "Fail to bind to: " + intent);
Dianne Hackborn221ea892013-08-04 16:50:16 -07001620 return false;
1621 }
1622 return true;
1623 }
1624
Zhihai Xu401202b2012-12-03 11:36:21 -08001625 private void handleDisable() {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001626 try {
1627 mBluetoothLock.readLock().lock();
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001628 if (mBluetooth != null) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001629 if (DBG) Slog.d(TAG,"Sending off request.");
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001630 if (!mBluetooth.disable()) {
1631 Slog.e(TAG,"IBluetooth.disable() returned false");
Matthew Xiecdce0b92012-07-12 19:06:15 -07001632 }
1633 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001634 } catch (RemoteException e) {
1635 Slog.e(TAG,"Unable to call disable()",e);
1636 } finally {
1637 mBluetoothLock.readLock().unlock();
Matthew Xiecdce0b92012-07-12 19:06:15 -07001638 }
1639 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001640
1641 private boolean checkIfCallerIsForegroundUser() {
1642 int foregroundUser;
1643 int callingUser = UserHandle.getCallingUserId();
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001644 int callingUid = Binder.getCallingUid();
Zhihai Xu40874a02012-10-08 17:57:03 -07001645 long callingIdentity = Binder.clearCallingIdentity();
Benjamin Franze8b98922014-11-12 15:57:54 +00001646 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
1647 UserInfo ui = um.getProfileParent(callingUser);
1648 int parentUser = (ui != null) ? ui.id : UserHandle.USER_NULL;
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001649 int callingAppId = UserHandle.getAppId(callingUid);
Zhihai Xu40874a02012-10-08 17:57:03 -07001650 boolean valid = false;
1651 try {
1652 foregroundUser = ActivityManager.getCurrentUser();
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001653 valid = (callingUser == foregroundUser) ||
Benjamin Franze8b98922014-11-12 15:57:54 +00001654 parentUser == foregroundUser ||
Adrian Roosbd9a9a52014-08-18 15:31:57 +02001655 callingAppId == Process.NFC_UID ||
1656 callingAppId == mSystemUiUid;
Zhihai Xu40874a02012-10-08 17:57:03 -07001657 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001658 Slog.d(TAG, "checkIfCallerIsForegroundUser: valid=" + valid
Zhihai Xu40874a02012-10-08 17:57:03 -07001659 + " callingUser=" + callingUser
Benjamin Franze8b98922014-11-12 15:57:54 +00001660 + " parentUser=" + parentUser
Zhihai Xu40874a02012-10-08 17:57:03 -07001661 + " foregroundUser=" + foregroundUser);
1662 }
1663 } finally {
1664 Binder.restoreCallingIdentity(callingIdentity);
1665 }
1666 return valid;
1667 }
1668
Nitin Arorad055adb2015-03-02 15:03:51 -08001669 private void sendBleStateChanged(int prevState, int newState) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001670 if (DBG) Slog.d(TAG,"BLE State Change Intent: " + prevState + " -> " + newState);
Nitin Arorad055adb2015-03-02 15:03:51 -08001671 // Send broadcast message to everyone else
1672 Intent intent = new Intent(BluetoothAdapter.ACTION_BLE_STATE_CHANGED);
1673 intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState);
1674 intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState);
1675 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1676 mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM);
1677 }
1678
Zhihai Xu40874a02012-10-08 17:57:03 -07001679 private void bluetoothStateChangeHandler(int prevState, int newState) {
Nitin Arorad055adb2015-03-02 15:03:51 -08001680 boolean isStandardBroadcast = true;
Zhihai Xu40874a02012-10-08 17:57:03 -07001681 if (prevState != newState) {
1682 //Notify all proxy objects first of adapter state change
Calvin Ona0b91d72016-06-15 17:58:23 -07001683 if (newState == BluetoothAdapter.STATE_BLE_ON ||
1684 newState == BluetoothAdapter.STATE_OFF) {
Nitin Arorad055adb2015-03-02 15:03:51 -08001685 boolean intermediate_off = (prevState == BluetoothAdapter.STATE_TURNING_OFF
1686 && newState == BluetoothAdapter.STATE_BLE_ON);
Zhihai Xu40874a02012-10-08 17:57:03 -07001687
Nitin Arorad055adb2015-03-02 15:03:51 -08001688 if (newState == BluetoothAdapter.STATE_OFF) {
1689 // If Bluetooth is off, send service down event to proxy objects, and unbind
Jeff Sharkey67609c72016-03-05 14:29:13 -07001690 if (DBG) Slog.d(TAG, "Bluetooth is complete turn off");
Pavlin Radoslavove47ec142016-06-01 22:25:18 -07001691 sendBluetoothServiceDownCallback();
1692 unbindAndFinish();
1693 sendBleStateChanged(prevState, newState);
1694 // Don't broadcast as it has already been broadcast before
1695 isStandardBroadcast = false;
Nitin Arorad055adb2015-03-02 15:03:51 -08001696
1697 } else if (!intermediate_off) {
1698 // connect to GattService
Jeff Sharkey67609c72016-03-05 14:29:13 -07001699 if (DBG) Slog.d(TAG, "Bluetooth is in LE only mode");
Nitin Arorad055adb2015-03-02 15:03:51 -08001700 if (mBluetoothGatt != null) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001701 if (DBG) Slog.d(TAG, "Calling BluetoothGattServiceUp");
Nitin Arorad055adb2015-03-02 15:03:51 -08001702 onBluetoothGattServiceUp();
1703 } else {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001704 if (DBG) Slog.d(TAG, "Binding Bluetooth GATT service");
Nitin Arorad055adb2015-03-02 15:03:51 -08001705 if (mContext.getPackageManager().hasSystemFeature(
1706 PackageManager.FEATURE_BLUETOOTH_LE)) {
1707 Intent i = new Intent(IBluetoothGatt.class.getName());
1708 doBind(i, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, UserHandle.CURRENT);
1709 }
1710 }
1711 sendBleStateChanged(prevState, newState);
1712 //Don't broadcase this as std intent
1713 isStandardBroadcast = false;
1714
1715 } else if (intermediate_off){
Jeff Sharkey67609c72016-03-05 14:29:13 -07001716 if (DBG) Slog.d(TAG, "Intermediate off, back to LE only mode");
Nitin Arorad055adb2015-03-02 15:03:51 -08001717 // For LE only mode, broadcast as is
1718 sendBleStateChanged(prevState, newState);
1719 sendBluetoothStateCallback(false); // BT is OFF for general users
1720 // Broadcast as STATE_OFF
1721 newState = BluetoothAdapter.STATE_OFF;
1722 sendBrEdrDownCallback();
Zhihai Xu40874a02012-10-08 17:57:03 -07001723 }
Nitin Arorad055adb2015-03-02 15:03:51 -08001724 } else if (newState == BluetoothAdapter.STATE_ON) {
1725 boolean isUp = (newState==BluetoothAdapter.STATE_ON);
1726 sendBluetoothStateCallback(isUp);
1727 sendBleStateChanged(prevState, newState);
1728
Calvin Ona0b91d72016-06-15 17:58:23 -07001729 } else if (newState == BluetoothAdapter.STATE_BLE_TURNING_ON ||
1730 newState == BluetoothAdapter.STATE_BLE_TURNING_OFF ) {
Nitin Arorad055adb2015-03-02 15:03:51 -08001731 sendBleStateChanged(prevState, newState);
1732 isStandardBroadcast = false;
1733
Calvin Ona0b91d72016-06-15 17:58:23 -07001734 } else if (newState == BluetoothAdapter.STATE_TURNING_ON ||
1735 newState == BluetoothAdapter.STATE_TURNING_OFF) {
Nitin Arorad055adb2015-03-02 15:03:51 -08001736 sendBleStateChanged(prevState, newState);
Zhihai Xu40874a02012-10-08 17:57:03 -07001737 }
1738
Nitin Arorad055adb2015-03-02 15:03:51 -08001739 if (isStandardBroadcast) {
1740 if (prevState == BluetoothAdapter.STATE_BLE_ON) {
1741 // Show prevState of BLE_ON as OFF to standard users
1742 prevState = BluetoothAdapter.STATE_OFF;
1743 }
1744 Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
1745 intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState);
1746 intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState);
1747 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1748 mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM);
1749 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001750 }
1751 }
1752
1753 /**
1754 * if on is true, wait for state become ON
1755 * if off is true, wait for state become OFF
1756 * if both on and off are false, wait for state not ON
1757 */
1758 private boolean waitForOnOff(boolean on, boolean off) {
1759 int i = 0;
1760 while (i < 10) {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001761 try {
1762 mBluetoothLock.readLock().lock();
1763 if (mBluetooth == null) break;
1764 if (on) {
1765 if (mBluetooth.getState() == BluetoothAdapter.STATE_ON) return true;
1766 } else if (off) {
1767 if (mBluetooth.getState() == BluetoothAdapter.STATE_OFF) return true;
1768 } else {
1769 if (mBluetooth.getState() != BluetoothAdapter.STATE_ON) return true;
Zhihai Xu40874a02012-10-08 17:57:03 -07001770 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001771 } catch (RemoteException e) {
1772 Slog.e(TAG, "getState()", e);
1773 break;
1774 } finally {
1775 mBluetoothLock.readLock().unlock();
Zhihai Xu40874a02012-10-08 17:57:03 -07001776 }
1777 if (on || off) {
1778 SystemClock.sleep(300);
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001779 } else {
Zhihai Xu40874a02012-10-08 17:57:03 -07001780 SystemClock.sleep(50);
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001781 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001782 i++;
1783 }
Jeff Sharkey67609c72016-03-05 14:29:13 -07001784 Slog.e(TAG,"waitForOnOff time out");
Zhihai Xu40874a02012-10-08 17:57:03 -07001785 return false;
1786 }
Zhihai Xu681ae7f2012-11-12 15:14:18 -08001787
Zhihai Xu401202b2012-12-03 11:36:21 -08001788 private void sendDisableMsg() {
1789 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISABLE));
1790 }
1791
1792 private void sendEnableMsg(boolean quietMode) {
1793 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_ENABLE,
1794 quietMode ? 1 : 0, 0));
1795 }
1796
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001797 private void recoverBluetoothServiceFromError() {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001798 Slog.e(TAG,"recoverBluetoothServiceFromError");
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001799 try {
1800 mBluetoothLock.readLock().lock();
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001801 if (mBluetooth != null) {
1802 //Unregister callback object
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001803 mBluetooth.unregisterCallback(mBluetoothCallback);
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001804 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001805 } catch (RemoteException re) {
1806 Slog.e(TAG, "Unable to unregister", re);
1807 } finally {
1808 mBluetoothLock.readLock().unlock();
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001809 }
1810
1811 SystemClock.sleep(500);
1812
1813 // disable
1814 handleDisable();
1815
1816 waitForOnOff(false, true);
1817
1818 sendBluetoothServiceDownCallback();
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001819
Pavlin Radoslavove957a8a2016-05-24 15:28:41 -07001820 try {
1821 mBluetoothLock.writeLock().lock();
1822 if (mBluetooth != null) {
1823 mBluetooth = null;
1824 // Unbind
1825 mContext.unbindService(mConnection);
1826 }
1827 mBluetoothGatt = null;
1828 } finally {
1829 mBluetoothLock.writeLock().unlock();
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001830 }
1831
1832 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
1833 mState = BluetoothAdapter.STATE_OFF;
1834
1835 mEnable = false;
1836
1837 if (mErrorRecoveryRetryCounter++ < MAX_ERROR_RESTART_RETRIES) {
1838 // Send a Bluetooth Restart message to reenable bluetooth
1839 Message restartMsg = mHandler.obtainMessage(
1840 MESSAGE_RESTART_BLUETOOTH_SERVICE);
1841 mHandler.sendMessageDelayed(restartMsg, ERROR_RESTART_TIME_MS);
1842 } else {
1843 // todo: notify user to power down and power up phone to make bluetooth work.
1844 }
1845 }
Mike Lockwood726d4de2014-10-28 14:06:28 -07001846
1847 @Override
Pavlin Radoslavov6e8faff2016-02-23 11:54:37 -08001848 public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
1849 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
1850 String errorMsg = null;
1851 if (mBluetoothBinder == null) {
1852 errorMsg = "Bluetooth Service not connected";
1853 } else {
1854 try {
1855 mBluetoothBinder.dump(fd, args);
1856 } catch (RemoteException re) {
1857 errorMsg = "RemoteException while calling Bluetooth Service";
1858 }
Mike Lockwood726d4de2014-10-28 14:06:28 -07001859 }
Pavlin Radoslavov6e8faff2016-02-23 11:54:37 -08001860 if (errorMsg != null) {
1861 // Silently return if we are extracting metrics in Protobuf format
1862 if ((args.length > 0) && args[0].startsWith("--proto"))
1863 return;
1864 writer.println(errorMsg);
1865 }
Mike Lockwood726d4de2014-10-28 14:06:28 -07001866 }
fredc0f420372012-04-12 00:02:00 -07001867}