blob: aa6afdfba0753e79859da0e8be5937af389c3ef1 [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;
Marie Janssen2977c3e2016-11-09 12:01:24 -080059import java.util.concurrent.ConcurrentHashMap;
Benjamin Franze8b98922014-11-12 15:57:54 +000060import java.util.HashMap;
61import java.util.Map;
Miao Chou658bf2f2015-06-26 17:14:35 -070062
fredc0f420372012-04-12 00:02:00 -070063class BluetoothManagerService extends IBluetoothManager.Stub {
64 private static final String TAG = "BluetoothManagerService";
Pavlin Radoslavov41401112016-06-27 15:25:18 -070065 private static final boolean DBG = true;
fredc0f420372012-04-12 00:02:00 -070066
fredc0f420372012-04-12 00:02:00 -070067 private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN;
68 private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
fredc0f420372012-04-12 00:02:00 -070069 private static final String ACTION_SERVICE_STATE_CHANGED="com.android.bluetooth.btservice.action.STATE_CHANGED";
70 private static final String EXTRA_ACTION="action";
Zhihai Xud31c3222012-10-31 16:08:57 -070071 private static final String SECURE_SETTINGS_BLUETOOTH_ADDR_VALID="bluetooth_addr_valid";
fredc0f420372012-04-12 00:02:00 -070072 private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address";
73 private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name";
fredc0f420372012-04-12 00:02:00 -070074 private static final int TIMEOUT_BIND_MS = 3000; //Maximum msec to wait for a bind
75 private static final int TIMEOUT_SAVE_MS = 500; //Maximum msec to wait for a save
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +053076 //Maximum msec to wait for service restart
77 private static final int SERVICE_RESTART_TIME_MS = 200;
Zhihai Xudd9d17d2013-01-08 17:05:58 -080078 //Maximum msec to wait for restart due to error
79 private static final int ERROR_RESTART_TIME_MS = 3000;
Zhihai Xu40874a02012-10-08 17:57:03 -070080 //Maximum msec to delay MESSAGE_USER_SWITCHED
81 private static final int USER_SWITCHED_TIME_MS = 200;
Benjamin Franze8b98922014-11-12 15:57:54 +000082 // Delay for the addProxy function in msec
83 private static final int ADD_PROXY_DELAY_MS = 100;
fredc0f420372012-04-12 00:02:00 -070084
85 private static final int MESSAGE_ENABLE = 1;
86 private static final int MESSAGE_DISABLE = 2;
fredc649fe492012-04-19 01:07:18 -070087 private static final int MESSAGE_REGISTER_ADAPTER = 20;
88 private static final int MESSAGE_UNREGISTER_ADAPTER = 21;
89 private static final int MESSAGE_REGISTER_STATE_CHANGE_CALLBACK = 30;
90 private static final int MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK = 31;
91 private static final int MESSAGE_BLUETOOTH_SERVICE_CONNECTED = 40;
92 private static final int MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED = 41;
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +053093 private static final int MESSAGE_RESTART_BLUETOOTH_SERVICE = 42;
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -070094 private static final int MESSAGE_BLUETOOTH_STATE_CHANGE = 60;
95 private static final int MESSAGE_TIMEOUT_BIND = 100;
96 private static final int MESSAGE_TIMEOUT_UNBIND = 101;
Ajay Panicker4bb48302016-03-31 14:14:27 -070097 private static final int MESSAGE_GET_NAME_AND_ADDRESS = 200;
Zhihai Xu40874a02012-10-08 17:57:03 -070098 private static final int MESSAGE_USER_SWITCHED = 300;
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -070099 private static final int MESSAGE_USER_UNLOCKED = 301;
Benjamin Franze8b98922014-11-12 15:57:54 +0000100 private static final int MESSAGE_ADD_PROXY_DELAYED = 400;
101 private static final int MESSAGE_BIND_PROFILE_SERVICE = 401;
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -0700102 private static final int MAX_SAVE_RETRIES = 3;
103 private static final int MAX_ERROR_RESTART_RETRIES = 6;
Zhihai Xudd9d17d2013-01-08 17:05:58 -0800104
Zhihai Xu401202b2012-12-03 11:36:21 -0800105 // Bluetooth persisted setting is off
106 private static final int BLUETOOTH_OFF=0;
107 // Bluetooth persisted setting is on
108 // and Airplane mode won't affect Bluetooth state at start up
109 private static final int BLUETOOTH_ON_BLUETOOTH=1;
110 // Bluetooth persisted setting is on
111 // but Airplane mode will affect Bluetooth state at start up
112 // and Airplane mode will have higher priority.
113 private static final int BLUETOOTH_ON_AIRPLANE=2;
fredc0f420372012-04-12 00:02:00 -0700114
Matthew Xieddf7e472013-03-01 18:41:02 -0800115 private static final int SERVICE_IBLUETOOTH = 1;
116 private static final int SERVICE_IBLUETOOTHGATT = 2;
117
fredc0f420372012-04-12 00:02:00 -0700118 private final Context mContext;
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
Marie Janssen2977c3e2016-11-09 12:01:24 -0800210 clearBleApps();
Nitin Arorad055adb2015-03-02 15:03:51 -0800211 if (st == BluetoothAdapter.STATE_BLE_ON) {
212 //if state is BLE_ON make sure you trigger disableBLE part
213 try {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700214 mBluetoothLock.readLock().lock();
Nitin Arorad055adb2015-03-02 15:03:51 -0800215 if (mBluetooth != null) {
216 mBluetooth.onBrEdrDown();
Marie Janssen6a383a72016-10-25 10:47:51 -0700217 mEnable = false;
Nitin Arorad055adb2015-03-02 15:03:51 -0800218 mEnableExternal = false;
219 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700220 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700221 Slog.e(TAG,"Unable to call onBrEdrDown", e);
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700222 } finally {
Pavlin Radoslavov7ee53be2016-06-09 12:58:07 -0700223 mBluetoothLock.readLock().unlock();
Nitin Arorad055adb2015-03-02 15:03:51 -0800224 }
225 } else if (st == BluetoothAdapter.STATE_ON){
226 // disable without persisting the setting
Jeff Sharkey67609c72016-03-05 14:29:13 -0700227 Slog.d(TAG, "Calling disable");
Nitin Arorad055adb2015-03-02 15:03:51 -0800228 sendDisableMsg();
229 }
Zhihai Xu401202b2012-12-03 11:36:21 -0800230 } else if (mEnableExternal) {
231 // enable without persisting the setting
Jeff Sharkey67609c72016-03-05 14:29:13 -0700232 Slog.d(TAG, "Calling enable");
Zhihai Xu401202b2012-12-03 11:36:21 -0800233 sendEnableMsg(mQuietEnableExternal);
234 }
fredc649fe492012-04-19 01:07:18 -0700235 }
fredc0f420372012-04-12 00:02:00 -0700236 }
237 }
238 };
239
240 BluetoothManagerService(Context context) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700241 mHandler = new BluetoothHandler(IoThread.get().getLooper());
Zhihai Xu40874a02012-10-08 17:57:03 -0700242
fredc0f420372012-04-12 00:02:00 -0700243 mContext = context;
244 mBluetooth = null;
Marie Janssen9db28eb2016-01-12 16:05:15 -0800245 mBluetoothBinder = null;
Nitin Arorad055adb2015-03-02 15:03:51 -0800246 mBluetoothGatt = null;
fredc0f420372012-04-12 00:02:00 -0700247 mBinding = false;
248 mUnbinding = false;
Zhihai Xu40874a02012-10-08 17:57:03 -0700249 mEnable = false;
250 mState = BluetoothAdapter.STATE_OFF;
Zhihai Xu401202b2012-12-03 11:36:21 -0800251 mQuietEnableExternal = false;
252 mEnableExternal = false;
fredc0f420372012-04-12 00:02:00 -0700253 mAddress = null;
254 mName = null;
Zhihai Xudd9d17d2013-01-08 17:05:58 -0800255 mErrorRecoveryRetryCounter = 0;
fredc0f420372012-04-12 00:02:00 -0700256 mContentResolver = context.getContentResolver();
Wei Wange4a744b2015-06-11 17:50:29 -0700257 // Observe BLE scan only mode settings change.
258 registerForBleScanModeChange();
fredcd6883532012-04-25 17:46:13 -0700259 mCallbacks = new RemoteCallbackList<IBluetoothManagerCallback>();
260 mStateChangeCallbacks = new RemoteCallbackList<IBluetoothStateChangeCallback>();
Miao Chou658bf2f2015-06-26 17:14:35 -0700261 IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
Matthew Xie6fde3092012-07-11 17:10:07 -0700262 registerForAirplaneMode(filter);
Dianne Hackbornd83a0962014-05-02 16:28:33 -0700263 filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
Matthew Xie6fde3092012-07-11 17:10:07 -0700264 mContext.registerReceiver(mReceiver, filter);
fredc0f420372012-04-12 00:02:00 -0700265 loadStoredNameAndAddress();
Zhihai Xu401202b2012-12-03 11:36:21 -0800266 if (isBluetoothPersistedStateOn()) {
267 mEnableExternal = true;
fredc0f420372012-04-12 00:02:00 -0700268 }
Adrian Roosbd9a9a52014-08-18 15:31:57 +0200269
270 int sysUiUid = -1;
271 try {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700272 sysUiUid = mContext.getPackageManager().getPackageUidAsUser("com.android.systemui",
Jeff Sharkeyc5967e92016-01-07 18:50:29 -0700273 PackageManager.MATCH_SYSTEM_ONLY, UserHandle.USER_SYSTEM);
Adrian Roosbd9a9a52014-08-18 15:31:57 +0200274 } catch (PackageManager.NameNotFoundException e) {
Joe LaPennaacddf2b2015-09-04 12:52:42 -0700275 // Some platforms, such as wearables do not have a system ui.
Jeff Sharkey67609c72016-03-05 14:29:13 -0700276 Slog.w(TAG, "Unable to resolve SystemUI's UID.", e);
Adrian Roosbd9a9a52014-08-18 15:31:57 +0200277 }
278 mSystemUiUid = sysUiUid;
fredc0f420372012-04-12 00:02:00 -0700279 }
280
fredc649fe492012-04-19 01:07:18 -0700281 /**
282 * Returns true if airplane mode is currently on
283 */
284 private final boolean isAirplaneModeOn() {
Christopher Tatec09cdce2012-09-10 16:50:14 -0700285 return Settings.Global.getInt(mContext.getContentResolver(),
286 Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
fredc649fe492012-04-19 01:07:18 -0700287 }
288
289 /**
290 * Returns true if the Bluetooth saved state is "on"
291 */
292 private final boolean isBluetoothPersistedStateOn() {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700293 return Settings.Global.getInt(mContentResolver,
Andre Eisenbach8c184312016-09-06 18:03:10 -0700294 Settings.Global.BLUETOOTH_ON, BLUETOOTH_ON_BLUETOOTH) != BLUETOOTH_OFF;
Zhihai Xu401202b2012-12-03 11:36:21 -0800295 }
296
297 /**
298 * Returns true if the Bluetooth saved state is BLUETOOTH_ON_BLUETOOTH
299 */
300 private final boolean isBluetoothPersistedStateOnBluetooth() {
301 return Settings.Global.getInt(mContentResolver,
Andre Eisenbach8c184312016-09-06 18:03:10 -0700302 Settings.Global.BLUETOOTH_ON, BLUETOOTH_ON_BLUETOOTH) == BLUETOOTH_ON_BLUETOOTH;
fredc649fe492012-04-19 01:07:18 -0700303 }
304
305 /**
306 * Save the Bluetooth on/off state
307 *
308 */
Zhihai Xu401202b2012-12-03 11:36:21 -0800309 private void persistBluetoothSetting(int value) {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700310 Settings.Global.putInt(mContext.getContentResolver(),
311 Settings.Global.BLUETOOTH_ON,
Zhihai Xu401202b2012-12-03 11:36:21 -0800312 value);
fredc649fe492012-04-19 01:07:18 -0700313 }
314
315 /**
316 * Returns true if the Bluetooth Adapter's name and address is
317 * locally cached
318 * @return
319 */
fredc0f420372012-04-12 00:02:00 -0700320 private boolean isNameAndAddressSet() {
321 return mName !=null && mAddress!= null && mName.length()>0 && mAddress.length()>0;
322 }
323
fredc649fe492012-04-19 01:07:18 -0700324 /**
325 * Retrieve the Bluetooth Adapter's name and address and save it in
326 * in the local cache
327 */
fredc0f420372012-04-12 00:02:00 -0700328 private void loadStoredNameAndAddress() {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700329 if (DBG) Slog.d(TAG, "Loading stored name and address");
Zhihai Xud31c3222012-10-31 16:08:57 -0700330 if (mContext.getResources().getBoolean
331 (com.android.internal.R.bool.config_bluetooth_address_validation) &&
332 Settings.Secure.getInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 0) == 0) {
333 // if the valid flag is not set, don't load the address and name
Jeff Sharkey67609c72016-03-05 14:29:13 -0700334 if (DBG) Slog.d(TAG, "invalid bluetooth name and address stored");
Zhihai Xud31c3222012-10-31 16:08:57 -0700335 return;
336 }
fredc0f420372012-04-12 00:02:00 -0700337 mName = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME);
338 mAddress = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS);
Jeff Sharkey67609c72016-03-05 14:29:13 -0700339 if (DBG) Slog.d(TAG, "Stored bluetooth Name=" + mName + ",Address=" + mAddress);
fredc0f420372012-04-12 00:02:00 -0700340 }
341
fredc649fe492012-04-19 01:07:18 -0700342 /**
343 * Save the Bluetooth name and address in the persistent store.
344 * Only non-null values will be saved.
345 * @param name
346 * @param address
347 */
fredc0f420372012-04-12 00:02:00 -0700348 private void storeNameAndAddress(String name, String address) {
349 if (name != null) {
350 Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME, name);
fredc0f420372012-04-12 00:02:00 -0700351 mName = name;
Jeff Sharkey67609c72016-03-05 14:29:13 -0700352 if (DBG) Slog.d(TAG,"Stored Bluetooth name: " +
fredc649fe492012-04-19 01:07:18 -0700353 Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_NAME));
fredc0f420372012-04-12 00:02:00 -0700354 }
355
356 if (address != null) {
357 Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS, address);
fredc0f420372012-04-12 00:02:00 -0700358 mAddress=address;
Jeff Sharkey67609c72016-03-05 14:29:13 -0700359 if (DBG) Slog.d(TAG,"Stored Bluetoothaddress: " +
fredc649fe492012-04-19 01:07:18 -0700360 Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_ADDRESS));
fredc0f420372012-04-12 00:02:00 -0700361 }
Zhihai Xud31c3222012-10-31 16:08:57 -0700362
363 if ((name != null) && (address != null)) {
364 Settings.Secure.putInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 1);
365 }
fredc0f420372012-04-12 00:02:00 -0700366 }
367
368 public IBluetooth registerAdapter(IBluetoothManagerCallback callback){
Natalie Silvanovich55db6462014-05-01 16:12:23 -0700369 if (callback == null) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700370 Slog.w(TAG, "Callback is null in registerAdapter");
Natalie Silvanovich55db6462014-05-01 16:12:23 -0700371 return null;
372 }
fredc0f420372012-04-12 00:02:00 -0700373 Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_ADAPTER);
374 msg.obj = callback;
375 mHandler.sendMessage(msg);
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700376
377 return mBluetooth;
fredc0f420372012-04-12 00:02:00 -0700378 }
379
380 public void unregisterAdapter(IBluetoothManagerCallback callback) {
Natalie Silvanovich55db6462014-05-01 16:12:23 -0700381 if (callback == null) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700382 Slog.w(TAG, "Callback is null in unregisterAdapter");
Natalie Silvanovich55db6462014-05-01 16:12:23 -0700383 return;
384 }
fredc0f420372012-04-12 00:02:00 -0700385 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
386 "Need BLUETOOTH permission");
387 Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_ADAPTER);
388 msg.obj = callback;
389 mHandler.sendMessage(msg);
390 }
391
392 public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) {
393 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
394 "Need BLUETOOTH permission");
395 Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_STATE_CHANGE_CALLBACK);
396 msg.obj = callback;
397 mHandler.sendMessage(msg);
398 }
399
400 public void unregisterStateChangeCallback(IBluetoothStateChangeCallback callback) {
401 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
402 "Need BLUETOOTH permission");
403 Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK);
404 msg.obj = callback;
405 mHandler.sendMessage(msg);
406 }
407
408 public boolean isEnabled() {
Zhihai Xu6eb76522012-11-29 15:41:04 -0800409 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
410 (!checkIfCallerIsForegroundUser())) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700411 Slog.w(TAG,"isEnabled(): not allowed for non-active and non system user");
Zhihai Xu40874a02012-10-08 17:57:03 -0700412 return false;
413 }
414
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700415 try {
416 mBluetoothLock.readLock().lock();
417 if (mBluetooth != null) return mBluetooth.isEnabled();
418 } catch (RemoteException e) {
419 Slog.e(TAG, "isEnabled()", e);
420 } finally {
421 mBluetoothLock.readLock().unlock();
fredc0f420372012-04-12 00:02:00 -0700422 }
423 return false;
424 }
425
Christine Hallstrom995c90a2016-05-25 15:49:08 -0700426 public int getState() {
427 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
428 (!checkIfCallerIsForegroundUser())) {
429 Slog.w(TAG, "getState(): not allowed for non-active and non system user");
430 return BluetoothAdapter.STATE_OFF;
431 }
432
433 try {
434 mBluetoothLock.readLock().lock();
435 if (mBluetooth != null) return mBluetooth.getState();
436 } catch (RemoteException e) {
437 Slog.e(TAG, "getState()", e);
438 } finally {
439 mBluetoothLock.readLock().unlock();
440 }
441 return BluetoothAdapter.STATE_OFF;
442 }
443
Nitin Arorad055adb2015-03-02 15:03:51 -0800444 class ClientDeathRecipient implements IBinder.DeathRecipient {
445 public void binderDied() {
Marie Janssen6a383a72016-10-25 10:47:51 -0700446 if (DBG) Slog.d(TAG, "Binder is dead - unregister Ble App");
Marie Janssen2977c3e2016-11-09 12:01:24 -0800447 if (isBleAppPresent()) {
448 // Nothing to do, another app is here.
449 return;
450 }
451 if (DBG) Slog.d(TAG, "Disabling LE only mode after application crash");
452 try {
453 mBluetoothLock.readLock().lock();
454 if (mBluetooth != null &&
455 mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON) {
456 mEnable = false;
457 mBluetooth.onBrEdrDown();
Nitin Arorad055adb2015-03-02 15:03:51 -0800458 }
Marie Janssen2977c3e2016-11-09 12:01:24 -0800459 } catch (RemoteException e) {
460 Slog.e(TAG,"Unable to call onBrEdrDown", e);
461 } finally {
462 mBluetoothLock.readLock().unlock();
Nitin Arorad055adb2015-03-02 15:03:51 -0800463 }
464 }
465 }
466
467 /** Internal death rec list */
Marie Janssen2977c3e2016-11-09 12:01:24 -0800468 Map<IBinder, ClientDeathRecipient> mBleApps = new ConcurrentHashMap<IBinder, ClientDeathRecipient>();
Nitin Arorad055adb2015-03-02 15:03:51 -0800469
Wei Wang67d84162015-04-26 17:04:29 -0700470 @Override
471 public boolean isBleScanAlwaysAvailable() {
Marie Janssen6a383a72016-10-25 10:47:51 -0700472 if (isAirplaneModeOn() && !mEnable) {
473 return false;
474 }
Wei Wang67d84162015-04-26 17:04:29 -0700475 try {
476 return (Settings.Global.getInt(mContentResolver,
477 Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE)) != 0;
478 } catch (SettingNotFoundException e) {
479 }
480 return false;
481 }
482
Wei Wange4a744b2015-06-11 17:50:29 -0700483 // Monitor change of BLE scan only mode settings.
484 private void registerForBleScanModeChange() {
485 ContentObserver contentObserver = new ContentObserver(null) {
486 @Override
487 public void onChange(boolean selfChange) {
Marie Janssen2977c3e2016-11-09 12:01:24 -0800488 if (isBleScanAlwaysAvailable()) {
489 // Nothing to do
490 return;
491 }
492 // BLE scan is not available.
493 disableBleScanMode();
494 clearBleApps();
495 try {
496 mBluetoothLock.readLock().lock();
497 if (mBluetooth != null) mBluetooth.onBrEdrDown();
498 } catch (RemoteException e) {
499 Slog.e(TAG, "error when disabling bluetooth", e);
500 } finally {
501 mBluetoothLock.readLock().unlock();
Wei Wange4a744b2015-06-11 17:50:29 -0700502 }
503 }
504 };
505
506 mContentResolver.registerContentObserver(
507 Settings.Global.getUriFor(Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE),
508 false, contentObserver);
509 }
510
511 // Disable ble scan only mode.
512 private void disableBleScanMode() {
513 try {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700514 mBluetoothLock.writeLock().lock();
Wei Wange4a744b2015-06-11 17:50:29 -0700515 if (mBluetooth != null && (mBluetooth.getState() != BluetoothAdapter.STATE_ON)) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700516 if (DBG) Slog.d(TAG, "Reseting the mEnable flag for clean disable");
Wei Wange4a744b2015-06-11 17:50:29 -0700517 mEnable = false;
518 }
519 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700520 Slog.e(TAG, "getState()", e);
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700521 } finally {
522 mBluetoothLock.writeLock().unlock();
Wei Wange4a744b2015-06-11 17:50:29 -0700523 }
524 }
525
Nitin Arorad055adb2015-03-02 15:03:51 -0800526 public int updateBleAppCount(IBinder token, boolean enable) {
527 if (enable) {
528 ClientDeathRecipient r = mBleApps.get(token);
529 if (r == null) {
530 ClientDeathRecipient deathRec = new ClientDeathRecipient();
531 try {
532 token.linkToDeath(deathRec, 0);
533 } catch (RemoteException ex) {
534 throw new IllegalArgumentException("Wake lock is already dead.");
535 }
536 mBleApps.put(token, deathRec);
Jeff Sharkey67609c72016-03-05 14:29:13 -0700537 if (DBG) Slog.d(TAG, "Registered for death Notification");
Nitin Arorad055adb2015-03-02 15:03:51 -0800538 }
539
540 } else {
541 ClientDeathRecipient r = mBleApps.get(token);
542 if (r != null) {
Wei Wange4a744b2015-06-11 17:50:29 -0700543 // Unregister death recipient as the app goes away.
544 token.unlinkToDeath(r, 0);
Nitin Arorad055adb2015-03-02 15:03:51 -0800545 mBleApps.remove(token);
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 }
Marie Janssen2977c3e2016-11-09 12:01:24 -0800549 int appCount = mBleApps.size();
550 if (DBG) Slog.d(TAG, appCount + " registered Ble Apps");
551 if (appCount == 0 && mEnable) {
Wei Wange4a744b2015-06-11 17:50:29 -0700552 disableBleScanMode();
Nitin Arorad055adb2015-03-02 15:03:51 -0800553 }
Marie Janssen2977c3e2016-11-09 12:01:24 -0800554 return appCount;
Nitin Arorad055adb2015-03-02 15:03:51 -0800555 }
556
Wei Wange4a744b2015-06-11 17:50:29 -0700557 // Clear all apps using BLE scan only mode.
558 private void clearBleApps() {
Marie Janssen2977c3e2016-11-09 12:01:24 -0800559 mBleApps.clear();
Wei Wange4a744b2015-06-11 17:50:29 -0700560 }
561
Nitin Arorad055adb2015-03-02 15:03:51 -0800562 /** @hide*/
563 public boolean isBleAppPresent() {
Marie Janssen2977c3e2016-11-09 12:01:24 -0800564 if (DBG) Slog.d(TAG, "isBleAppPresent() count: " + mBleApps.size());
565 return mBleApps.size() > 0;
Nitin Arorad055adb2015-03-02 15:03:51 -0800566 }
567
568 /**
569 * Action taken when GattService is turned off
570 */
571 private void onBluetoothGattServiceUp() {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700572 if (DBG) Slog.d(TAG,"BluetoothGatt Service is Up");
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700573 try {
574 mBluetoothLock.readLock().lock();
Nitin Arorabdfaa7f2015-04-29 12:35:03 -0700575 if (isBleAppPresent() == false && mBluetooth != null
576 && mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON) {
Nitin Arorad055adb2015-03-02 15:03:51 -0800577 mBluetooth.onLeServiceUp();
578
579 // waive WRITE_SECURE_SETTINGS permission check
580 long callingIdentity = Binder.clearCallingIdentity();
581 persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);
582 Binder.restoreCallingIdentity(callingIdentity);
583 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700584 } catch (RemoteException e) {
585 Slog.e(TAG,"Unable to call onServiceUp", e);
586 } finally {
587 mBluetoothLock.readLock().unlock();
Nitin Arorad055adb2015-03-02 15:03:51 -0800588 }
589 }
590
591 /**
592 * Inform BluetoothAdapter instances that BREDR part is down
593 * and turn off all service and stack if no LE app needs it
594 */
595 private void sendBrEdrDownCallback() {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700596 if (DBG) Slog.d(TAG,"Calling sendBrEdrDownCallback callbacks");
Nitin Arorabdfaa7f2015-04-29 12:35:03 -0700597
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700598 if (mBluetooth == null) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700599 Slog.w(TAG, "Bluetooth handle is null");
Nitin Arorabdfaa7f2015-04-29 12:35:03 -0700600 return;
601 }
Nitin Arorad055adb2015-03-02 15:03:51 -0800602
603 if (isBleAppPresent() == false) {
604 try {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700605 mBluetoothLock.readLock().lock();
606 if (mBluetooth != null) mBluetooth.onBrEdrDown();
607 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700608 Slog.e(TAG, "Call to onBrEdrDown() failed.", e);
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700609 } finally {
610 mBluetoothLock.readLock().unlock();
Nitin Arorad055adb2015-03-02 15:03:51 -0800611 }
Nitin Arorabdfaa7f2015-04-29 12:35:03 -0700612 } else {
613 // Need to stay at BLE ON. Disconnect all Gatt connections
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700614 try {
Nitin Arorabdfaa7f2015-04-29 12:35:03 -0700615 mBluetoothGatt.unregAll();
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700616 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700617 Slog.e(TAG, "Unable to disconnect all apps.", e);
Nitin Arorad055adb2015-03-02 15:03:51 -0800618 }
619 }
Nitin Arorad055adb2015-03-02 15:03:51 -0800620 }
621
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700622 public boolean enableNoAutoConnect()
623 {
624 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
625 "Need BLUETOOTH ADMIN permission");
Zhihai Xu40874a02012-10-08 17:57:03 -0700626
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700627 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700628 Slog.d(TAG,"enableNoAutoConnect(): mBluetooth =" + mBluetooth +
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700629 " mBinding = " + mBinding);
630 }
Martijn Coenen8385c5a2012-11-29 10:14:16 -0800631 int callingAppId = UserHandle.getAppId(Binder.getCallingUid());
632
633 if (callingAppId != Process.NFC_UID) {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700634 throw new SecurityException("no permission to enable Bluetooth quietly");
635 }
Martijn Coenen8385c5a2012-11-29 10:14:16 -0800636
Zhihai Xu401202b2012-12-03 11:36:21 -0800637 synchronized(mReceiver) {
638 mQuietEnableExternal = true;
639 mEnableExternal = true;
640 sendEnableMsg(true);
641 }
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700642 return true;
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700643 }
Ajay Panicker4bb48302016-03-31 14:14:27 -0700644
fredc0f420372012-04-12 00:02:00 -0700645 public boolean enable() {
Zhihai Xu6eb76522012-11-29 15:41:04 -0800646 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
647 (!checkIfCallerIsForegroundUser())) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700648 Slog.w(TAG,"enable(): not allowed for non-active and non system user");
Zhihai Xu40874a02012-10-08 17:57:03 -0700649 return false;
fredcf2458862012-04-16 15:18:27 -0700650 }
651
Zhihai Xu401202b2012-12-03 11:36:21 -0800652 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
653 "Need BLUETOOTH ADMIN permission");
654 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700655 Slog.d(TAG,"enable(): mBluetooth =" + mBluetooth +
Sanket Agarwal090bf552016-04-21 14:10:55 -0700656 " mBinding = " + mBinding + " mState = " + mState);
657 }
Zhihai Xu401202b2012-12-03 11:36:21 -0800658
659 synchronized(mReceiver) {
660 mQuietEnableExternal = false;
661 mEnableExternal = true;
662 // waive WRITE_SECURE_SETTINGS permission check
Zhihai Xu401202b2012-12-03 11:36:21 -0800663 sendEnableMsg(false);
664 }
Jeff Sharkey67609c72016-03-05 14:29:13 -0700665 if (DBG) Slog.d(TAG, "enable returning");
Zhihai Xu401202b2012-12-03 11:36:21 -0800666 return true;
fredc0f420372012-04-12 00:02:00 -0700667 }
668
669 public boolean disable(boolean persist) {
670 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
671 "Need BLUETOOTH ADMIN permissicacheNameAndAddresson");
Zhihai Xu40874a02012-10-08 17:57:03 -0700672
Zhihai Xu6eb76522012-11-29 15:41:04 -0800673 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
674 (!checkIfCallerIsForegroundUser())) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700675 Slog.w(TAG,"disable(): not allowed for non-active and non system user");
Zhihai Xu40874a02012-10-08 17:57:03 -0700676 return false;
677 }
678
fredcf2458862012-04-16 15:18:27 -0700679 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700680 Slog.d(TAG,"disable(): mBluetooth = " + mBluetooth +
Matthew Xiecdce0b92012-07-12 19:06:15 -0700681 " mBinding = " + mBinding);
682 }
fredcf2458862012-04-16 15:18:27 -0700683
Zhihai Xu401202b2012-12-03 11:36:21 -0800684 synchronized(mReceiver) {
685 if (persist) {
686 // waive WRITE_SECURE_SETTINGS permission check
687 long callingIdentity = Binder.clearCallingIdentity();
688 persistBluetoothSetting(BLUETOOTH_OFF);
689 Binder.restoreCallingIdentity(callingIdentity);
690 }
691 mEnableExternal = false;
692 sendDisableMsg();
693 }
fredc0f420372012-04-12 00:02:00 -0700694 return true;
695 }
696
fredc649fe492012-04-19 01:07:18 -0700697 public void unbindAndFinish() {
fredcf2458862012-04-16 15:18:27 -0700698 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700699 Slog.d(TAG,"unbindAndFinish(): " + mBluetooth +
Matthew Xiecdce0b92012-07-12 19:06:15 -0700700 " mBinding = " + mBinding);
fredcf2458862012-04-16 15:18:27 -0700701 }
702
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700703 try {
704 mBluetoothLock.writeLock().lock();
fredc0f420372012-04-12 00:02:00 -0700705 if (mUnbinding) return;
706 mUnbinding = true;
Pavlin Radoslavove47ec142016-06-01 22:25:18 -0700707 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
Pavlin Radoslavov74f60c02016-09-21 17:28:11 -0700708 mHandler.removeMessages(MESSAGE_BIND_PROFILE_SERVICE);
Zhihai Xu40874a02012-10-08 17:57:03 -0700709 if (mBluetooth != null) {
Andre Eisenbach305fdab2015-11-11 21:43:26 -0800710 //Unregister callback object
711 try {
712 mBluetooth.unregisterCallback(mBluetoothCallback);
713 } catch (RemoteException re) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700714 Slog.e(TAG, "Unable to unregister BluetoothCallback",re);
fredcbf072a72012-05-09 16:52:50 -0700715 }
Andre Eisenbach305fdab2015-11-11 21:43:26 -0800716
Jeff Sharkey67609c72016-03-05 14:29:13 -0700717 if (DBG) Slog.d(TAG, "Sending unbind request.");
Marie Janssen9db28eb2016-01-12 16:05:15 -0800718 mBluetoothBinder = null;
fredcd6883532012-04-25 17:46:13 -0700719 mBluetooth = null;
720 //Unbind
fredc0f420372012-04-12 00:02:00 -0700721 mContext.unbindService(mConnection);
fredcd6883532012-04-25 17:46:13 -0700722 mUnbinding = false;
Zhihai Xu40874a02012-10-08 17:57:03 -0700723 mBinding = false;
fredcf2458862012-04-16 15:18:27 -0700724 } else {
725 mUnbinding=false;
fredc0f420372012-04-12 00:02:00 -0700726 }
Nitin Arorad055adb2015-03-02 15:03:51 -0800727 mBluetoothGatt = null;
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -0700728 } finally {
729 mBluetoothLock.writeLock().unlock();
fredc0f420372012-04-12 00:02:00 -0700730 }
731 }
732
Matthew Xieddf7e472013-03-01 18:41:02 -0800733 public IBluetoothGatt getBluetoothGatt() {
734 // sync protection
735 return mBluetoothGatt;
736 }
737
Benjamin Franze8b98922014-11-12 15:57:54 +0000738 @Override
739 public boolean bindBluetoothProfileService(int bluetoothProfile,
740 IBluetoothProfileServiceConnection proxy) {
741 if (!mEnable) {
742 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700743 Slog.d(TAG, "Trying to bind to profile: " + bluetoothProfile +
Benjamin Franze8b98922014-11-12 15:57:54 +0000744 ", while Bluetooth was disabled");
745 }
746 return false;
747 }
748 synchronized (mProfileServices) {
749 ProfileServiceConnections psc = mProfileServices.get(new Integer(bluetoothProfile));
750 if (psc == null) {
751 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700752 Slog.d(TAG, "Creating new ProfileServiceConnections object for"
Benjamin Franze8b98922014-11-12 15:57:54 +0000753 + " profile: " + bluetoothProfile);
754 }
Benjamin Franz5b614592014-12-09 18:58:45 +0000755
756 if (bluetoothProfile != BluetoothProfile.HEADSET) return false;
757
758 Intent intent = new Intent(IBluetoothHeadset.class.getName());
Benjamin Franze8b98922014-11-12 15:57:54 +0000759 psc = new ProfileServiceConnections(intent);
Benjamin Franz5b614592014-12-09 18:58:45 +0000760 if (!psc.bindService()) return false;
761
Benjamin Franze8b98922014-11-12 15:57:54 +0000762 mProfileServices.put(new Integer(bluetoothProfile), psc);
Benjamin Franze8b98922014-11-12 15:57:54 +0000763 }
764 }
765
766 // Introducing a delay to give the client app time to prepare
767 Message addProxyMsg = mHandler.obtainMessage(MESSAGE_ADD_PROXY_DELAYED);
768 addProxyMsg.arg1 = bluetoothProfile;
769 addProxyMsg.obj = proxy;
770 mHandler.sendMessageDelayed(addProxyMsg, ADD_PROXY_DELAY_MS);
771 return true;
772 }
773
774 @Override
775 public void unbindBluetoothProfileService(int bluetoothProfile,
776 IBluetoothProfileServiceConnection proxy) {
777 synchronized (mProfileServices) {
778 ProfileServiceConnections psc = mProfileServices.get(new Integer(bluetoothProfile));
779 if (psc == null) {
780 return;
781 }
782 psc.removeProxy(proxy);
783 }
784 }
785
786 private void unbindAllBluetoothProfileServices() {
787 synchronized (mProfileServices) {
788 for (Integer i : mProfileServices.keySet()) {
789 ProfileServiceConnections psc = mProfileServices.get(i);
Benjamin Franz5b614592014-12-09 18:58:45 +0000790 try {
791 mContext.unbindService(psc);
792 } catch (IllegalArgumentException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700793 Slog.e(TAG, "Unable to unbind service with intent: " + psc.mIntent, e);
Benjamin Franz5b614592014-12-09 18:58:45 +0000794 }
Benjamin Franze8b98922014-11-12 15:57:54 +0000795 psc.removeAllProxies();
796 }
797 mProfileServices.clear();
798 }
799 }
800
801 /**
Miao Chou658bf2f2015-06-26 17:14:35 -0700802 * Send enable message and set adapter name and address. Called when the boot phase becomes
803 * PHASE_SYSTEM_SERVICES_READY.
804 */
805 public void handleOnBootPhase() {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700806 if (DBG) Slog.d(TAG, "Bluetooth boot completed");
Miao Chou658bf2f2015-06-26 17:14:35 -0700807 if (mEnableExternal && isBluetoothPersistedStateOnBluetooth()) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700808 if (DBG) Slog.d(TAG, "Auto-enabling Bluetooth.");
Miao Chou658bf2f2015-06-26 17:14:35 -0700809 sendEnableMsg(mQuietEnableExternal);
Ajay Panickerbf796d82016-03-11 13:47:20 -0800810 } else if (!isNameAndAddressSet()) {
811 if (DBG) Slog.d(TAG, "Getting adapter name and address");
Ajay Panicker4bb48302016-03-31 14:14:27 -0700812 Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
813 mHandler.sendMessage(getMsg);
Miao Chou658bf2f2015-06-26 17:14:35 -0700814 }
Miao Chou658bf2f2015-06-26 17:14:35 -0700815 }
816
817 /**
818 * Called when switching to a different foreground user.
819 */
820 public void handleOnSwitchUser(int userHandle) {
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -0700821 if (DBG) Slog.d(TAG, "User " + userHandle + " switched");
822 mHandler.obtainMessage(MESSAGE_USER_SWITCHED, userHandle, 0).sendToTarget();
823 }
824
825 /**
826 * Called when user is unlocked.
827 */
828 public void handleOnUnlockUser(int userHandle) {
829 if (DBG) Slog.d(TAG, "User " + userHandle + " unlocked");
830 mHandler.obtainMessage(MESSAGE_USER_UNLOCKED, userHandle, 0).sendToTarget();
Miao Chou658bf2f2015-06-26 17:14:35 -0700831 }
832
833 /**
Benjamin Franze8b98922014-11-12 15:57:54 +0000834 * This class manages the clients connected to a given ProfileService
835 * and maintains the connection with that service.
836 */
837 final private class ProfileServiceConnections implements ServiceConnection,
838 IBinder.DeathRecipient {
839 final RemoteCallbackList<IBluetoothProfileServiceConnection> mProxies =
840 new RemoteCallbackList <IBluetoothProfileServiceConnection>();
841 IBinder mService;
842 ComponentName mClassName;
843 Intent mIntent;
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700844 boolean mInvokingProxyCallbacks = false;
Benjamin Franze8b98922014-11-12 15:57:54 +0000845
846 ProfileServiceConnections(Intent intent) {
847 mService = null;
848 mClassName = null;
849 mIntent = intent;
850 }
851
Benjamin Franz5b614592014-12-09 18:58:45 +0000852 private boolean bindService() {
853 if (mIntent != null && mService == null &&
854 doBind(mIntent, this, 0, UserHandle.CURRENT_OR_SELF)) {
Benjamin Franze8b98922014-11-12 15:57:54 +0000855 Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE);
856 msg.obj = this;
857 mHandler.sendMessageDelayed(msg, TIMEOUT_BIND_MS);
Benjamin Franz5b614592014-12-09 18:58:45 +0000858 return true;
Benjamin Franze8b98922014-11-12 15:57:54 +0000859 }
Jeff Sharkey67609c72016-03-05 14:29:13 -0700860 Slog.w(TAG, "Unable to bind with intent: " + mIntent);
Benjamin Franz5b614592014-12-09 18:58:45 +0000861 return false;
Benjamin Franze8b98922014-11-12 15:57:54 +0000862 }
863
864 private void addProxy(IBluetoothProfileServiceConnection proxy) {
865 mProxies.register(proxy);
866 if (mService != null) {
867 try{
868 proxy.onServiceConnected(mClassName, mService);
869 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700870 Slog.e(TAG, "Unable to connect to proxy", e);
Benjamin Franze8b98922014-11-12 15:57:54 +0000871 }
872 } else {
873 if (!mHandler.hasMessages(MESSAGE_BIND_PROFILE_SERVICE, this)) {
874 Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE);
875 msg.obj = this;
876 mHandler.sendMessage(msg);
877 }
878 }
879 }
880
881 private void removeProxy(IBluetoothProfileServiceConnection proxy) {
882 if (proxy != null) {
883 if (mProxies.unregister(proxy)) {
884 try {
885 proxy.onServiceDisconnected(mClassName);
886 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700887 Slog.e(TAG, "Unable to disconnect proxy", e);
Benjamin Franze8b98922014-11-12 15:57:54 +0000888 }
889 }
890 } else {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700891 Slog.w(TAG, "Trying to remove a null proxy");
Benjamin Franze8b98922014-11-12 15:57:54 +0000892 }
893 }
894
895 private void removeAllProxies() {
896 onServiceDisconnected(mClassName);
897 mProxies.kill();
898 }
899
900 @Override
901 public void onServiceConnected(ComponentName className, IBinder service) {
902 // remove timeout message
903 mHandler.removeMessages(MESSAGE_BIND_PROFILE_SERVICE, this);
904 mService = service;
905 mClassName = className;
906 try {
907 mService.linkToDeath(this, 0);
908 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700909 Slog.e(TAG, "Unable to linkToDeath", e);
Benjamin Franze8b98922014-11-12 15:57:54 +0000910 }
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700911
912 if (mInvokingProxyCallbacks) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700913 Slog.e(TAG, "Proxy callbacks already in progress.");
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700914 return;
Benjamin Franze8b98922014-11-12 15:57:54 +0000915 }
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700916 mInvokingProxyCallbacks = true;
917
918 final int n = mProxies.beginBroadcast();
919 try {
920 for (int i = 0; i < n; i++) {
921 try {
922 mProxies.getBroadcastItem(i).onServiceConnected(className, service);
923 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700924 Slog.e(TAG, "Unable to connect to proxy", e);
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700925 }
926 }
927 } finally {
928 mProxies.finishBroadcast();
929 mInvokingProxyCallbacks = false;
930 }
Benjamin Franze8b98922014-11-12 15:57:54 +0000931 }
932
933 @Override
934 public void onServiceDisconnected(ComponentName className) {
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700935 if (mService == null) return;
Benjamin Franze8b98922014-11-12 15:57:54 +0000936 mService.unlinkToDeath(this, 0);
937 mService = null;
938 mClassName = null;
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700939
940 if (mInvokingProxyCallbacks) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700941 Slog.e(TAG, "Proxy callbacks already in progress.");
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700942 return;
Benjamin Franze8b98922014-11-12 15:57:54 +0000943 }
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700944 mInvokingProxyCallbacks = true;
945
946 final int n = mProxies.beginBroadcast();
947 try {
948 for (int i = 0; i < n; i++) {
949 try {
950 mProxies.getBroadcastItem(i).onServiceDisconnected(className);
951 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700952 Slog.e(TAG, "Unable to disconnect from proxy", e);
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700953 }
954 }
955 } finally {
956 mProxies.finishBroadcast();
957 mInvokingProxyCallbacks = false;
958 }
Benjamin Franze8b98922014-11-12 15:57:54 +0000959 }
960
961 @Override
962 public void binderDied() {
963 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700964 Slog.w(TAG, "Profile service for profile: " + mClassName
Benjamin Franze8b98922014-11-12 15:57:54 +0000965 + " died.");
966 }
967 onServiceDisconnected(mClassName);
968 // Trigger rebind
969 Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE);
970 msg.obj = this;
971 mHandler.sendMessageDelayed(msg, TIMEOUT_BIND_MS);
972 }
973 }
974
fredcbf072a72012-05-09 16:52:50 -0700975 private void sendBluetoothStateCallback(boolean isUp) {
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700976 try {
977 int n = mStateChangeCallbacks.beginBroadcast();
Jeff Sharkey67609c72016-03-05 14:29:13 -0700978 if (DBG) Slog.d(TAG,"Broadcasting onBluetoothStateChange("+isUp+") to " + n + " receivers.");
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700979 for (int i=0; i <n;i++) {
980 try {
981 mStateChangeCallbacks.getBroadcastItem(i).onBluetoothStateChange(isUp);
982 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700983 Slog.e(TAG, "Unable to call onBluetoothStateChange() on callback #" + i , e);
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700984 }
fredcbf072a72012-05-09 16:52:50 -0700985 }
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700986 } finally {
987 mStateChangeCallbacks.finishBroadcast();
fredcbf072a72012-05-09 16:52:50 -0700988 }
fredcbf072a72012-05-09 16:52:50 -0700989 }
990
991 /**
Zhihai Xu40874a02012-10-08 17:57:03 -0700992 * Inform BluetoothAdapter instances that Adapter service is up
993 */
994 private void sendBluetoothServiceUpCallback() {
Jeff Sharkey67609c72016-03-05 14:29:13 -0700995 if (DBG) Slog.d(TAG,"Calling onBluetoothServiceUp callbacks");
Andre Eisenbach305fdab2015-11-11 21:43:26 -0800996 try {
997 int n = mCallbacks.beginBroadcast();
Jeff Sharkey67609c72016-03-05 14:29:13 -0700998 Slog.d(TAG,"Broadcasting onBluetoothServiceUp() to " + n + " receivers.");
Andre Eisenbach305fdab2015-11-11 21:43:26 -0800999 for (int i=0; i <n;i++) {
1000 try {
1001 mCallbacks.getBroadcastItem(i).onBluetoothServiceUp(mBluetooth);
1002 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001003 Slog.e(TAG, "Unable to call onBluetoothServiceUp() on callback #" + i, e);
Zhihai Xu40874a02012-10-08 17:57:03 -07001004 }
1005 }
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001006 } finally {
1007 mCallbacks.finishBroadcast();
Zhihai Xu40874a02012-10-08 17:57:03 -07001008 }
1009 }
1010 /**
fredcbf072a72012-05-09 16:52:50 -07001011 * Inform BluetoothAdapter instances that Adapter service is down
1012 */
1013 private void sendBluetoothServiceDownCallback() {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001014 if (DBG) Slog.d(TAG,"Calling onBluetoothServiceDown callbacks");
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001015 try {
1016 int n = mCallbacks.beginBroadcast();
Jeff Sharkey67609c72016-03-05 14:29:13 -07001017 Slog.d(TAG,"Broadcasting onBluetoothServiceDown() to " + n + " receivers.");
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001018 for (int i=0; i <n;i++) {
1019 try {
1020 mCallbacks.getBroadcastItem(i).onBluetoothServiceDown();
1021 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001022 Slog.e(TAG, "Unable to call onBluetoothServiceDown() on callback #" + i, e);
fredcd6883532012-04-25 17:46:13 -07001023 }
1024 }
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001025 } finally {
1026 mCallbacks.finishBroadcast();
fredcd6883532012-04-25 17:46:13 -07001027 }
1028 }
Svet Ganov408abf72015-05-12 19:13:36 -07001029
fredc0f420372012-04-12 00:02:00 -07001030 public String getAddress() {
Matthew Xieaf5ddbf2012-12-04 10:47:43 -08001031 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
Svet Ganov408abf72015-05-12 19:13:36 -07001032 "Need BLUETOOTH permission");
Zhihai Xu40874a02012-10-08 17:57:03 -07001033
Zhihai Xu6eb76522012-11-29 15:41:04 -08001034 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
Svet Ganov408abf72015-05-12 19:13:36 -07001035 (!checkIfCallerIsForegroundUser())) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001036 Slog.w(TAG,"getAddress(): not allowed for non-active and non system user");
Zhihai Xu6eb76522012-11-29 15:41:04 -08001037 return null;
Zhihai Xu40874a02012-10-08 17:57:03 -07001038 }
1039
Svet Ganov408abf72015-05-12 19:13:36 -07001040 if (mContext.checkCallingOrSelfPermission(Manifest.permission.LOCAL_MAC_ADDRESS)
1041 != PackageManager.PERMISSION_GRANTED) {
1042 return BluetoothAdapter.DEFAULT_MAC_ADDRESS;
1043 }
1044
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001045 try {
1046 mBluetoothLock.readLock().lock();
1047 if (mBluetooth != null) return mBluetooth.getAddress();
1048 } catch (RemoteException e) {
1049 Slog.e(TAG, "getAddress(): Unable to retrieve address remotely. Returning cached address", e);
1050 } finally {
1051 mBluetoothLock.readLock().unlock();
fredc116d1d462012-04-20 14:47:08 -07001052 }
Ajay Panickerbf796d82016-03-11 13:47:20 -08001053
Matthew Xiecdce0b92012-07-12 19:06:15 -07001054 // mAddress is accessed from outside.
1055 // It is alright without a lock. Here, bluetooth is off, no other thread is
1056 // changing mAddress
fredc0f420372012-04-12 00:02:00 -07001057 return mAddress;
1058 }
fredc649fe492012-04-19 01:07:18 -07001059
fredc0f420372012-04-12 00:02:00 -07001060 public String getName() {
Matthew Xieaf5ddbf2012-12-04 10:47:43 -08001061 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
1062 "Need BLUETOOTH permission");
Zhihai Xu40874a02012-10-08 17:57:03 -07001063
Zhihai Xu6eb76522012-11-29 15:41:04 -08001064 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
1065 (!checkIfCallerIsForegroundUser())) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001066 Slog.w(TAG,"getName(): not allowed for non-active and non system user");
Zhihai Xu6eb76522012-11-29 15:41:04 -08001067 return null;
Zhihai Xu40874a02012-10-08 17:57:03 -07001068 }
1069
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001070 try {
1071 mBluetoothLock.readLock().lock();
1072 if (mBluetooth != null) return mBluetooth.getName();
1073 } catch (RemoteException e) {
1074 Slog.e(TAG, "getName(): Unable to retrieve name remotely. Returning cached name", e);
1075 } finally {
1076 mBluetoothLock.readLock().unlock();
fredc116d1d462012-04-20 14:47:08 -07001077 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001078
Matthew Xiecdce0b92012-07-12 19:06:15 -07001079 // mName is accessed from outside.
1080 // It alright without a lock. Here, bluetooth is off, no other thread is
1081 // changing mName
fredc0f420372012-04-12 00:02:00 -07001082 return mName;
1083 }
1084
fredc0f420372012-04-12 00:02:00 -07001085 private class BluetoothServiceConnection implements ServiceConnection {
fredc0f420372012-04-12 00:02:00 -07001086 public void onServiceConnected(ComponentName className, IBinder service) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001087 if (DBG) Slog.d(TAG, "BluetoothServiceConnection: " + className.getClassName());
fredc0f420372012-04-12 00:02:00 -07001088 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED);
Matthew Xieddf7e472013-03-01 18:41:02 -08001089 // TBD if (className.getClassName().equals(IBluetooth.class.getName())) {
1090 if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) {
1091 msg.arg1 = SERVICE_IBLUETOOTH;
1092 // } else if (className.getClassName().equals(IBluetoothGatt.class.getName())) {
1093 } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) {
1094 msg.arg1 = SERVICE_IBLUETOOTHGATT;
1095 } else {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001096 Slog.e(TAG, "Unknown service connected: " + className.getClassName());
Matthew Xieddf7e472013-03-01 18:41:02 -08001097 return;
1098 }
fredc0f420372012-04-12 00:02:00 -07001099 msg.obj = service;
1100 mHandler.sendMessage(msg);
1101 }
1102
1103 public void onServiceDisconnected(ComponentName className) {
fredc0f420372012-04-12 00:02:00 -07001104 // Called if we unexpected disconnected.
Jeff Sharkey67609c72016-03-05 14:29:13 -07001105 if (DBG) Slog.d(TAG, "BluetoothServiceConnection, disconnected: " +
Matthew Xieddf7e472013-03-01 18:41:02 -08001106 className.getClassName());
fredc0f420372012-04-12 00:02:00 -07001107 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED);
Matthew Xieddf7e472013-03-01 18:41:02 -08001108 if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) {
1109 msg.arg1 = SERVICE_IBLUETOOTH;
1110 } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) {
1111 msg.arg1 = SERVICE_IBLUETOOTHGATT;
1112 } else {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001113 Slog.e(TAG, "Unknown service disconnected: " + className.getClassName());
Matthew Xieddf7e472013-03-01 18:41:02 -08001114 return;
1115 }
fredc0f420372012-04-12 00:02:00 -07001116 mHandler.sendMessage(msg);
1117 }
1118 }
1119
1120 private BluetoothServiceConnection mConnection = new BluetoothServiceConnection();
1121
Zhihai Xu40874a02012-10-08 17:57:03 -07001122 private class BluetoothHandler extends Handler {
Ajay Panicker4bb48302016-03-31 14:14:27 -07001123 boolean mGetNameAddressOnly = false;
1124
Zhihai Xu40874a02012-10-08 17:57:03 -07001125 public BluetoothHandler(Looper looper) {
1126 super(looper);
1127 }
1128
fredc0f420372012-04-12 00:02:00 -07001129 @Override
1130 public void handleMessage(Message msg) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001131 if (DBG) Slog.d (TAG, "Message: " + msg.what);
fredc0f420372012-04-12 00:02:00 -07001132 switch (msg.what) {
Ajay Panicker4bb48302016-03-31 14:14:27 -07001133 case MESSAGE_GET_NAME_AND_ADDRESS:
1134 if (DBG) Slog.d(TAG, "MESSAGE_GET_NAME_AND_ADDRESS");
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001135 try {
1136 mBluetoothLock.writeLock().lock();
Ajay Panicker4bb48302016-03-31 14:14:27 -07001137 if ((mBluetooth == null) && (!mBinding)) {
1138 if (DBG) Slog.d(TAG, "Binding to service to get name and address");
1139 mGetNameAddressOnly = true;
1140 Message timeoutMsg = mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
1141 mHandler.sendMessageDelayed(timeoutMsg, TIMEOUT_BIND_MS);
1142 Intent i = new Intent(IBluetooth.class.getName());
1143 if (!doBind(i, mConnection,
1144 Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT,
1145 UserHandle.CURRENT)) {
1146 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
1147 } else {
1148 mBinding = true;
1149 }
1150 } else if (mBluetooth != null) {
1151 try {
1152 storeNameAndAddress(mBluetooth.getName(),
1153 mBluetooth.getAddress());
1154 } catch (RemoteException re) {
1155 Slog.e(TAG, "Unable to grab names", re);
1156 }
1157 if (mGetNameAddressOnly && !mEnable) {
1158 unbindAndFinish();
1159 }
1160 mGetNameAddressOnly = false;
1161 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001162 } finally {
1163 mBluetoothLock.writeLock().unlock();
Ajay Panicker4bb48302016-03-31 14:14:27 -07001164 }
1165 break;
1166
Matthew Xiecdce0b92012-07-12 19:06:15 -07001167 case MESSAGE_ENABLE:
fredcf2458862012-04-16 15:18:27 -07001168 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001169 Slog.d(TAG, "MESSAGE_ENABLE: mBluetooth = " + mBluetooth);
fredc649fe492012-04-19 01:07:18 -07001170 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001171 mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
1172 mEnable = true;
Calvin Ona0b91d72016-06-15 17:58:23 -07001173
1174 // Use service interface to get the exact state
1175 try {
1176 mBluetoothLock.readLock().lock();
1177 if (mBluetooth != null) {
1178 int state = mBluetooth.getState();
1179 if (state == BluetoothAdapter.STATE_BLE_ON) {
1180 Slog.w(TAG, "BT is in BLE_ON State");
1181 mBluetooth.onLeServiceUp();
1182 break;
1183 }
1184 }
1185 } catch (RemoteException e) {
1186 Slog.e(TAG, "", e);
1187 } finally {
1188 mBluetoothLock.readLock().unlock();
1189 }
1190
1191 mQuietEnable = (msg.arg1 == 1);
Pavlin Radoslavove47ec142016-06-01 22:25:18 -07001192 if (mBluetooth == null) {
Calvin Ona0b91d72016-06-15 17:58:23 -07001193 handleEnable(mQuietEnable);
Pavlin Radoslavove47ec142016-06-01 22:25:18 -07001194 } else {
1195 //
1196 // We need to wait until transitioned to STATE_OFF and
1197 // the previous Bluetooth process has exited. The
1198 // waiting period has three components:
1199 // (a) Wait until the local state is STATE_OFF. This
1200 // is accomplished by "waitForOnOff(false, true)".
1201 // (b) Wait until the STATE_OFF state is updated to
1202 // all components.
1203 // (c) Wait until the Bluetooth process exits, and
1204 // ActivityManager detects it.
1205 // The waiting for (b) and (c) is accomplished by
1206 // delaying the MESSAGE_RESTART_BLUETOOTH_SERVICE
1207 // message. On slower devices, that delay needs to be
1208 // on the order of (2 * SERVICE_RESTART_TIME_MS).
1209 //
1210 waitForOnOff(false, true);
Pavlin Radoslavove47ec142016-06-01 22:25:18 -07001211 Message restartMsg = mHandler.obtainMessage(
1212 MESSAGE_RESTART_BLUETOOTH_SERVICE);
1213 mHandler.sendMessageDelayed(restartMsg,
1214 2 * SERVICE_RESTART_TIME_MS);
1215 }
fredc649fe492012-04-19 01:07:18 -07001216 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001217
fredc0f420372012-04-12 00:02:00 -07001218 case MESSAGE_DISABLE:
Zhihai Xu40874a02012-10-08 17:57:03 -07001219 mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
1220 if (mEnable && mBluetooth != null) {
1221 waitForOnOff(true, false);
1222 mEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -08001223 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -07001224 waitForOnOff(false, false);
1225 } else {
1226 mEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -08001227 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -07001228 }
fredc0f420372012-04-12 00:02:00 -07001229 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001230
fredc0f420372012-04-12 00:02:00 -07001231 case MESSAGE_REGISTER_ADAPTER:
1232 {
1233 IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -07001234 boolean added = mCallbacks.register(callback);
Jeff Sharkey67609c72016-03-05 14:29:13 -07001235 Slog.d(TAG,"Added callback: " + (callback == null? "null": callback) +":" +added );
fredc0f420372012-04-12 00:02:00 -07001236 }
1237 break;
1238 case MESSAGE_UNREGISTER_ADAPTER:
1239 {
1240 IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -07001241 boolean removed = mCallbacks.unregister(callback);
Jeff Sharkey67609c72016-03-05 14:29:13 -07001242 Slog.d(TAG,"Removed callback: " + (callback == null? "null": callback) +":" + removed);
fredc0f420372012-04-12 00:02:00 -07001243 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001244 }
fredc0f420372012-04-12 00:02:00 -07001245 case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK:
1246 {
1247 IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj;
Matthew Xie9b693992013-10-10 11:21:40 -07001248 if (callback != null) {
1249 mStateChangeCallbacks.register(callback);
1250 }
fredc0f420372012-04-12 00:02:00 -07001251 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001252 }
fredc0f420372012-04-12 00:02:00 -07001253 case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK:
1254 {
1255 IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj;
Matthew Xie9b693992013-10-10 11:21:40 -07001256 if (callback != null) {
1257 mStateChangeCallbacks.unregister(callback);
1258 }
fredc0f420372012-04-12 00:02:00 -07001259 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001260 }
Benjamin Franze8b98922014-11-12 15:57:54 +00001261 case MESSAGE_ADD_PROXY_DELAYED:
1262 {
1263 ProfileServiceConnections psc = mProfileServices.get(
1264 new Integer(msg.arg1));
1265 if (psc == null) {
1266 break;
1267 }
1268 IBluetoothProfileServiceConnection proxy =
1269 (IBluetoothProfileServiceConnection) msg.obj;
1270 psc.addProxy(proxy);
1271 break;
1272 }
1273 case MESSAGE_BIND_PROFILE_SERVICE:
1274 {
1275 ProfileServiceConnections psc = (ProfileServiceConnections) msg.obj;
1276 removeMessages(MESSAGE_BIND_PROFILE_SERVICE, msg.obj);
1277 if (psc == null) {
1278 break;
1279 }
1280 psc.bindService();
1281 break;
1282 }
fredc0f420372012-04-12 00:02:00 -07001283 case MESSAGE_BLUETOOTH_SERVICE_CONNECTED:
1284 {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001285 if (DBG) Slog.d(TAG,"MESSAGE_BLUETOOTH_SERVICE_CONNECTED: " + msg.arg1);
fredc0f420372012-04-12 00:02:00 -07001286
1287 IBinder service = (IBinder) msg.obj;
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001288 try {
1289 mBluetoothLock.writeLock().lock();
Matthew Xieddf7e472013-03-01 18:41:02 -08001290 if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
1291 mBluetoothGatt = IBluetoothGatt.Stub.asInterface(service);
Nitin Arorad055adb2015-03-02 15:03:51 -08001292 onBluetoothGattServiceUp();
Matthew Xieddf7e472013-03-01 18:41:02 -08001293 break;
1294 } // else must be SERVICE_IBLUETOOTH
1295
1296 //Remove timeout
Zhihai Xuaf5971e2013-06-10 20:28:31 -07001297 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
Matthew Xieddf7e472013-03-01 18:41:02 -08001298
fredc0f420372012-04-12 00:02:00 -07001299 mBinding = false;
Marie Janssen9db28eb2016-01-12 16:05:15 -08001300 mBluetoothBinder = service;
fredc0f420372012-04-12 00:02:00 -07001301 mBluetooth = IBluetooth.Stub.asInterface(service);
fredc0f420372012-04-12 00:02:00 -07001302
Ajay Panicker4bb48302016-03-31 14:14:27 -07001303 if (!isNameAndAddressSet()) {
1304 Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
1305 mHandler.sendMessage(getMsg);
1306 if (mGetNameAddressOnly) return;
1307 }
1308
Zhihai Xuaf5971e2013-06-10 20:28:31 -07001309 try {
1310 boolean enableHciSnoopLog = (Settings.Secure.getInt(mContentResolver,
1311 Settings.Secure.BLUETOOTH_HCI_LOG, 0) == 1);
1312 if (!mBluetooth.configHciSnoopLog(enableHciSnoopLog)) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001313 Slog.e(TAG,"IBluetooth.configHciSnoopLog return false");
Zhihai Xuaf5971e2013-06-10 20:28:31 -07001314 }
1315 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001316 Slog.e(TAG,"Unable to call configHciSnoopLog", e);
Zhihai Xuaf5971e2013-06-10 20:28:31 -07001317 }
1318
Matthew Xiecdce0b92012-07-12 19:06:15 -07001319 //Register callback object
fredcbf072a72012-05-09 16:52:50 -07001320 try {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001321 mBluetooth.registerCallback(mBluetoothCallback);
1322 } catch (RemoteException re) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001323 Slog.e(TAG, "Unable to register BluetoothCallback",re);
fredcbf072a72012-05-09 16:52:50 -07001324 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001325 //Inform BluetoothAdapter instances that service is up
Zhihai Xu40874a02012-10-08 17:57:03 -07001326 sendBluetoothServiceUpCallback();
1327
Matthew Xiecdce0b92012-07-12 19:06:15 -07001328 //Do enable request
1329 try {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001330 if (mQuietEnable == false) {
1331 if(!mBluetooth.enable()) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001332 Slog.e(TAG,"IBluetooth.enable() returned false");
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001333 }
1334 }
1335 else
1336 {
1337 if(!mBluetooth.enableNoAutoConnect()) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001338 Slog.e(TAG,"IBluetooth.enableNoAutoConnect() returned false");
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001339 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001340 }
1341 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001342 Slog.e(TAG,"Unable to call enable()",e);
Matthew Xiecdce0b92012-07-12 19:06:15 -07001343 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001344 } finally {
1345 mBluetoothLock.writeLock().unlock();
Freda8c6df02012-07-11 10:25:23 -07001346 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001347
1348 if (!mEnable) {
1349 waitForOnOff(true, false);
Zhihai Xu401202b2012-12-03 11:36:21 -08001350 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -07001351 waitForOnOff(false, false);
1352 }
fredc649fe492012-04-19 01:07:18 -07001353 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001354 }
fredc649fe492012-04-19 01:07:18 -07001355 case MESSAGE_TIMEOUT_BIND: {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001356 Slog.e(TAG, "MESSAGE_TIMEOUT_BIND");
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001357 mBluetoothLock.writeLock().lock();
1358 mBinding = false;
1359 mBluetoothLock.writeLock().unlock();
1360
fredc649fe492012-04-19 01:07:18 -07001361 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001362 }
fredcbf072a72012-05-09 16:52:50 -07001363 case MESSAGE_BLUETOOTH_STATE_CHANGE:
fredc0f420372012-04-12 00:02:00 -07001364 {
fredcbf072a72012-05-09 16:52:50 -07001365 int prevState = msg.arg1;
1366 int newState = msg.arg2;
Jeff Sharkey67609c72016-03-05 14:29:13 -07001367 if (DBG) Slog.d(TAG, "MESSAGE_BLUETOOTH_STATE_CHANGE: prevState = " + prevState + ", newState=" + newState);
Zhihai Xu40874a02012-10-08 17:57:03 -07001368 mState = newState;
1369 bluetoothStateChangeHandler(prevState, newState);
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001370 // handle error state transition case from TURNING_ON to OFF
1371 // unbind and rebind bluetooth service and enable bluetooth
Nitin Arorad055adb2015-03-02 15:03:51 -08001372 if ((prevState == BluetoothAdapter.STATE_BLE_TURNING_ON) &&
Calvin Ona0b91d72016-06-15 17:58:23 -07001373 (newState == BluetoothAdapter.STATE_OFF) &&
1374 (mBluetooth != null) && mEnable) {
Marie Janssen2977c3e2016-11-09 12:01:24 -08001375 recoverBluetoothServiceFromError(false);
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001376 }
Nitin Arorad055adb2015-03-02 15:03:51 -08001377 if ((prevState == BluetoothAdapter.STATE_TURNING_ON) &&
Calvin Ona0b91d72016-06-15 17:58:23 -07001378 (newState == BluetoothAdapter.STATE_BLE_ON) &&
1379 (mBluetooth != null) && mEnable) {
Marie Janssen2977c3e2016-11-09 12:01:24 -08001380 recoverBluetoothServiceFromError(true);
Nitin Arorad055adb2015-03-02 15:03:51 -08001381 }
Calvin Ona0b91d72016-06-15 17:58:23 -07001382 // If we tried to enable BT while BT was in the process of shutting down,
1383 // wait for the BT process to fully tear down and then force a restart
1384 // here. This is a bit of a hack (b/29363429).
1385 if ((prevState == BluetoothAdapter.STATE_BLE_TURNING_OFF) &&
1386 (newState == BluetoothAdapter.STATE_OFF)) {
1387 if (mEnable) {
1388 Slog.d(TAG, "Entering STATE_OFF but mEnabled is true; restarting.");
1389 waitForOnOff(false, true);
1390 Message restartMsg = mHandler.obtainMessage(
1391 MESSAGE_RESTART_BLUETOOTH_SERVICE);
1392 mHandler.sendMessageDelayed(restartMsg, 2 * SERVICE_RESTART_TIME_MS);
1393 }
1394 }
Nitin Arorad055adb2015-03-02 15:03:51 -08001395 if (newState == BluetoothAdapter.STATE_ON ||
Calvin Ona0b91d72016-06-15 17:58:23 -07001396 newState == BluetoothAdapter.STATE_BLE_ON) {
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001397 // bluetooth is working, reset the counter
1398 if (mErrorRecoveryRetryCounter != 0) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001399 Slog.w(TAG, "bluetooth is recovered from error");
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001400 mErrorRecoveryRetryCounter = 0;
1401 }
1402 }
fredc649fe492012-04-19 01:07:18 -07001403 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001404 }
fredc0f420372012-04-12 00:02:00 -07001405 case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED:
1406 {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001407 Slog.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: " + msg.arg1);
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001408 try {
1409 mBluetoothLock.writeLock().lock();
Matthew Xieddf7e472013-03-01 18:41:02 -08001410 if (msg.arg1 == SERVICE_IBLUETOOTH) {
1411 // if service is unbinded already, do nothing and return
1412 if (mBluetooth == null) break;
1413 mBluetooth = null;
1414 } else if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
1415 mBluetoothGatt = null;
1416 break;
1417 } else {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001418 Slog.e(TAG, "Bad msg.arg1: " + msg.arg1);
Matthew Xieddf7e472013-03-01 18:41:02 -08001419 break;
1420 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001421 } finally {
1422 mBluetoothLock.writeLock().unlock();
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301423 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001424
1425 if (mEnable) {
1426 mEnable = false;
1427 // Send a Bluetooth Restart message
1428 Message restartMsg = mHandler.obtainMessage(
1429 MESSAGE_RESTART_BLUETOOTH_SERVICE);
1430 mHandler.sendMessageDelayed(restartMsg,
1431 SERVICE_RESTART_TIME_MS);
1432 }
1433
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001434 sendBluetoothServiceDownCallback();
Zhihai Xu40874a02012-10-08 17:57:03 -07001435
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001436 // Send BT state broadcast to update
1437 // the BT icon correctly
1438 if ((mState == BluetoothAdapter.STATE_TURNING_ON) ||
Calvin Ona0b91d72016-06-15 17:58:23 -07001439 (mState == BluetoothAdapter.STATE_ON)) {
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001440 bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,
1441 BluetoothAdapter.STATE_TURNING_OFF);
1442 mState = BluetoothAdapter.STATE_TURNING_OFF;
Zhihai Xu40874a02012-10-08 17:57:03 -07001443 }
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001444 if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
1445 bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
1446 BluetoothAdapter.STATE_OFF);
1447 }
1448
1449 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
1450 mState = BluetoothAdapter.STATE_OFF;
fredc649fe492012-04-19 01:07:18 -07001451 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001452 }
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301453 case MESSAGE_RESTART_BLUETOOTH_SERVICE:
1454 {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001455 Slog.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE:"
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301456 +" Restart IBluetooth service");
1457 /* Enable without persisting the setting as
1458 it doesnt change when IBluetooth
1459 service restarts */
Zhihai Xu40874a02012-10-08 17:57:03 -07001460 mEnable = true;
Zhihai Xu401202b2012-12-03 11:36:21 -08001461 handleEnable(mQuietEnable);
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301462 break;
1463 }
1464
fredc0f420372012-04-12 00:02:00 -07001465 case MESSAGE_TIMEOUT_UNBIND:
1466 {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001467 Slog.e(TAG, "MESSAGE_TIMEOUT_UNBIND");
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001468 mBluetoothLock.writeLock().lock();
1469 mUnbinding = false;
1470 mBluetoothLock.writeLock().unlock();
fredc649fe492012-04-19 01:07:18 -07001471 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001472 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001473
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -07001474 case MESSAGE_USER_SWITCHED: {
1475 if (DBG) Slog.d(TAG, "MESSAGE_USER_SWITCHED");
Zhihai Xu40874a02012-10-08 17:57:03 -07001476 mHandler.removeMessages(MESSAGE_USER_SWITCHED);
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -07001477
Zhihai Xu40874a02012-10-08 17:57:03 -07001478 /* disable and enable BT when detect a user switch */
1479 if (mEnable && mBluetooth != null) {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001480 try {
1481 mBluetoothLock.readLock().lock();
Zhihai Xu40874a02012-10-08 17:57:03 -07001482 if (mBluetooth != null) {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001483 mBluetooth.unregisterCallback(mBluetoothCallback);
Zhihai Xu40874a02012-10-08 17:57:03 -07001484 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001485 } catch (RemoteException re) {
1486 Slog.e(TAG, "Unable to unregister", re);
1487 } finally {
1488 mBluetoothLock.readLock().unlock();
Zhihai Xu40874a02012-10-08 17:57:03 -07001489 }
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001490
1491 if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
1492 // MESSAGE_USER_SWITCHED happened right after MESSAGE_ENABLE
1493 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_OFF);
1494 mState = BluetoothAdapter.STATE_OFF;
1495 }
1496 if (mState == BluetoothAdapter.STATE_OFF) {
1497 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_TURNING_ON);
1498 mState = BluetoothAdapter.STATE_TURNING_ON;
1499 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001500
1501 waitForOnOff(true, false);
1502
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001503 if (mState == BluetoothAdapter.STATE_TURNING_ON) {
1504 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_ON);
1505 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001506
Benjamin Franze8b98922014-11-12 15:57:54 +00001507 unbindAllBluetoothProfileServices();
Zhihai Xu40874a02012-10-08 17:57:03 -07001508 // disable
Zhihai Xu401202b2012-12-03 11:36:21 -08001509 handleDisable();
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001510 // Pbap service need receive STATE_TURNING_OFF intent to close
1511 bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,
1512 BluetoothAdapter.STATE_TURNING_OFF);
Zhihai Xu40874a02012-10-08 17:57:03 -07001513
Pavlin Radoslavov41401112016-06-27 15:25:18 -07001514 boolean didDisableTimeout = !waitForOnOff(false, true);
Zhihai Xu40874a02012-10-08 17:57:03 -07001515
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001516 bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
Zhihai Xu40874a02012-10-08 17:57:03 -07001517 BluetoothAdapter.STATE_OFF);
Zhihai Xu40874a02012-10-08 17:57:03 -07001518 sendBluetoothServiceDownCallback();
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001519
Pavlin Radoslavove957a8a2016-05-24 15:28:41 -07001520 try {
1521 mBluetoothLock.writeLock().lock();
1522 if (mBluetooth != null) {
1523 mBluetooth = null;
1524 // Unbind
1525 mContext.unbindService(mConnection);
1526 }
1527 mBluetoothGatt = null;
1528 } finally {
1529 mBluetoothLock.writeLock().unlock();
Zhihai Xu40874a02012-10-08 17:57:03 -07001530 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001531
Pavlin Radoslavov41401112016-06-27 15:25:18 -07001532 //
1533 // If disabling Bluetooth times out, wait for an
1534 // additional amount of time to ensure the process is
1535 // shut down completely before attempting to restart.
1536 //
1537 if (didDisableTimeout) {
1538 SystemClock.sleep(3000);
1539 } else {
1540 SystemClock.sleep(100);
1541 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001542
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001543 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
1544 mState = BluetoothAdapter.STATE_OFF;
Zhihai Xu40874a02012-10-08 17:57:03 -07001545 // enable
Zhihai Xu401202b2012-12-03 11:36:21 -08001546 handleEnable(mQuietEnable);
John Spurlock8a985d22014-02-25 09:40:05 -05001547 } else if (mBinding || mBluetooth != null) {
Zhihai Xu40874a02012-10-08 17:57:03 -07001548 Message userMsg = mHandler.obtainMessage(MESSAGE_USER_SWITCHED);
1549 userMsg.arg2 = 1 + msg.arg2;
1550 // if user is switched when service is being binding
1551 // delay sending MESSAGE_USER_SWITCHED
1552 mHandler.sendMessageDelayed(userMsg, USER_SWITCHED_TIME_MS);
1553 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001554 Slog.d(TAG, "delay MESSAGE_USER_SWITCHED " + userMsg.arg2);
Zhihai Xu40874a02012-10-08 17:57:03 -07001555 }
John Spurlock8a985d22014-02-25 09:40:05 -05001556 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001557 break;
1558 }
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -07001559 case MESSAGE_USER_UNLOCKED: {
1560 if (DBG) Slog.d(TAG, "MESSAGE_USER_UNLOCKED");
1561 mHandler.removeMessages(MESSAGE_USER_SWITCHED);
1562
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001563 if (mEnable && !mBinding && (mBluetooth == null)) {
1564 // We should be connected, but we gave up for some
1565 // reason; maybe the Bluetooth service wasn't encryption
1566 // aware, so try binding again.
1567 if (DBG) Slog.d(TAG, "Enabled but not bound; retrying after unlock");
1568 handleEnable(mQuietEnable);
Jeff Sharkeyaacb89e2016-03-05 14:42:58 -07001569 }
1570 }
fredc0f420372012-04-12 00:02:00 -07001571 }
1572 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001573 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001574
Zhihai Xu401202b2012-12-03 11:36:21 -08001575 private void handleEnable(boolean quietMode) {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001576 mQuietEnable = quietMode;
1577
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001578 try {
1579 mBluetoothLock.writeLock().lock();
Zhihai Xu40874a02012-10-08 17:57:03 -07001580 if ((mBluetooth == null) && (!mBinding)) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001581 //Start bind timeout and bind
1582 Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
1583 mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS);
Matthew Xiecdce0b92012-07-12 19:06:15 -07001584 Intent i = new Intent(IBluetooth.class.getName());
Dianne Hackbornce09f5a2014-10-10 15:03:13 -07001585 if (!doBind(i, mConnection,Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT,
1586 UserHandle.CURRENT)) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001587 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
Zhihai Xu40874a02012-10-08 17:57:03 -07001588 } else {
1589 mBinding = true;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001590 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001591 } else if (mBluetooth != null) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001592 //Enable bluetooth
1593 try {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001594 if (!mQuietEnable) {
1595 if(!mBluetooth.enable()) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001596 Slog.e(TAG,"IBluetooth.enable() returned false");
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001597 }
1598 }
1599 else {
1600 if(!mBluetooth.enableNoAutoConnect()) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001601 Slog.e(TAG,"IBluetooth.enableNoAutoConnect() returned false");
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001602 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001603 }
1604 } catch (RemoteException e) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001605 Slog.e(TAG,"Unable to call enable()",e);
Matthew Xiecdce0b92012-07-12 19:06:15 -07001606 }
1607 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001608 } finally {
1609 mBluetoothLock.writeLock().unlock();
Matthew Xiecdce0b92012-07-12 19:06:15 -07001610 }
1611 }
1612
Dianne Hackborn221ea892013-08-04 16:50:16 -07001613 boolean doBind(Intent intent, ServiceConnection conn, int flags, UserHandle user) {
1614 ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
1615 intent.setComponent(comp);
1616 if (comp == null || !mContext.bindServiceAsUser(intent, conn, flags, user)) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001617 Slog.e(TAG, "Fail to bind to: " + intent);
Dianne Hackborn221ea892013-08-04 16:50:16 -07001618 return false;
1619 }
1620 return true;
1621 }
1622
Zhihai Xu401202b2012-12-03 11:36:21 -08001623 private void handleDisable() {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001624 try {
1625 mBluetoothLock.readLock().lock();
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001626 if (mBluetooth != null) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001627 if (DBG) Slog.d(TAG,"Sending off request.");
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001628 if (!mBluetooth.disable()) {
1629 Slog.e(TAG,"IBluetooth.disable() returned false");
Matthew Xiecdce0b92012-07-12 19:06:15 -07001630 }
1631 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001632 } catch (RemoteException e) {
1633 Slog.e(TAG,"Unable to call disable()",e);
1634 } finally {
1635 mBluetoothLock.readLock().unlock();
Matthew Xiecdce0b92012-07-12 19:06:15 -07001636 }
1637 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001638
1639 private boolean checkIfCallerIsForegroundUser() {
1640 int foregroundUser;
1641 int callingUser = UserHandle.getCallingUserId();
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001642 int callingUid = Binder.getCallingUid();
Zhihai Xu40874a02012-10-08 17:57:03 -07001643 long callingIdentity = Binder.clearCallingIdentity();
Benjamin Franze8b98922014-11-12 15:57:54 +00001644 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
1645 UserInfo ui = um.getProfileParent(callingUser);
1646 int parentUser = (ui != null) ? ui.id : UserHandle.USER_NULL;
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001647 int callingAppId = UserHandle.getAppId(callingUid);
Zhihai Xu40874a02012-10-08 17:57:03 -07001648 boolean valid = false;
1649 try {
1650 foregroundUser = ActivityManager.getCurrentUser();
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001651 valid = (callingUser == foregroundUser) ||
Benjamin Franze8b98922014-11-12 15:57:54 +00001652 parentUser == foregroundUser ||
Adrian Roosbd9a9a52014-08-18 15:31:57 +02001653 callingAppId == Process.NFC_UID ||
1654 callingAppId == mSystemUiUid;
Zhihai Xu40874a02012-10-08 17:57:03 -07001655 if (DBG) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001656 Slog.d(TAG, "checkIfCallerIsForegroundUser: valid=" + valid
Zhihai Xu40874a02012-10-08 17:57:03 -07001657 + " callingUser=" + callingUser
Benjamin Franze8b98922014-11-12 15:57:54 +00001658 + " parentUser=" + parentUser
Zhihai Xu40874a02012-10-08 17:57:03 -07001659 + " foregroundUser=" + foregroundUser);
1660 }
1661 } finally {
1662 Binder.restoreCallingIdentity(callingIdentity);
1663 }
1664 return valid;
1665 }
1666
Nitin Arorad055adb2015-03-02 15:03:51 -08001667 private void sendBleStateChanged(int prevState, int newState) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001668 if (DBG) Slog.d(TAG,"BLE State Change Intent: " + prevState + " -> " + newState);
Nitin Arorad055adb2015-03-02 15:03:51 -08001669 // Send broadcast message to everyone else
1670 Intent intent = new Intent(BluetoothAdapter.ACTION_BLE_STATE_CHANGED);
1671 intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState);
1672 intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState);
1673 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1674 mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM);
1675 }
1676
Zhihai Xu40874a02012-10-08 17:57:03 -07001677 private void bluetoothStateChangeHandler(int prevState, int newState) {
Nitin Arorad055adb2015-03-02 15:03:51 -08001678 boolean isStandardBroadcast = true;
Zhihai Xu40874a02012-10-08 17:57:03 -07001679 if (prevState != newState) {
1680 //Notify all proxy objects first of adapter state change
Calvin Ona0b91d72016-06-15 17:58:23 -07001681 if (newState == BluetoothAdapter.STATE_BLE_ON ||
1682 newState == BluetoothAdapter.STATE_OFF) {
Nitin Arorad055adb2015-03-02 15:03:51 -08001683 boolean intermediate_off = (prevState == BluetoothAdapter.STATE_TURNING_OFF
1684 && newState == BluetoothAdapter.STATE_BLE_ON);
Zhihai Xu40874a02012-10-08 17:57:03 -07001685
Nitin Arorad055adb2015-03-02 15:03:51 -08001686 if (newState == BluetoothAdapter.STATE_OFF) {
1687 // If Bluetooth is off, send service down event to proxy objects, and unbind
Jeff Sharkey67609c72016-03-05 14:29:13 -07001688 if (DBG) Slog.d(TAG, "Bluetooth is complete turn off");
Pavlin Radoslavove47ec142016-06-01 22:25:18 -07001689 sendBluetoothServiceDownCallback();
1690 unbindAndFinish();
1691 sendBleStateChanged(prevState, newState);
1692 // Don't broadcast as it has already been broadcast before
1693 isStandardBroadcast = false;
Nitin Arorad055adb2015-03-02 15:03:51 -08001694
1695 } else if (!intermediate_off) {
1696 // connect to GattService
Jeff Sharkey67609c72016-03-05 14:29:13 -07001697 if (DBG) Slog.d(TAG, "Bluetooth is in LE only mode");
Nitin Arorad055adb2015-03-02 15:03:51 -08001698 if (mBluetoothGatt != null) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001699 if (DBG) Slog.d(TAG, "Calling BluetoothGattServiceUp");
Nitin Arorad055adb2015-03-02 15:03:51 -08001700 onBluetoothGattServiceUp();
1701 } else {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001702 if (DBG) Slog.d(TAG, "Binding Bluetooth GATT service");
Nitin Arorad055adb2015-03-02 15:03:51 -08001703 if (mContext.getPackageManager().hasSystemFeature(
1704 PackageManager.FEATURE_BLUETOOTH_LE)) {
1705 Intent i = new Intent(IBluetoothGatt.class.getName());
1706 doBind(i, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, UserHandle.CURRENT);
1707 }
1708 }
1709 sendBleStateChanged(prevState, newState);
1710 //Don't broadcase this as std intent
1711 isStandardBroadcast = false;
1712
1713 } else if (intermediate_off){
Jeff Sharkey67609c72016-03-05 14:29:13 -07001714 if (DBG) Slog.d(TAG, "Intermediate off, back to LE only mode");
Nitin Arorad055adb2015-03-02 15:03:51 -08001715 // For LE only mode, broadcast as is
1716 sendBleStateChanged(prevState, newState);
1717 sendBluetoothStateCallback(false); // BT is OFF for general users
1718 // Broadcast as STATE_OFF
1719 newState = BluetoothAdapter.STATE_OFF;
1720 sendBrEdrDownCallback();
Zhihai Xu40874a02012-10-08 17:57:03 -07001721 }
Nitin Arorad055adb2015-03-02 15:03:51 -08001722 } else if (newState == BluetoothAdapter.STATE_ON) {
1723 boolean isUp = (newState==BluetoothAdapter.STATE_ON);
1724 sendBluetoothStateCallback(isUp);
1725 sendBleStateChanged(prevState, newState);
1726
Calvin Ona0b91d72016-06-15 17:58:23 -07001727 } else if (newState == BluetoothAdapter.STATE_BLE_TURNING_ON ||
1728 newState == BluetoothAdapter.STATE_BLE_TURNING_OFF ) {
Nitin Arorad055adb2015-03-02 15:03:51 -08001729 sendBleStateChanged(prevState, newState);
1730 isStandardBroadcast = false;
1731
Calvin Ona0b91d72016-06-15 17:58:23 -07001732 } else if (newState == BluetoothAdapter.STATE_TURNING_ON ||
1733 newState == BluetoothAdapter.STATE_TURNING_OFF) {
Nitin Arorad055adb2015-03-02 15:03:51 -08001734 sendBleStateChanged(prevState, newState);
Zhihai Xu40874a02012-10-08 17:57:03 -07001735 }
1736
Nitin Arorad055adb2015-03-02 15:03:51 -08001737 if (isStandardBroadcast) {
1738 if (prevState == BluetoothAdapter.STATE_BLE_ON) {
1739 // Show prevState of BLE_ON as OFF to standard users
1740 prevState = BluetoothAdapter.STATE_OFF;
1741 }
1742 Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
1743 intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState);
1744 intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState);
1745 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1746 mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM);
1747 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001748 }
1749 }
1750
1751 /**
1752 * if on is true, wait for state become ON
1753 * if off is true, wait for state become OFF
1754 * if both on and off are false, wait for state not ON
1755 */
1756 private boolean waitForOnOff(boolean on, boolean off) {
1757 int i = 0;
1758 while (i < 10) {
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001759 try {
1760 mBluetoothLock.readLock().lock();
1761 if (mBluetooth == null) break;
1762 if (on) {
1763 if (mBluetooth.getState() == BluetoothAdapter.STATE_ON) return true;
1764 } else if (off) {
1765 if (mBluetooth.getState() == BluetoothAdapter.STATE_OFF) return true;
1766 } else {
1767 if (mBluetooth.getState() != BluetoothAdapter.STATE_ON) return true;
Zhihai Xu40874a02012-10-08 17:57:03 -07001768 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001769 } catch (RemoteException e) {
1770 Slog.e(TAG, "getState()", e);
1771 break;
1772 } finally {
1773 mBluetoothLock.readLock().unlock();
Zhihai Xu40874a02012-10-08 17:57:03 -07001774 }
1775 if (on || off) {
1776 SystemClock.sleep(300);
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001777 } else {
Zhihai Xu40874a02012-10-08 17:57:03 -07001778 SystemClock.sleep(50);
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001779 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001780 i++;
1781 }
Jeff Sharkey67609c72016-03-05 14:29:13 -07001782 Slog.e(TAG,"waitForOnOff time out");
Zhihai Xu40874a02012-10-08 17:57:03 -07001783 return false;
1784 }
Zhihai Xu681ae7f2012-11-12 15:14:18 -08001785
Zhihai Xu401202b2012-12-03 11:36:21 -08001786 private void sendDisableMsg() {
1787 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISABLE));
1788 }
1789
1790 private void sendEnableMsg(boolean quietMode) {
1791 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_ENABLE,
1792 quietMode ? 1 : 0, 0));
1793 }
1794
Marie Janssen2977c3e2016-11-09 12:01:24 -08001795 private void recoverBluetoothServiceFromError(boolean clearBle) {
Jeff Sharkey67609c72016-03-05 14:29:13 -07001796 Slog.e(TAG,"recoverBluetoothServiceFromError");
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001797 try {
1798 mBluetoothLock.readLock().lock();
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001799 if (mBluetooth != null) {
1800 //Unregister callback object
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001801 mBluetooth.unregisterCallback(mBluetoothCallback);
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001802 }
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001803 } catch (RemoteException re) {
1804 Slog.e(TAG, "Unable to unregister", re);
1805 } finally {
1806 mBluetoothLock.readLock().unlock();
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001807 }
1808
1809 SystemClock.sleep(500);
1810
1811 // disable
1812 handleDisable();
1813
1814 waitForOnOff(false, true);
1815
1816 sendBluetoothServiceDownCallback();
Pavlin Radoslavoveb50a392016-05-22 22:16:41 -07001817
Pavlin Radoslavove957a8a2016-05-24 15:28:41 -07001818 try {
1819 mBluetoothLock.writeLock().lock();
1820 if (mBluetooth != null) {
1821 mBluetooth = null;
1822 // Unbind
1823 mContext.unbindService(mConnection);
1824 }
1825 mBluetoothGatt = null;
1826 } finally {
1827 mBluetoothLock.writeLock().unlock();
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001828 }
1829
1830 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
1831 mState = BluetoothAdapter.STATE_OFF;
1832
Marie Janssen2977c3e2016-11-09 12:01:24 -08001833 if (clearBle) {
1834 clearBleApps();
1835 }
1836
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001837 mEnable = false;
1838
1839 if (mErrorRecoveryRetryCounter++ < MAX_ERROR_RESTART_RETRIES) {
1840 // Send a Bluetooth Restart message to reenable bluetooth
1841 Message restartMsg = mHandler.obtainMessage(
1842 MESSAGE_RESTART_BLUETOOTH_SERVICE);
1843 mHandler.sendMessageDelayed(restartMsg, ERROR_RESTART_TIME_MS);
1844 } else {
1845 // todo: notify user to power down and power up phone to make bluetooth work.
1846 }
1847 }
Mike Lockwood726d4de2014-10-28 14:06:28 -07001848
1849 @Override
Pavlin Radoslavov6e8faff2016-02-23 11:54:37 -08001850 public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
1851 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
1852 String errorMsg = null;
1853 if (mBluetoothBinder == null) {
1854 errorMsg = "Bluetooth Service not connected";
1855 } else {
1856 try {
1857 mBluetoothBinder.dump(fd, args);
1858 } catch (RemoteException re) {
1859 errorMsg = "RemoteException while calling Bluetooth Service";
1860 }
Mike Lockwood726d4de2014-10-28 14:06:28 -07001861 }
Pavlin Radoslavov6e8faff2016-02-23 11:54:37 -08001862 if (errorMsg != null) {
1863 // Silently return if we are extracting metrics in Protobuf format
1864 if ((args.length > 0) && args[0].startsWith("--proto"))
1865 return;
1866 writer.println(errorMsg);
1867 }
Mike Lockwood726d4de2014-10-28 14:06:28 -07001868 }
fredc0f420372012-04-12 00:02:00 -07001869}