blob: 89fdfaf30559db640afe714879146fd6cd041da3 [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);
Zhihai Xu40874a02012-10-08 17:57:03 -0700710 if (mBluetooth != null) {
Andre Eisenbach305fdab2015-11-11 21:43:26 -0800711 //Unregister callback object
712 try {
713 mBluetooth.unregisterCallback(mBluetoothCallback);
714 } catch (RemoteException re) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700715 Slog.e(TAG, "Unable to unregister BluetoothCallback",re);
fredcbf072a72012-05-09 16:52:50 -0700716 }
Andre Eisenbach305fdab2015-11-11 21:43:26 -0800717
Jeff Sharkey67609c72016-03-05 14:29:13 -0700718 if (DBG) Slog.d(TAG, "Sending unbind request.");
Marie Janssen9db28eb2016-01-12 16:05:15 -0800719 mBluetoothBinder = null;
fredcd6883532012-04-25 17:46:13 -0700720 mBluetooth = null;
721 //Unbind
fredc0f420372012-04-12 00:02:00 -0700722 mContext.unbindService(mConnection);
fredcd6883532012-04-25 17:46:13 -0700723 mUnbinding = false;
Zhihai Xu40874a02012-10-08 17:57:03 -0700724 mBinding = false;
fredcf2458862012-04-16 15:18:27 -0700725 } else {
726 mUnbinding=false;
fredc0f420372012-04-12 00:02:00 -0700727 }
Nitin Arorad055adb2015-03-02 15:03:51 -0800728 mBluetoothGatt = null;
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700729 } finally {
730 mBluetoothLock.writeLock().unlock();
fredc0f420372012-04-12 00:02:00 -0700731 }
732 }
733
Matthew Xieddf7e472013-03-01 18:41:02 -0800734 public IBluetoothGatt getBluetoothGatt() {
735 // sync protection
736 return mBluetoothGatt;
737 }
738
Benjamin Franze8b98922014-11-12 15:57:54 +0000739 @Override
740 public boolean bindBluetoothProfileService(int bluetoothProfile,
741 IBluetoothProfileServiceConnection proxy) {
742 if (!mEnable) {
743 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700744 Slog.d(TAG, "Trying to bind to profile: " + bluetoothProfile +
Benjamin Franze8b98922014-11-12 15:57:54 +0000745 ", while Bluetooth was disabled");
746 }
747 return false;
748 }
749 synchronized (mProfileServices) {
750 ProfileServiceConnections psc = mProfileServices.get(new Integer(bluetoothProfile));
751 if (psc == null) {
752 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700753 Slog.d(TAG, "Creating new ProfileServiceConnections object for"
Benjamin Franze8b98922014-11-12 15:57:54 +0000754 + " profile: " + bluetoothProfile);
755 }
Benjamin Franz5b614592014-12-09 18:58:45 +0000756
757 if (bluetoothProfile != BluetoothProfile.HEADSET) return false;
758
759 Intent intent = new Intent(IBluetoothHeadset.class.getName());
Benjamin Franze8b98922014-11-12 15:57:54 +0000760 psc = new ProfileServiceConnections(intent);
Benjamin Franz5b614592014-12-09 18:58:45 +0000761 if (!psc.bindService()) return false;
762
Benjamin Franze8b98922014-11-12 15:57:54 +0000763 mProfileServices.put(new Integer(bluetoothProfile), psc);
Benjamin Franze8b98922014-11-12 15:57:54 +0000764 }
765 }
766
767 // Introducing a delay to give the client app time to prepare
768 Message addProxyMsg = mHandler.obtainMessage(MESSAGE_ADD_PROXY_DELAYED);
769 addProxyMsg.arg1 = bluetoothProfile;
770 addProxyMsg.obj = proxy;
771 mHandler.sendMessageDelayed(addProxyMsg, ADD_PROXY_DELAY_MS);
772 return true;
773 }
774
775 @Override
776 public void unbindBluetoothProfileService(int bluetoothProfile,
777 IBluetoothProfileServiceConnection proxy) {
778 synchronized (mProfileServices) {
779 ProfileServiceConnections psc = mProfileServices.get(new Integer(bluetoothProfile));
780 if (psc == null) {
781 return;
782 }
783 psc.removeProxy(proxy);
784 }
785 }
786
787 private void unbindAllBluetoothProfileServices() {
788 synchronized (mProfileServices) {
789 for (Integer i : mProfileServices.keySet()) {
790 ProfileServiceConnections psc = mProfileServices.get(i);
Benjamin Franz5b614592014-12-09 18:58:45 +0000791 try {
792 mContext.unbindService(psc);
793 } catch (IllegalArgumentException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700794 Slog.e(TAG, "Unable to unbind service with intent: " + psc.mIntent, e);
Benjamin Franz5b614592014-12-09 18:58:45 +0000795 }
Benjamin Franze8b98922014-11-12 15:57:54 +0000796 psc.removeAllProxies();
797 }
798 mProfileServices.clear();
799 }
800 }
801
802 /**
Miao Chou658bf2f2015-06-26 17:14:35 -0700803 * Send enable message and set adapter name and address. Called when the boot phase becomes
804 * PHASE_SYSTEM_SERVICES_READY.
805 */
806 public void handleOnBootPhase() {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700807 if (DBG) Slog.d(TAG, "Bluetooth boot completed");
Miao Chou658bf2f2015-06-26 17:14:35 -0700808 if (mEnableExternal && isBluetoothPersistedStateOnBluetooth()) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700809 if (DBG) Slog.d(TAG, "Auto-enabling Bluetooth.");
Miao Chou658bf2f2015-06-26 17:14:35 -0700810 sendEnableMsg(mQuietEnableExternal);
Ajay Panickerbf796d82016-03-11 13:47:20 -0800811 } else if (!isNameAndAddressSet()) {
812 if (DBG) Slog.d(TAG, "Getting adapter name and address");
Ajay Panicker4bb48302016-03-31 14:14:27 -0700813 Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
814 mHandler.sendMessage(getMsg);
Miao Chou658bf2f2015-06-26 17:14:35 -0700815 }
Miao Chou658bf2f2015-06-26 17:14:35 -0700816 }
817
818 /**
819 * Called when switching to a different foreground user.
820 */
821 public void handleOnSwitchUser(int userHandle) {
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -0700822 if (DBG) Slog.d(TAG, "User " + userHandle + " switched");
823 mHandler.obtainMessage(MESSAGE_USER_SWITCHED, userHandle, 0).sendToTarget();
824 }
825
826 /**
827 * Called when user is unlocked.
828 */
829 public void handleOnUnlockUser(int userHandle) {
830 if (DBG) Slog.d(TAG, "User " + userHandle + " unlocked");
831 mHandler.obtainMessage(MESSAGE_USER_UNLOCKED, userHandle, 0).sendToTarget();
Miao Chou658bf2f2015-06-26 17:14:35 -0700832 }
833
834 /**
Benjamin Franze8b98922014-11-12 15:57:54 +0000835 * This class manages the clients connected to a given ProfileService
836 * and maintains the connection with that service.
837 */
838 final private class ProfileServiceConnections implements ServiceConnection,
839 IBinder.DeathRecipient {
840 final RemoteCallbackList<IBluetoothProfileServiceConnection> mProxies =
841 new RemoteCallbackList <IBluetoothProfileServiceConnection>();
842 IBinder mService;
843 ComponentName mClassName;
844 Intent mIntent;
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700845 boolean mInvokingProxyCallbacks = false;
Benjamin Franze8b98922014-11-12 15:57:54 +0000846
847 ProfileServiceConnections(Intent intent) {
848 mService = null;
849 mClassName = null;
850 mIntent = intent;
851 }
852
Benjamin Franz5b614592014-12-09 18:58:45 +0000853 private boolean bindService() {
854 if (mIntent != null && mService == null &&
855 doBind(mIntent, this, 0, UserHandle.CURRENT_OR_SELF)) {
Benjamin Franze8b98922014-11-12 15:57:54 +0000856 Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE);
857 msg.obj = this;
858 mHandler.sendMessageDelayed(msg, TIMEOUT_BIND_MS);
Benjamin Franz5b614592014-12-09 18:58:45 +0000859 return true;
Benjamin Franze8b98922014-11-12 15:57:54 +0000860 }
Jeff Sharkey67609c72016-03-05 14:29:13 -0700861 Slog.w(TAG, "Unable to bind with intent: " + mIntent);
Benjamin Franz5b614592014-12-09 18:58:45 +0000862 return false;
Benjamin Franze8b98922014-11-12 15:57:54 +0000863 }
864
865 private void addProxy(IBluetoothProfileServiceConnection proxy) {
866 mProxies.register(proxy);
867 if (mService != null) {
868 try{
869 proxy.onServiceConnected(mClassName, mService);
870 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700871 Slog.e(TAG, "Unable to connect to proxy", e);
Benjamin Franze8b98922014-11-12 15:57:54 +0000872 }
873 } else {
874 if (!mHandler.hasMessages(MESSAGE_BIND_PROFILE_SERVICE, this)) {
875 Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE);
876 msg.obj = this;
877 mHandler.sendMessage(msg);
878 }
879 }
880 }
881
882 private void removeProxy(IBluetoothProfileServiceConnection proxy) {
883 if (proxy != null) {
884 if (mProxies.unregister(proxy)) {
885 try {
886 proxy.onServiceDisconnected(mClassName);
887 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700888 Slog.e(TAG, "Unable to disconnect proxy", e);
Benjamin Franze8b98922014-11-12 15:57:54 +0000889 }
890 }
891 } else {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700892 Slog.w(TAG, "Trying to remove a null proxy");
Benjamin Franze8b98922014-11-12 15:57:54 +0000893 }
894 }
895
896 private void removeAllProxies() {
897 onServiceDisconnected(mClassName);
898 mProxies.kill();
899 }
900
901 @Override
902 public void onServiceConnected(ComponentName className, IBinder service) {
903 // remove timeout message
904 mHandler.removeMessages(MESSAGE_BIND_PROFILE_SERVICE, this);
905 mService = service;
906 mClassName = className;
907 try {
908 mService.linkToDeath(this, 0);
909 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700910 Slog.e(TAG, "Unable to linkToDeath", e);
Benjamin Franze8b98922014-11-12 15:57:54 +0000911 }
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700912
913 if (mInvokingProxyCallbacks) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700914 Slog.e(TAG, "Proxy callbacks already in progress.");
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700915 return;
Benjamin Franze8b98922014-11-12 15:57:54 +0000916 }
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700917 mInvokingProxyCallbacks = true;
918
919 final int n = mProxies.beginBroadcast();
920 try {
921 for (int i = 0; i < n; i++) {
922 try {
923 mProxies.getBroadcastItem(i).onServiceConnected(className, service);
924 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700925 Slog.e(TAG, "Unable to connect to proxy", e);
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700926 }
927 }
928 } finally {
929 mProxies.finishBroadcast();
930 mInvokingProxyCallbacks = false;
931 }
Benjamin Franze8b98922014-11-12 15:57:54 +0000932 }
933
934 @Override
935 public void onServiceDisconnected(ComponentName className) {
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700936 if (mService == null) return;
Benjamin Franze8b98922014-11-12 15:57:54 +0000937 mService.unlinkToDeath(this, 0);
938 mService = null;
939 mClassName = null;
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700940
941 if (mInvokingProxyCallbacks) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700942 Slog.e(TAG, "Proxy callbacks already in progress.");
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700943 return;
Benjamin Franze8b98922014-11-12 15:57:54 +0000944 }
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700945 mInvokingProxyCallbacks = true;
946
947 final int n = mProxies.beginBroadcast();
948 try {
949 for (int i = 0; i < n; i++) {
950 try {
951 mProxies.getBroadcastItem(i).onServiceDisconnected(className);
952 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700953 Slog.e(TAG, "Unable to disconnect from proxy", e);
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700954 }
955 }
956 } finally {
957 mProxies.finishBroadcast();
958 mInvokingProxyCallbacks = false;
959 }
Benjamin Franze8b98922014-11-12 15:57:54 +0000960 }
961
962 @Override
963 public void binderDied() {
964 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700965 Slog.w(TAG, "Profile service for profile: " + mClassName
Benjamin Franze8b98922014-11-12 15:57:54 +0000966 + " died.");
967 }
968 onServiceDisconnected(mClassName);
969 // Trigger rebind
970 Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE);
971 msg.obj = this;
972 mHandler.sendMessageDelayed(msg, TIMEOUT_BIND_MS);
973 }
974 }
975
fredcbf072a72012-05-09 16:52:50 -0700976 private void sendBluetoothStateCallback(boolean isUp) {
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700977 try {
978 int n = mStateChangeCallbacks.beginBroadcast();
Jeff Sharkey67609c72016-03-05 14:29:13 -0700979 if (DBG) Slog.d(TAG,"Broadcasting onBluetoothStateChange("+isUp+") to " + n + " receivers.");
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700980 for (int i=0; i <n;i++) {
981 try {
982 mStateChangeCallbacks.getBroadcastItem(i).onBluetoothStateChange(isUp);
983 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700984 Slog.e(TAG, "Unable to call onBluetoothStateChange() on callback #" + i , e);
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700985 }
fredcbf072a72012-05-09 16:52:50 -0700986 }
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700987 } finally {
988 mStateChangeCallbacks.finishBroadcast();
fredcbf072a72012-05-09 16:52:50 -0700989 }
fredcbf072a72012-05-09 16:52:50 -0700990 }
991
992 /**
Zhihai Xu40874a02012-10-08 17:57:03 -0700993 * Inform BluetoothAdapter instances that Adapter service is up
994 */
995 private void sendBluetoothServiceUpCallback() {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700996 if (DBG) Slog.d(TAG,"Calling onBluetoothServiceUp callbacks");
Andre Eisenbach305fdab2015-11-11 21:43:26 -0800997 try {
998 int n = mCallbacks.beginBroadcast();
Jeff Sharkey67609c72016-03-05 14:29:13 -0700999 Slog.d(TAG,"Broadcasting onBluetoothServiceUp() to " + n + " receivers.");
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001000 for (int i=0; i <n;i++) {
1001 try {
1002 mCallbacks.getBroadcastItem(i).onBluetoothServiceUp(mBluetooth);
1003 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001004 Slog.e(TAG, "Unable to call onBluetoothServiceUp() on callback #" + i, e);
Zhihai Xu40874a02012-10-08 17:57:03 -07001005 }
1006 }
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001007 } finally {
1008 mCallbacks.finishBroadcast();
Zhihai Xu40874a02012-10-08 17:57:03 -07001009 }
1010 }
1011 /**
fredcbf072a72012-05-09 16:52:50 -07001012 * Inform BluetoothAdapter instances that Adapter service is down
1013 */
1014 private void sendBluetoothServiceDownCallback() {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001015 if (DBG) Slog.d(TAG,"Calling onBluetoothServiceDown callbacks");
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001016 try {
1017 int n = mCallbacks.beginBroadcast();
Jeff Sharkey67609c72016-03-05 14:29:13 -07001018 Slog.d(TAG,"Broadcasting onBluetoothServiceDown() to " + n + " receivers.");
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001019 for (int i=0; i <n;i++) {
1020 try {
1021 mCallbacks.getBroadcastItem(i).onBluetoothServiceDown();
1022 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001023 Slog.e(TAG, "Unable to call onBluetoothServiceDown() on callback #" + i, e);
fredcd6883532012-04-25 17:46:13 -07001024 }
1025 }
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001026 } finally {
1027 mCallbacks.finishBroadcast();
fredcd6883532012-04-25 17:46:13 -07001028 }
1029 }
Svet Ganov408abf72015-05-12 19:13:36 -07001030
fredc0f420372012-04-12 00:02:00 -07001031 public String getAddress() {
Matthew Xieaf5ddbf2012-12-04 10:47:43 -08001032 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
Svet Ganov408abf72015-05-12 19:13:36 -07001033 "Need BLUETOOTH permission");
Zhihai Xu40874a02012-10-08 17:57:03 -07001034
Zhihai Xu6eb76522012-11-29 15:41:04 -08001035 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
Svet Ganov408abf72015-05-12 19:13:36 -07001036 (!checkIfCallerIsForegroundUser())) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001037 Slog.w(TAG,"getAddress(): not allowed for non-active and non system user");
Zhihai Xu6eb76522012-11-29 15:41:04 -08001038 return null;
Zhihai Xu40874a02012-10-08 17:57:03 -07001039 }
1040
Svet Ganov408abf72015-05-12 19:13:36 -07001041 if (mContext.checkCallingOrSelfPermission(Manifest.permission.LOCAL_MAC_ADDRESS)
1042 != PackageManager.PERMISSION_GRANTED) {
1043 return BluetoothAdapter.DEFAULT_MAC_ADDRESS;
1044 }
1045
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001046 try {
1047 mBluetoothLock.readLock().lock();
1048 if (mBluetooth != null) return mBluetooth.getAddress();
1049 } catch (RemoteException e) {
1050 Slog.e(TAG, "getAddress(): Unable to retrieve address remotely. Returning cached address", e);
1051 } finally {
1052 mBluetoothLock.readLock().unlock();
fredc116d1d462012-04-20 14:47:08 -07001053 }
Ajay Panickerbf796d82016-03-11 13:47:20 -08001054
Matthew Xiecdce0b92012-07-12 19:06:15 -07001055 // mAddress is accessed from outside.
1056 // It is alright without a lock. Here, bluetooth is off, no other thread is
1057 // changing mAddress
fredc0f420372012-04-12 00:02:00 -07001058 return mAddress;
1059 }
fredc649fe492012-04-19 01:07:18 -07001060
fredc0f420372012-04-12 00:02:00 -07001061 public String getName() {
Matthew Xieaf5ddbf2012-12-04 10:47:43 -08001062 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
1063 "Need BLUETOOTH permission");
Zhihai Xu40874a02012-10-08 17:57:03 -07001064
Zhihai Xu6eb76522012-11-29 15:41:04 -08001065 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
1066 (!checkIfCallerIsForegroundUser())) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001067 Slog.w(TAG,"getName(): not allowed for non-active and non system user");
Zhihai Xu6eb76522012-11-29 15:41:04 -08001068 return null;
Zhihai Xu40874a02012-10-08 17:57:03 -07001069 }
1070
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001071 try {
1072 mBluetoothLock.readLock().lock();
1073 if (mBluetooth != null) return mBluetooth.getName();
1074 } catch (RemoteException e) {
1075 Slog.e(TAG, "getName(): Unable to retrieve name remotely. Returning cached name", e);
1076 } finally {
1077 mBluetoothLock.readLock().unlock();
fredc116d1d462012-04-20 14:47:08 -07001078 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001079
Matthew Xiecdce0b92012-07-12 19:06:15 -07001080 // mName is accessed from outside.
1081 // It alright without a lock. Here, bluetooth is off, no other thread is
1082 // changing mName
fredc0f420372012-04-12 00:02:00 -07001083 return mName;
1084 }
1085
fredc0f420372012-04-12 00:02:00 -07001086 private class BluetoothServiceConnection implements ServiceConnection {
fredc0f420372012-04-12 00:02:00 -07001087 public void onServiceConnected(ComponentName className, IBinder service) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001088 if (DBG) Slog.d(TAG, "BluetoothServiceConnection: " + className.getClassName());
fredc0f420372012-04-12 00:02:00 -07001089 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED);
Matthew Xieddf7e472013-03-01 18:41:02 -08001090 // TBD if (className.getClassName().equals(IBluetooth.class.getName())) {
1091 if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) {
1092 msg.arg1 = SERVICE_IBLUETOOTH;
1093 // } else if (className.getClassName().equals(IBluetoothGatt.class.getName())) {
1094 } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) {
1095 msg.arg1 = SERVICE_IBLUETOOTHGATT;
1096 } else {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001097 Slog.e(TAG, "Unknown service connected: " + className.getClassName());
Matthew Xieddf7e472013-03-01 18:41:02 -08001098 return;
1099 }
fredc0f420372012-04-12 00:02:00 -07001100 msg.obj = service;
1101 mHandler.sendMessage(msg);
1102 }
1103
1104 public void onServiceDisconnected(ComponentName className) {
fredc0f420372012-04-12 00:02:00 -07001105 // Called if we unexpected disconnected.
Jeff Sharkey67609c72016-03-05 14:29:13 -07001106 if (DBG) Slog.d(TAG, "BluetoothServiceConnection, disconnected: " +
Matthew Xieddf7e472013-03-01 18:41:02 -08001107 className.getClassName());
fredc0f420372012-04-12 00:02:00 -07001108 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED);
Matthew Xieddf7e472013-03-01 18:41:02 -08001109 if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) {
1110 msg.arg1 = SERVICE_IBLUETOOTH;
1111 } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) {
1112 msg.arg1 = SERVICE_IBLUETOOTHGATT;
1113 } else {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001114 Slog.e(TAG, "Unknown service disconnected: " + className.getClassName());
Matthew Xieddf7e472013-03-01 18:41:02 -08001115 return;
1116 }
fredc0f420372012-04-12 00:02:00 -07001117 mHandler.sendMessage(msg);
1118 }
1119 }
1120
1121 private BluetoothServiceConnection mConnection = new BluetoothServiceConnection();
1122
Zhihai Xu40874a02012-10-08 17:57:03 -07001123 private class BluetoothHandler extends Handler {
Ajay Panicker4bb48302016-03-31 14:14:27 -07001124 boolean mGetNameAddressOnly = false;
1125
Zhihai Xu40874a02012-10-08 17:57:03 -07001126 public BluetoothHandler(Looper looper) {
1127 super(looper);
1128 }
1129
fredc0f420372012-04-12 00:02:00 -07001130 @Override
1131 public void handleMessage(Message msg) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001132 if (DBG) Slog.d (TAG, "Message: " + msg.what);
fredc0f420372012-04-12 00:02:00 -07001133 switch (msg.what) {
Ajay Panicker4bb48302016-03-31 14:14:27 -07001134 case MESSAGE_GET_NAME_AND_ADDRESS:
1135 if (DBG) Slog.d(TAG, "MESSAGE_GET_NAME_AND_ADDRESS");
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001136 try {
1137 mBluetoothLock.writeLock().lock();
Ajay Panicker4bb48302016-03-31 14:14:27 -07001138 if ((mBluetooth == null) && (!mBinding)) {
1139 if (DBG) Slog.d(TAG, "Binding to service to get name and address");
1140 mGetNameAddressOnly = true;
1141 Message timeoutMsg = mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
1142 mHandler.sendMessageDelayed(timeoutMsg, TIMEOUT_BIND_MS);
1143 Intent i = new Intent(IBluetooth.class.getName());
1144 if (!doBind(i, mConnection,
1145 Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT,
1146 UserHandle.CURRENT)) {
1147 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
1148 } else {
1149 mBinding = true;
1150 }
1151 } else if (mBluetooth != null) {
1152 try {
1153 storeNameAndAddress(mBluetooth.getName(),
1154 mBluetooth.getAddress());
1155 } catch (RemoteException re) {
1156 Slog.e(TAG, "Unable to grab names", re);
1157 }
1158 if (mGetNameAddressOnly && !mEnable) {
1159 unbindAndFinish();
1160 }
1161 mGetNameAddressOnly = false;
1162 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001163 } finally {
1164 mBluetoothLock.writeLock().unlock();
Ajay Panicker4bb48302016-03-31 14:14:27 -07001165 }
1166 break;
1167
Matthew Xiecdce0b92012-07-12 19:06:15 -07001168 case MESSAGE_ENABLE:
fredcf2458862012-04-16 15:18:27 -07001169 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001170 Slog.d(TAG, "MESSAGE_ENABLE: mBluetooth = " + mBluetooth);
fredc649fe492012-04-19 01:07:18 -07001171 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001172 mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
1173 mEnable = true;
Calvin Ona0b91d72016-06-15 17:58:23 -07001174
1175 // Use service interface to get the exact state
1176 try {
1177 mBluetoothLock.readLock().lock();
1178 if (mBluetooth != null) {
1179 int state = mBluetooth.getState();
1180 if (state == BluetoothAdapter.STATE_BLE_ON) {
1181 Slog.w(TAG, "BT is in BLE_ON State");
1182 mBluetooth.onLeServiceUp();
1183 break;
1184 }
1185 }
1186 } catch (RemoteException e) {
1187 Slog.e(TAG, "", e);
1188 } finally {
1189 mBluetoothLock.readLock().unlock();
1190 }
1191
1192 mQuietEnable = (msg.arg1 == 1);
Pavlin Radoslavove47ec142016-06-01 22:25:18 -07001193 if (mBluetooth == null) {
Calvin Ona0b91d72016-06-15 17:58:23 -07001194 handleEnable(mQuietEnable);
Pavlin Radoslavove47ec142016-06-01 22:25:18 -07001195 } else {
1196 //
1197 // We need to wait until transitioned to STATE_OFF and
1198 // the previous Bluetooth process has exited. The
1199 // waiting period has three components:
1200 // (a) Wait until the local state is STATE_OFF. This
1201 // is accomplished by "waitForOnOff(false, true)".
1202 // (b) Wait until the STATE_OFF state is updated to
1203 // all components.
1204 // (c) Wait until the Bluetooth process exits, and
1205 // ActivityManager detects it.
1206 // The waiting for (b) and (c) is accomplished by
1207 // delaying the MESSAGE_RESTART_BLUETOOTH_SERVICE
1208 // message. On slower devices, that delay needs to be
1209 // on the order of (2 * SERVICE_RESTART_TIME_MS).
1210 //
1211 waitForOnOff(false, true);
Pavlin Radoslavove47ec142016-06-01 22:25:18 -07001212 Message restartMsg = mHandler.obtainMessage(
1213 MESSAGE_RESTART_BLUETOOTH_SERVICE);
1214 mHandler.sendMessageDelayed(restartMsg,
1215 2 * SERVICE_RESTART_TIME_MS);
1216 }
fredc649fe492012-04-19 01:07:18 -07001217 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001218
fredc0f420372012-04-12 00:02:00 -07001219 case MESSAGE_DISABLE:
Zhihai Xu40874a02012-10-08 17:57:03 -07001220 mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
1221 if (mEnable && mBluetooth != null) {
1222 waitForOnOff(true, false);
1223 mEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -08001224 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -07001225 waitForOnOff(false, false);
1226 } else {
1227 mEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -08001228 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -07001229 }
fredc0f420372012-04-12 00:02:00 -07001230 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001231
fredc0f420372012-04-12 00:02:00 -07001232 case MESSAGE_REGISTER_ADAPTER:
1233 {
1234 IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -07001235 boolean added = mCallbacks.register(callback);
Jeff Sharkey67609c72016-03-05 14:29:13 -07001236 Slog.d(TAG,"Added callback: " + (callback == null? "null": callback) +":" +added );
fredc0f420372012-04-12 00:02:00 -07001237 }
1238 break;
1239 case MESSAGE_UNREGISTER_ADAPTER:
1240 {
1241 IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -07001242 boolean removed = mCallbacks.unregister(callback);
Jeff Sharkey67609c72016-03-05 14:29:13 -07001243 Slog.d(TAG,"Removed callback: " + (callback == null? "null": callback) +":" + removed);
fredc0f420372012-04-12 00:02:00 -07001244 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001245 }
fredc0f420372012-04-12 00:02:00 -07001246 case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK:
1247 {
1248 IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj;
Matthew Xie9b693992013-10-10 11:21:40 -07001249 if (callback != null) {
1250 mStateChangeCallbacks.register(callback);
1251 }
fredc0f420372012-04-12 00:02:00 -07001252 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001253 }
fredc0f420372012-04-12 00:02:00 -07001254 case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK:
1255 {
1256 IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj;
Matthew Xie9b693992013-10-10 11:21:40 -07001257 if (callback != null) {
1258 mStateChangeCallbacks.unregister(callback);
1259 }
fredc0f420372012-04-12 00:02:00 -07001260 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001261 }
Benjamin Franze8b98922014-11-12 15:57:54 +00001262 case MESSAGE_ADD_PROXY_DELAYED:
1263 {
1264 ProfileServiceConnections psc = mProfileServices.get(
1265 new Integer(msg.arg1));
1266 if (psc == null) {
1267 break;
1268 }
1269 IBluetoothProfileServiceConnection proxy =
1270 (IBluetoothProfileServiceConnection) msg.obj;
1271 psc.addProxy(proxy);
1272 break;
1273 }
1274 case MESSAGE_BIND_PROFILE_SERVICE:
1275 {
1276 ProfileServiceConnections psc = (ProfileServiceConnections) msg.obj;
1277 removeMessages(MESSAGE_BIND_PROFILE_SERVICE, msg.obj);
1278 if (psc == null) {
1279 break;
1280 }
1281 psc.bindService();
1282 break;
1283 }
fredc0f420372012-04-12 00:02:00 -07001284 case MESSAGE_BLUETOOTH_SERVICE_CONNECTED:
1285 {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001286 if (DBG) Slog.d(TAG,"MESSAGE_BLUETOOTH_SERVICE_CONNECTED: " + msg.arg1);
fredc0f420372012-04-12 00:02:00 -07001287
1288 IBinder service = (IBinder) msg.obj;
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001289 try {
1290 mBluetoothLock.writeLock().lock();
Matthew Xieddf7e472013-03-01 18:41:02 -08001291 if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
1292 mBluetoothGatt = IBluetoothGatt.Stub.asInterface(service);
Nitin Arorad055adb2015-03-02 15:03:51 -08001293 onBluetoothGattServiceUp();
Matthew Xieddf7e472013-03-01 18:41:02 -08001294 break;
1295 } // else must be SERVICE_IBLUETOOTH
1296
1297 //Remove timeout
Zhihai Xuaf5971e2013-06-10 20:28:31 -07001298 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
Matthew Xieddf7e472013-03-01 18:41:02 -08001299
fredc0f420372012-04-12 00:02:00 -07001300 mBinding = false;
Marie Janssen9db28eb2016-01-12 16:05:15 -08001301 mBluetoothBinder = service;
fredc0f420372012-04-12 00:02:00 -07001302 mBluetooth = IBluetooth.Stub.asInterface(service);
fredc0f420372012-04-12 00:02:00 -07001303
Ajay Panicker4bb48302016-03-31 14:14:27 -07001304 if (!isNameAndAddressSet()) {
1305 Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
1306 mHandler.sendMessage(getMsg);
1307 if (mGetNameAddressOnly) return;
1308 }
1309
Zhihai Xuaf5971e2013-06-10 20:28:31 -07001310 try {
1311 boolean enableHciSnoopLog = (Settings.Secure.getInt(mContentResolver,
1312 Settings.Secure.BLUETOOTH_HCI_LOG, 0) == 1);
1313 if (!mBluetooth.configHciSnoopLog(enableHciSnoopLog)) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001314 Slog.e(TAG,"IBluetooth.configHciSnoopLog return false");
Zhihai Xuaf5971e2013-06-10 20:28:31 -07001315 }
1316 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001317 Slog.e(TAG,"Unable to call configHciSnoopLog", e);
Zhihai Xuaf5971e2013-06-10 20:28:31 -07001318 }
1319
Matthew Xiecdce0b92012-07-12 19:06:15 -07001320 //Register callback object
fredcbf072a72012-05-09 16:52:50 -07001321 try {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001322 mBluetooth.registerCallback(mBluetoothCallback);
1323 } catch (RemoteException re) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001324 Slog.e(TAG, "Unable to register BluetoothCallback",re);
fredcbf072a72012-05-09 16:52:50 -07001325 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001326 //Inform BluetoothAdapter instances that service is up
Zhihai Xu40874a02012-10-08 17:57:03 -07001327 sendBluetoothServiceUpCallback();
1328
Matthew Xiecdce0b92012-07-12 19:06:15 -07001329 //Do enable request
1330 try {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001331 if (mQuietEnable == false) {
1332 if(!mBluetooth.enable()) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001333 Slog.e(TAG,"IBluetooth.enable() returned false");
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001334 }
1335 }
1336 else
1337 {
1338 if(!mBluetooth.enableNoAutoConnect()) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001339 Slog.e(TAG,"IBluetooth.enableNoAutoConnect() returned false");
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001340 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001341 }
1342 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001343 Slog.e(TAG,"Unable to call enable()",e);
Matthew Xiecdce0b92012-07-12 19:06:15 -07001344 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001345 } finally {
1346 mBluetoothLock.writeLock().unlock();
Freda8c6df02012-07-11 10:25:23 -07001347 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001348
1349 if (!mEnable) {
1350 waitForOnOff(true, false);
Zhihai Xu401202b2012-12-03 11:36:21 -08001351 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -07001352 waitForOnOff(false, false);
1353 }
fredc649fe492012-04-19 01:07:18 -07001354 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001355 }
fredc649fe492012-04-19 01:07:18 -07001356 case MESSAGE_TIMEOUT_BIND: {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001357 Slog.e(TAG, "MESSAGE_TIMEOUT_BIND");
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001358 mBluetoothLock.writeLock().lock();
1359 mBinding = false;
1360 mBluetoothLock.writeLock().unlock();
1361
fredc649fe492012-04-19 01:07:18 -07001362 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001363 }
fredcbf072a72012-05-09 16:52:50 -07001364 case MESSAGE_BLUETOOTH_STATE_CHANGE:
fredc0f420372012-04-12 00:02:00 -07001365 {
fredcbf072a72012-05-09 16:52:50 -07001366 int prevState = msg.arg1;
1367 int newState = msg.arg2;
Jeff Sharkey67609c72016-03-05 14:29:13 -07001368 if (DBG) Slog.d(TAG, "MESSAGE_BLUETOOTH_STATE_CHANGE: prevState = " + prevState + ", newState=" + newState);
Zhihai Xu40874a02012-10-08 17:57:03 -07001369 mState = newState;
1370 bluetoothStateChangeHandler(prevState, newState);
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001371 // handle error state transition case from TURNING_ON to OFF
1372 // unbind and rebind bluetooth service and enable bluetooth
Nitin Arorad055adb2015-03-02 15:03:51 -08001373 if ((prevState == BluetoothAdapter.STATE_BLE_TURNING_ON) &&
Calvin Ona0b91d72016-06-15 17:58:23 -07001374 (newState == BluetoothAdapter.STATE_OFF) &&
1375 (mBluetooth != null) && mEnable) {
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001376 recoverBluetoothServiceFromError();
1377 }
Nitin Arorad055adb2015-03-02 15:03:51 -08001378 if ((prevState == BluetoothAdapter.STATE_TURNING_ON) &&
Calvin Ona0b91d72016-06-15 17:58:23 -07001379 (newState == BluetoothAdapter.STATE_BLE_ON) &&
1380 (mBluetooth != null) && mEnable) {
Nitin Arorad055adb2015-03-02 15:03:51 -08001381 recoverBluetoothServiceFromError();
1382 }
Calvin Ona0b91d72016-06-15 17:58:23 -07001383 // If we tried to enable BT while BT was in the process of shutting down,
1384 // wait for the BT process to fully tear down and then force a restart
1385 // here. This is a bit of a hack (b/29363429).
1386 if ((prevState == BluetoothAdapter.STATE_BLE_TURNING_OFF) &&
1387 (newState == BluetoothAdapter.STATE_OFF)) {
1388 if (mEnable) {
1389 Slog.d(TAG, "Entering STATE_OFF but mEnabled is true; restarting.");
1390 waitForOnOff(false, true);
1391 Message restartMsg = mHandler.obtainMessage(
1392 MESSAGE_RESTART_BLUETOOTH_SERVICE);
1393 mHandler.sendMessageDelayed(restartMsg, 2 * SERVICE_RESTART_TIME_MS);
1394 }
1395 }
Nitin Arorad055adb2015-03-02 15:03:51 -08001396 if (newState == BluetoothAdapter.STATE_ON ||
Calvin Ona0b91d72016-06-15 17:58:23 -07001397 newState == BluetoothAdapter.STATE_BLE_ON) {
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001398 // bluetooth is working, reset the counter
1399 if (mErrorRecoveryRetryCounter != 0) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001400 Slog.w(TAG, "bluetooth is recovered from error");
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001401 mErrorRecoveryRetryCounter = 0;
1402 }
1403 }
fredc649fe492012-04-19 01:07:18 -07001404 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001405 }
fredc0f420372012-04-12 00:02:00 -07001406 case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED:
1407 {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001408 Slog.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: " + msg.arg1);
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001409 try {
1410 mBluetoothLock.writeLock().lock();
Matthew Xieddf7e472013-03-01 18:41:02 -08001411 if (msg.arg1 == SERVICE_IBLUETOOTH) {
1412 // if service is unbinded already, do nothing and return
1413 if (mBluetooth == null) break;
1414 mBluetooth = null;
1415 } else if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
1416 mBluetoothGatt = null;
1417 break;
1418 } else {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001419 Slog.e(TAG, "Bad msg.arg1: " + msg.arg1);
Matthew Xieddf7e472013-03-01 18:41:02 -08001420 break;
1421 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001422 } finally {
1423 mBluetoothLock.writeLock().unlock();
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301424 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001425
1426 if (mEnable) {
1427 mEnable = false;
1428 // Send a Bluetooth Restart message
1429 Message restartMsg = mHandler.obtainMessage(
1430 MESSAGE_RESTART_BLUETOOTH_SERVICE);
1431 mHandler.sendMessageDelayed(restartMsg,
1432 SERVICE_RESTART_TIME_MS);
1433 }
1434
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001435 sendBluetoothServiceDownCallback();
Zhihai Xu40874a02012-10-08 17:57:03 -07001436
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001437 // Send BT state broadcast to update
1438 // the BT icon correctly
1439 if ((mState == BluetoothAdapter.STATE_TURNING_ON) ||
Calvin Ona0b91d72016-06-15 17:58:23 -07001440 (mState == BluetoothAdapter.STATE_ON)) {
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001441 bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,
1442 BluetoothAdapter.STATE_TURNING_OFF);
1443 mState = BluetoothAdapter.STATE_TURNING_OFF;
Zhihai Xu40874a02012-10-08 17:57:03 -07001444 }
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001445 if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
1446 bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
1447 BluetoothAdapter.STATE_OFF);
1448 }
1449
1450 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
1451 mState = BluetoothAdapter.STATE_OFF;
fredc649fe492012-04-19 01:07:18 -07001452 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001453 }
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301454 case MESSAGE_RESTART_BLUETOOTH_SERVICE:
1455 {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001456 Slog.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE:"
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301457 +" Restart IBluetooth service");
1458 /* Enable without persisting the setting as
1459 it doesnt change when IBluetooth
1460 service restarts */
Zhihai Xu40874a02012-10-08 17:57:03 -07001461 mEnable = true;
Zhihai Xu401202b2012-12-03 11:36:21 -08001462 handleEnable(mQuietEnable);
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301463 break;
1464 }
1465
fredc0f420372012-04-12 00:02:00 -07001466 case MESSAGE_TIMEOUT_UNBIND:
1467 {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001468 Slog.e(TAG, "MESSAGE_TIMEOUT_UNBIND");
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001469 mBluetoothLock.writeLock().lock();
1470 mUnbinding = false;
1471 mBluetoothLock.writeLock().unlock();
fredc649fe492012-04-19 01:07:18 -07001472 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001473 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001474
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -07001475 case MESSAGE_USER_SWITCHED: {
1476 if (DBG) Slog.d(TAG, "MESSAGE_USER_SWITCHED");
Zhihai Xu40874a02012-10-08 17:57:03 -07001477 mHandler.removeMessages(MESSAGE_USER_SWITCHED);
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -07001478
Zhihai Xu40874a02012-10-08 17:57:03 -07001479 /* disable and enable BT when detect a user switch */
1480 if (mEnable && mBluetooth != null) {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001481 try {
1482 mBluetoothLock.readLock().lock();
Zhihai Xu40874a02012-10-08 17:57:03 -07001483 if (mBluetooth != null) {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001484 mBluetooth.unregisterCallback(mBluetoothCallback);
Zhihai Xu40874a02012-10-08 17:57:03 -07001485 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001486 } catch (RemoteException re) {
1487 Slog.e(TAG, "Unable to unregister", re);
1488 } finally {
1489 mBluetoothLock.readLock().unlock();
Zhihai Xu40874a02012-10-08 17:57:03 -07001490 }
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001491
1492 if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
1493 // MESSAGE_USER_SWITCHED happened right after MESSAGE_ENABLE
1494 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_OFF);
1495 mState = BluetoothAdapter.STATE_OFF;
1496 }
1497 if (mState == BluetoothAdapter.STATE_OFF) {
1498 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_TURNING_ON);
1499 mState = BluetoothAdapter.STATE_TURNING_ON;
1500 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001501
1502 waitForOnOff(true, false);
1503
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001504 if (mState == BluetoothAdapter.STATE_TURNING_ON) {
1505 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_ON);
1506 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001507
Benjamin Franze8b98922014-11-12 15:57:54 +00001508 unbindAllBluetoothProfileServices();
Zhihai Xu40874a02012-10-08 17:57:03 -07001509 // disable
Zhihai Xu401202b2012-12-03 11:36:21 -08001510 handleDisable();
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001511 // Pbap service need receive STATE_TURNING_OFF intent to close
1512 bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,
1513 BluetoothAdapter.STATE_TURNING_OFF);
Zhihai Xu40874a02012-10-08 17:57:03 -07001514
Pavlin Radoslavov41401112016-06-27 15:25:18 -07001515 boolean didDisableTimeout = !waitForOnOff(false, true);
Zhihai Xu40874a02012-10-08 17:57:03 -07001516
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001517 bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
Zhihai Xu40874a02012-10-08 17:57:03 -07001518 BluetoothAdapter.STATE_OFF);
Zhihai Xu40874a02012-10-08 17:57:03 -07001519 sendBluetoothServiceDownCallback();
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001520
Pavlin Radoslavove957a8a2016-05-24 15:28:41 -07001521 try {
1522 mBluetoothLock.writeLock().lock();
1523 if (mBluetooth != null) {
1524 mBluetooth = null;
1525 // Unbind
1526 mContext.unbindService(mConnection);
1527 }
1528 mBluetoothGatt = null;
1529 } finally {
1530 mBluetoothLock.writeLock().unlock();
Zhihai Xu40874a02012-10-08 17:57:03 -07001531 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001532
Pavlin Radoslavov41401112016-06-27 15:25:18 -07001533 //
1534 // If disabling Bluetooth times out, wait for an
1535 // additional amount of time to ensure the process is
1536 // shut down completely before attempting to restart.
1537 //
1538 if (didDisableTimeout) {
1539 SystemClock.sleep(3000);
1540 } else {
1541 SystemClock.sleep(100);
1542 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001543
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001544 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
1545 mState = BluetoothAdapter.STATE_OFF;
Zhihai Xu40874a02012-10-08 17:57:03 -07001546 // enable
Zhihai Xu401202b2012-12-03 11:36:21 -08001547 handleEnable(mQuietEnable);
John Spurlock8a985d22014-02-25 09:40:05 -05001548 } else if (mBinding || mBluetooth != null) {
Zhihai Xu40874a02012-10-08 17:57:03 -07001549 Message userMsg = mHandler.obtainMessage(MESSAGE_USER_SWITCHED);
1550 userMsg.arg2 = 1 + msg.arg2;
1551 // if user is switched when service is being binding
1552 // delay sending MESSAGE_USER_SWITCHED
1553 mHandler.sendMessageDelayed(userMsg, USER_SWITCHED_TIME_MS);
1554 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001555 Slog.d(TAG, "delay MESSAGE_USER_SWITCHED " + userMsg.arg2);
Zhihai Xu40874a02012-10-08 17:57:03 -07001556 }
John Spurlock8a985d22014-02-25 09:40:05 -05001557 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001558 break;
1559 }
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -07001560 case MESSAGE_USER_UNLOCKED: {
1561 if (DBG) Slog.d(TAG, "MESSAGE_USER_UNLOCKED");
1562 mHandler.removeMessages(MESSAGE_USER_SWITCHED);
1563
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001564 if (mEnable && !mBinding && (mBluetooth == null)) {
1565 // We should be connected, but we gave up for some
1566 // reason; maybe the Bluetooth service wasn't encryption
1567 // aware, so try binding again.
1568 if (DBG) Slog.d(TAG, "Enabled but not bound; retrying after unlock");
1569 handleEnable(mQuietEnable);
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -07001570 }
1571 }
fredc0f420372012-04-12 00:02:00 -07001572 }
1573 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001574 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001575
Zhihai Xu401202b2012-12-03 11:36:21 -08001576 private void handleEnable(boolean quietMode) {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001577 mQuietEnable = quietMode;
1578
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001579 try {
1580 mBluetoothLock.writeLock().lock();
Zhihai Xu40874a02012-10-08 17:57:03 -07001581 if ((mBluetooth == null) && (!mBinding)) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001582 //Start bind timeout and bind
1583 Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
1584 mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS);
Matthew Xiecdce0b92012-07-12 19:06:15 -07001585 Intent i = new Intent(IBluetooth.class.getName());
Dianne Hackbornce09f5a2014-10-10 15:03:13 -07001586 if (!doBind(i, mConnection,Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT,
1587 UserHandle.CURRENT)) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001588 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
Zhihai Xu40874a02012-10-08 17:57:03 -07001589 } else {
1590 mBinding = true;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001591 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001592 } else if (mBluetooth != null) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001593 //Enable bluetooth
1594 try {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001595 if (!mQuietEnable) {
1596 if(!mBluetooth.enable()) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001597 Slog.e(TAG,"IBluetooth.enable() returned false");
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001598 }
1599 }
1600 else {
1601 if(!mBluetooth.enableNoAutoConnect()) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001602 Slog.e(TAG,"IBluetooth.enableNoAutoConnect() returned false");
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001603 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001604 }
1605 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001606 Slog.e(TAG,"Unable to call enable()",e);
Matthew Xiecdce0b92012-07-12 19:06:15 -07001607 }
1608 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001609 } finally {
1610 mBluetoothLock.writeLock().unlock();
Matthew Xiecdce0b92012-07-12 19:06:15 -07001611 }
1612 }
1613
Dianne Hackborn221ea892013-08-04 16:50:16 -07001614 boolean doBind(Intent intent, ServiceConnection conn, int flags, UserHandle user) {
1615 ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
1616 intent.setComponent(comp);
1617 if (comp == null || !mContext.bindServiceAsUser(intent, conn, flags, user)) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001618 Slog.e(TAG, "Fail to bind to: " + intent);
Dianne Hackborn221ea892013-08-04 16:50:16 -07001619 return false;
1620 }
1621 return true;
1622 }
1623
Zhihai Xu401202b2012-12-03 11:36:21 -08001624 private void handleDisable() {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001625 try {
1626 mBluetoothLock.readLock().lock();
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001627 if (mBluetooth != null) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001628 if (DBG) Slog.d(TAG,"Sending off request.");
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001629 if (!mBluetooth.disable()) {
1630 Slog.e(TAG,"IBluetooth.disable() returned false");
Matthew Xiecdce0b92012-07-12 19:06:15 -07001631 }
1632 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001633 } catch (RemoteException e) {
1634 Slog.e(TAG,"Unable to call disable()",e);
1635 } finally {
1636 mBluetoothLock.readLock().unlock();
Matthew Xiecdce0b92012-07-12 19:06:15 -07001637 }
1638 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001639
1640 private boolean checkIfCallerIsForegroundUser() {
1641 int foregroundUser;
1642 int callingUser = UserHandle.getCallingUserId();
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001643 int callingUid = Binder.getCallingUid();
Zhihai Xu40874a02012-10-08 17:57:03 -07001644 long callingIdentity = Binder.clearCallingIdentity();
Benjamin Franze8b98922014-11-12 15:57:54 +00001645 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
1646 UserInfo ui = um.getProfileParent(callingUser);
1647 int parentUser = (ui != null) ? ui.id : UserHandle.USER_NULL;
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001648 int callingAppId = UserHandle.getAppId(callingUid);
Zhihai Xu40874a02012-10-08 17:57:03 -07001649 boolean valid = false;
1650 try {
1651 foregroundUser = ActivityManager.getCurrentUser();
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001652 valid = (callingUser == foregroundUser) ||
Benjamin Franze8b98922014-11-12 15:57:54 +00001653 parentUser == foregroundUser ||
Adrian Roosbd9a9a52014-08-18 15:31:57 +02001654 callingAppId == Process.NFC_UID ||
1655 callingAppId == mSystemUiUid;
Zhihai Xu40874a02012-10-08 17:57:03 -07001656 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001657 Slog.d(TAG, "checkIfCallerIsForegroundUser: valid=" + valid
Zhihai Xu40874a02012-10-08 17:57:03 -07001658 + " callingUser=" + callingUser
Benjamin Franze8b98922014-11-12 15:57:54 +00001659 + " parentUser=" + parentUser
Zhihai Xu40874a02012-10-08 17:57:03 -07001660 + " foregroundUser=" + foregroundUser);
1661 }
1662 } finally {
1663 Binder.restoreCallingIdentity(callingIdentity);
1664 }
1665 return valid;
1666 }
1667
Nitin Arorad055adb2015-03-02 15:03:51 -08001668 private void sendBleStateChanged(int prevState, int newState) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001669 if (DBG) Slog.d(TAG,"BLE State Change Intent: " + prevState + " -> " + newState);
Nitin Arorad055adb2015-03-02 15:03:51 -08001670 // Send broadcast message to everyone else
1671 Intent intent = new Intent(BluetoothAdapter.ACTION_BLE_STATE_CHANGED);
1672 intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState);
1673 intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState);
1674 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1675 mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM);
1676 }
1677
Zhihai Xu40874a02012-10-08 17:57:03 -07001678 private void bluetoothStateChangeHandler(int prevState, int newState) {
Nitin Arorad055adb2015-03-02 15:03:51 -08001679 boolean isStandardBroadcast = true;
Zhihai Xu40874a02012-10-08 17:57:03 -07001680 if (prevState != newState) {
1681 //Notify all proxy objects first of adapter state change
Calvin Ona0b91d72016-06-15 17:58:23 -07001682 if (newState == BluetoothAdapter.STATE_BLE_ON ||
1683 newState == BluetoothAdapter.STATE_OFF) {
Nitin Arorad055adb2015-03-02 15:03:51 -08001684 boolean intermediate_off = (prevState == BluetoothAdapter.STATE_TURNING_OFF
1685 && newState == BluetoothAdapter.STATE_BLE_ON);
Zhihai Xu40874a02012-10-08 17:57:03 -07001686
Nitin Arorad055adb2015-03-02 15:03:51 -08001687 if (newState == BluetoothAdapter.STATE_OFF) {
1688 // If Bluetooth is off, send service down event to proxy objects, and unbind
Jeff Sharkey67609c72016-03-05 14:29:13 -07001689 if (DBG) Slog.d(TAG, "Bluetooth is complete turn off");
Pavlin Radoslavove47ec142016-06-01 22:25:18 -07001690 sendBluetoothServiceDownCallback();
1691 unbindAndFinish();
1692 sendBleStateChanged(prevState, newState);
1693 // Don't broadcast as it has already been broadcast before
1694 isStandardBroadcast = false;
Nitin Arorad055adb2015-03-02 15:03:51 -08001695
1696 } else if (!intermediate_off) {
1697 // connect to GattService
Jeff Sharkey67609c72016-03-05 14:29:13 -07001698 if (DBG) Slog.d(TAG, "Bluetooth is in LE only mode");
Nitin Arorad055adb2015-03-02 15:03:51 -08001699 if (mBluetoothGatt != null) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001700 if (DBG) Slog.d(TAG, "Calling BluetoothGattServiceUp");
Nitin Arorad055adb2015-03-02 15:03:51 -08001701 onBluetoothGattServiceUp();
1702 } else {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001703 if (DBG) Slog.d(TAG, "Binding Bluetooth GATT service");
Nitin Arorad055adb2015-03-02 15:03:51 -08001704 if (mContext.getPackageManager().hasSystemFeature(
1705 PackageManager.FEATURE_BLUETOOTH_LE)) {
1706 Intent i = new Intent(IBluetoothGatt.class.getName());
1707 doBind(i, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, UserHandle.CURRENT);
1708 }
1709 }
1710 sendBleStateChanged(prevState, newState);
1711 //Don't broadcase this as std intent
1712 isStandardBroadcast = false;
1713
1714 } else if (intermediate_off){
Jeff Sharkey67609c72016-03-05 14:29:13 -07001715 if (DBG) Slog.d(TAG, "Intermediate off, back to LE only mode");
Nitin Arorad055adb2015-03-02 15:03:51 -08001716 // For LE only mode, broadcast as is
1717 sendBleStateChanged(prevState, newState);
1718 sendBluetoothStateCallback(false); // BT is OFF for general users
1719 // Broadcast as STATE_OFF
1720 newState = BluetoothAdapter.STATE_OFF;
1721 sendBrEdrDownCallback();
Zhihai Xu40874a02012-10-08 17:57:03 -07001722 }
Nitin Arorad055adb2015-03-02 15:03:51 -08001723 } else if (newState == BluetoothAdapter.STATE_ON) {
1724 boolean isUp = (newState==BluetoothAdapter.STATE_ON);
1725 sendBluetoothStateCallback(isUp);
1726 sendBleStateChanged(prevState, newState);
1727
Calvin Ona0b91d72016-06-15 17:58:23 -07001728 } else if (newState == BluetoothAdapter.STATE_BLE_TURNING_ON ||
1729 newState == BluetoothAdapter.STATE_BLE_TURNING_OFF ) {
Nitin Arorad055adb2015-03-02 15:03:51 -08001730 sendBleStateChanged(prevState, newState);
1731 isStandardBroadcast = false;
1732
Calvin Ona0b91d72016-06-15 17:58:23 -07001733 } else if (newState == BluetoothAdapter.STATE_TURNING_ON ||
1734 newState == BluetoothAdapter.STATE_TURNING_OFF) {
Nitin Arorad055adb2015-03-02 15:03:51 -08001735 sendBleStateChanged(prevState, newState);
Zhihai Xu40874a02012-10-08 17:57:03 -07001736 }
1737
Nitin Arorad055adb2015-03-02 15:03:51 -08001738 if (isStandardBroadcast) {
1739 if (prevState == BluetoothAdapter.STATE_BLE_ON) {
1740 // Show prevState of BLE_ON as OFF to standard users
1741 prevState = BluetoothAdapter.STATE_OFF;
1742 }
1743 Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
1744 intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState);
1745 intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState);
1746 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1747 mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM);
1748 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001749 }
1750 }
1751
1752 /**
1753 * if on is true, wait for state become ON
1754 * if off is true, wait for state become OFF
1755 * if both on and off are false, wait for state not ON
1756 */
1757 private boolean waitForOnOff(boolean on, boolean off) {
1758 int i = 0;
1759 while (i < 10) {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001760 try {
1761 mBluetoothLock.readLock().lock();
1762 if (mBluetooth == null) break;
1763 if (on) {
1764 if (mBluetooth.getState() == BluetoothAdapter.STATE_ON) return true;
1765 } else if (off) {
1766 if (mBluetooth.getState() == BluetoothAdapter.STATE_OFF) return true;
1767 } else {
1768 if (mBluetooth.getState() != BluetoothAdapter.STATE_ON) return true;
Zhihai Xu40874a02012-10-08 17:57:03 -07001769 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001770 } catch (RemoteException e) {
1771 Slog.e(TAG, "getState()", e);
1772 break;
1773 } finally {
1774 mBluetoothLock.readLock().unlock();
Zhihai Xu40874a02012-10-08 17:57:03 -07001775 }
1776 if (on || off) {
1777 SystemClock.sleep(300);
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001778 } else {
Zhihai Xu40874a02012-10-08 17:57:03 -07001779 SystemClock.sleep(50);
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001780 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001781 i++;
1782 }
Jeff Sharkey67609c72016-03-05 14:29:13 -07001783 Slog.e(TAG,"waitForOnOff time out");
Zhihai Xu40874a02012-10-08 17:57:03 -07001784 return false;
1785 }
Zhihai Xu681ae7f2012-11-12 15:14:18 -08001786
Zhihai Xu401202b2012-12-03 11:36:21 -08001787 private void sendDisableMsg() {
1788 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISABLE));
1789 }
1790
1791 private void sendEnableMsg(boolean quietMode) {
1792 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_ENABLE,
1793 quietMode ? 1 : 0, 0));
1794 }
1795
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001796 private void recoverBluetoothServiceFromError() {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001797 Slog.e(TAG,"recoverBluetoothServiceFromError");
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001798 try {
1799 mBluetoothLock.readLock().lock();
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001800 if (mBluetooth != null) {
1801 //Unregister callback object
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001802 mBluetooth.unregisterCallback(mBluetoothCallback);
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001803 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001804 } catch (RemoteException re) {
1805 Slog.e(TAG, "Unable to unregister", re);
1806 } finally {
1807 mBluetoothLock.readLock().unlock();
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001808 }
1809
1810 SystemClock.sleep(500);
1811
1812 // disable
1813 handleDisable();
1814
1815 waitForOnOff(false, true);
1816
1817 sendBluetoothServiceDownCallback();
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001818
Pavlin Radoslavove957a8a2016-05-24 15:28:41 -07001819 try {
1820 mBluetoothLock.writeLock().lock();
1821 if (mBluetooth != null) {
1822 mBluetooth = null;
1823 // Unbind
1824 mContext.unbindService(mConnection);
1825 }
1826 mBluetoothGatt = null;
1827 } finally {
1828 mBluetoothLock.writeLock().unlock();
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001829 }
1830
1831 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
1832 mState = BluetoothAdapter.STATE_OFF;
1833
1834 mEnable = false;
1835
1836 if (mErrorRecoveryRetryCounter++ < MAX_ERROR_RESTART_RETRIES) {
1837 // Send a Bluetooth Restart message to reenable bluetooth
1838 Message restartMsg = mHandler.obtainMessage(
1839 MESSAGE_RESTART_BLUETOOTH_SERVICE);
1840 mHandler.sendMessageDelayed(restartMsg, ERROR_RESTART_TIME_MS);
1841 } else {
1842 // todo: notify user to power down and power up phone to make bluetooth work.
1843 }
1844 }
Mike Lockwood726d4de2014-10-28 14:06:28 -07001845
1846 @Override
Pavlin Radoslavov6e8faff2016-02-23 11:54:37 -08001847 public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
1848 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
1849 String errorMsg = null;
1850 if (mBluetoothBinder == null) {
1851 errorMsg = "Bluetooth Service not connected";
1852 } else {
1853 try {
1854 mBluetoothBinder.dump(fd, args);
1855 } catch (RemoteException re) {
1856 errorMsg = "RemoteException while calling Bluetooth Service";
1857 }
Mike Lockwood726d4de2014-10-28 14:06:28 -07001858 }
Pavlin Radoslavov6e8faff2016-02-23 11:54:37 -08001859 if (errorMsg != null) {
1860 // Silently return if we are extracting metrics in Protobuf format
1861 if ((args.length > 0) && args[0].startsWith("--proto"))
1862 return;
1863 writer.println(errorMsg);
1864 }
Mike Lockwood726d4de2014-10-28 14:06:28 -07001865 }
fredc0f420372012-04-12 00:02:00 -07001866}