blob: ef82bb70cec47c8038ab163bfeb856bdbd02d3a5 [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
Zhihai Xu40874a02012-10-08 17:57:03 -070019import android.app.ActivityManager;
fredc0f420372012-04-12 00:02:00 -070020import android.bluetooth.BluetoothAdapter;
Benjamin Franze8b98922014-11-12 15:57:54 +000021import android.bluetooth.BluetoothProfile;
fredc0f420372012-04-12 00:02:00 -070022import android.bluetooth.IBluetooth;
Matthew Xieddf7e472013-03-01 18:41:02 -080023import android.bluetooth.IBluetoothGatt;
fredcbf072a72012-05-09 16:52:50 -070024import android.bluetooth.IBluetoothCallback;
Benjamin Franze8b98922014-11-12 15:57:54 +000025import android.bluetooth.IBluetoothHeadset;
fredc0f420372012-04-12 00:02:00 -070026import android.bluetooth.IBluetoothManager;
27import android.bluetooth.IBluetoothManagerCallback;
Benjamin Franze8b98922014-11-12 15:57:54 +000028import android.bluetooth.IBluetoothProfileServiceConnection;
fredc0f420372012-04-12 00:02:00 -070029import android.bluetooth.IBluetoothStateChangeCallback;
fredc0f420372012-04-12 00:02:00 -070030import android.content.BroadcastReceiver;
31import android.content.ComponentName;
32import android.content.ContentResolver;
33import android.content.Context;
34import android.content.Intent;
35import android.content.IntentFilter;
36import android.content.ServiceConnection;
Matthew Xie32ab77b2013-05-08 19:26:57 -070037import android.content.pm.PackageManager;
Benjamin Franze8b98922014-11-12 15:57:54 +000038import android.content.pm.UserInfo;
Zhihai Xu40874a02012-10-08 17:57:03 -070039import android.os.Binder;
fredc0f420372012-04-12 00:02:00 -070040import android.os.Handler;
fredc0f420372012-04-12 00:02:00 -070041import android.os.IBinder;
Zhihai Xu40874a02012-10-08 17:57:03 -070042import android.os.Looper;
fredc0f420372012-04-12 00:02:00 -070043import android.os.Message;
Zhihai Xu40874a02012-10-08 17:57:03 -070044import android.os.Process;
fredcd6883532012-04-25 17:46:13 -070045import android.os.RemoteCallbackList;
fredc0f420372012-04-12 00:02:00 -070046import android.os.RemoteException;
Zhihai Xu40874a02012-10-08 17:57:03 -070047import android.os.SystemClock;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070048import android.os.UserHandle;
Benjamin Franze8b98922014-11-12 15:57:54 +000049import android.os.UserManager;
fredc0f420372012-04-12 00:02:00 -070050import android.provider.Settings;
51import android.util.Log;
Mike Lockwood726d4de2014-10-28 14:06:28 -070052
53import java.io.FileDescriptor;
54import java.io.PrintWriter;
55
Benjamin Franze8b98922014-11-12 15:57:54 +000056import java.util.HashMap;
57import java.util.Map;
Benjamin Franze8b98922014-11-12 15:57:54 +000058
Nitin Arora6ddbb5e2015-03-02 15:03:51 -080059import java.util.*;
fredc0f420372012-04-12 00:02:00 -070060class BluetoothManagerService extends IBluetoothManager.Stub {
61 private static final String TAG = "BluetoothManagerService";
62 private static final boolean DBG = true;
63
fredc0f420372012-04-12 00:02:00 -070064 private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN;
65 private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
fredc0f420372012-04-12 00:02:00 -070066 private static final String ACTION_SERVICE_STATE_CHANGED="com.android.bluetooth.btservice.action.STATE_CHANGED";
67 private static final String EXTRA_ACTION="action";
Zhihai Xud31c3222012-10-31 16:08:57 -070068 private static final String SECURE_SETTINGS_BLUETOOTH_ADDR_VALID="bluetooth_addr_valid";
fredc0f420372012-04-12 00:02:00 -070069 private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address";
70 private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name";
fredc0f420372012-04-12 00:02:00 -070071 private static final int TIMEOUT_BIND_MS = 3000; //Maximum msec to wait for a bind
72 private static final int TIMEOUT_SAVE_MS = 500; //Maximum msec to wait for a save
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +053073 //Maximum msec to wait for service restart
74 private static final int SERVICE_RESTART_TIME_MS = 200;
Zhihai Xudd9d17d2013-01-08 17:05:58 -080075 //Maximum msec to wait for restart due to error
76 private static final int ERROR_RESTART_TIME_MS = 3000;
Zhihai Xu40874a02012-10-08 17:57:03 -070077 //Maximum msec to delay MESSAGE_USER_SWITCHED
78 private static final int USER_SWITCHED_TIME_MS = 200;
Benjamin Franze8b98922014-11-12 15:57:54 +000079 // Delay for the addProxy function in msec
80 private static final int ADD_PROXY_DELAY_MS = 100;
fredc0f420372012-04-12 00:02:00 -070081
82 private static final int MESSAGE_ENABLE = 1;
83 private static final int MESSAGE_DISABLE = 2;
fredc649fe492012-04-19 01:07:18 -070084 private static final int MESSAGE_REGISTER_ADAPTER = 20;
85 private static final int MESSAGE_UNREGISTER_ADAPTER = 21;
86 private static final int MESSAGE_REGISTER_STATE_CHANGE_CALLBACK = 30;
87 private static final int MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK = 31;
88 private static final int MESSAGE_BLUETOOTH_SERVICE_CONNECTED = 40;
89 private static final int MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED = 41;
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +053090 private static final int MESSAGE_RESTART_BLUETOOTH_SERVICE = 42;
fredcbf072a72012-05-09 16:52:50 -070091 private static final int MESSAGE_BLUETOOTH_STATE_CHANGE=60;
fredc0f420372012-04-12 00:02:00 -070092 private static final int MESSAGE_TIMEOUT_BIND =100;
93 private static final int MESSAGE_TIMEOUT_UNBIND =101;
94 private static final int MESSAGE_GET_NAME_AND_ADDRESS=200;
95 private static final int MESSAGE_SAVE_NAME_AND_ADDRESS=201;
Zhihai Xu40874a02012-10-08 17:57:03 -070096 private static final int MESSAGE_USER_SWITCHED = 300;
Benjamin Franze8b98922014-11-12 15:57:54 +000097 private static final int MESSAGE_ADD_PROXY_DELAYED = 400;
98 private static final int MESSAGE_BIND_PROFILE_SERVICE = 401;
fredc0f420372012-04-12 00:02:00 -070099 private static final int MAX_SAVE_RETRIES=3;
Zhihai Xudd9d17d2013-01-08 17:05:58 -0800100 private static final int MAX_ERROR_RESTART_RETRIES=6;
101
Zhihai Xu401202b2012-12-03 11:36:21 -0800102 // Bluetooth persisted setting is off
103 private static final int BLUETOOTH_OFF=0;
104 // Bluetooth persisted setting is on
105 // and Airplane mode won't affect Bluetooth state at start up
106 private static final int BLUETOOTH_ON_BLUETOOTH=1;
107 // Bluetooth persisted setting is on
108 // but Airplane mode will affect Bluetooth state at start up
109 // and Airplane mode will have higher priority.
110 private static final int BLUETOOTH_ON_AIRPLANE=2;
fredc0f420372012-04-12 00:02:00 -0700111
Matthew Xieddf7e472013-03-01 18:41:02 -0800112 private static final int SERVICE_IBLUETOOTH = 1;
113 private static final int SERVICE_IBLUETOOTHGATT = 2;
114
fredc0f420372012-04-12 00:02:00 -0700115 private final Context mContext;
Nitin Arorad055adb2015-03-02 15:03:51 -0800116 private static int mBleAppCount = 0;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700117
118 // Locks are not provided for mName and mAddress.
119 // They are accessed in handler or broadcast receiver, same thread context.
fredc0f420372012-04-12 00:02:00 -0700120 private String mAddress;
121 private String mName;
Matthew Xie6fde3092012-07-11 17:10:07 -0700122 private final ContentResolver mContentResolver;
123 private final RemoteCallbackList<IBluetoothManagerCallback> mCallbacks;
124 private final RemoteCallbackList<IBluetoothStateChangeCallback> mStateChangeCallbacks;
fredc649fe492012-04-19 01:07:18 -0700125 private IBluetooth mBluetooth;
Matthew Xieddf7e472013-03-01 18:41:02 -0800126 private IBluetoothGatt mBluetoothGatt;
fredc649fe492012-04-19 01:07:18 -0700127 private boolean mBinding;
128 private boolean mUnbinding;
Zhihai Xu401202b2012-12-03 11:36:21 -0800129 // used inside handler thread
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700130 private boolean mQuietEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -0800131 // configuarion from external IBinder call which is used to
132 // synchronize with broadcast receiver.
133 private boolean mQuietEnableExternal;
134 // configuarion from external IBinder call which is used to
135 // synchronize with broadcast receiver.
136 private boolean mEnableExternal;
137 // used inside handler thread
Zhihai Xu40874a02012-10-08 17:57:03 -0700138 private boolean mEnable;
139 private int mState;
Zhihai Xu40874a02012-10-08 17:57:03 -0700140 private final BluetoothHandler mHandler;
Zhihai Xudd9d17d2013-01-08 17:05:58 -0800141 private int mErrorRecoveryRetryCounter;
Adrian Roosbd9a9a52014-08-18 15:31:57 +0200142 private final int mSystemUiUid;
fredc0f420372012-04-12 00:02:00 -0700143
Benjamin Franze8b98922014-11-12 15:57:54 +0000144 // Save a ProfileServiceConnections object for each of the bound
145 // bluetooth profile services
146 private final Map <Integer, ProfileServiceConnections> mProfileServices =
147 new HashMap <Integer, ProfileServiceConnections>();
148
fredc649fe492012-04-19 01:07:18 -0700149 private void registerForAirplaneMode(IntentFilter filter) {
150 final ContentResolver resolver = mContext.getContentResolver();
Christopher Tatec09cdce2012-09-10 16:50:14 -0700151 final String airplaneModeRadios = Settings.Global.getString(resolver,
152 Settings.Global.AIRPLANE_MODE_RADIOS);
153 final String toggleableRadios = Settings.Global.getString(resolver,
154 Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
fredc649fe492012-04-19 01:07:18 -0700155 boolean mIsAirplaneSensitive = airplaneModeRadios == null ? true :
Christopher Tatec09cdce2012-09-10 16:50:14 -0700156 airplaneModeRadios.contains(Settings.Global.RADIO_BLUETOOTH);
fredc649fe492012-04-19 01:07:18 -0700157 if (mIsAirplaneSensitive) {
158 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
159 }
160 }
161
fredcbf072a72012-05-09 16:52:50 -0700162 private final IBluetoothCallback mBluetoothCallback = new IBluetoothCallback.Stub() {
163 @Override
164 public void onBluetoothStateChange(int prevState, int newState) throws RemoteException {
165 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_STATE_CHANGE,prevState,newState);
166 mHandler.sendMessage(msg);
167 }
168 };
169
170 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
fredc0f420372012-04-12 00:02:00 -0700171 @Override
172 public void onReceive(Context context, Intent intent) {
173 String action = intent.getAction();
fredcbf072a72012-05-09 16:52:50 -0700174 if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action)) {
fredc0f420372012-04-12 00:02:00 -0700175 String newName = intent.getStringExtra(BluetoothAdapter.EXTRA_LOCAL_NAME);
Freda8c6df02012-07-11 10:25:23 -0700176 if (DBG) Log.d(TAG, "Bluetooth Adapter name changed to " + newName);
fredc0f420372012-04-12 00:02:00 -0700177 if (newName != null) {
178 storeNameAndAddress(newName, null);
179 }
fredc649fe492012-04-19 01:07:18 -0700180 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
Zhihai Xu401202b2012-12-03 11:36:21 -0800181 synchronized(mReceiver) {
182 if (isBluetoothPersistedStateOn()) {
183 if (isAirplaneModeOn()) {
184 persistBluetoothSetting(BLUETOOTH_ON_AIRPLANE);
185 } else {
186 persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);
187 }
188 }
Nitin Arorad055adb2015-03-02 15:03:51 -0800189
190 int st = BluetoothAdapter.STATE_OFF;
191 if (mBluetooth != null) {
192 try {
193 st = mBluetooth.getState();
194 } catch (RemoteException e) {
195 Log.e(TAG,"Unable to call getState", e);
196 }
197 }
198 Log.d(TAG, "state" + st);
199
Zhihai Xu401202b2012-12-03 11:36:21 -0800200 if (isAirplaneModeOn()) {
Nitin Arorad055adb2015-03-02 15:03:51 -0800201 // Clear registered LE apps to force shut-off
202 synchronized (this) {
203 mBleAppCount = 0;
204 }
205 if (st == BluetoothAdapter.STATE_BLE_ON) {
206 //if state is BLE_ON make sure you trigger disableBLE part
207 try {
208 if (mBluetooth != null) {
209 mBluetooth.onBrEdrDown();
210 mEnableExternal = false;
211 }
212 } catch(RemoteException e) {
213 Log.e(TAG,"Unable to call onBrEdrDown", e);
214 }
215 } else if (st == BluetoothAdapter.STATE_ON){
216 // disable without persisting the setting
217 Log.d(TAG, "Calling disable");
218 sendDisableMsg();
219 }
Zhihai Xu401202b2012-12-03 11:36:21 -0800220 } else if (mEnableExternal) {
221 // enable without persisting the setting
Nitin Arorad055adb2015-03-02 15:03:51 -0800222 Log.d(TAG, "Calling enable");
Zhihai Xu401202b2012-12-03 11:36:21 -0800223 sendEnableMsg(mQuietEnableExternal);
224 }
fredc649fe492012-04-19 01:07:18 -0700225 }
Zhihai Xu40874a02012-10-08 17:57:03 -0700226 } else if (Intent.ACTION_USER_SWITCHED.equals(action)) {
227 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_USER_SWITCHED,
228 intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0), 0));
Zhihai Xu401202b2012-12-03 11:36:21 -0800229 } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
230 synchronized(mReceiver) {
231 if (mEnableExternal && isBluetoothPersistedStateOnBluetooth()) {
232 //Enable
233 if (DBG) Log.d(TAG, "Auto-enabling Bluetooth.");
234 sendEnableMsg(mQuietEnableExternal);
235 }
236 }
fredc0f420372012-04-12 00:02:00 -0700237 }
238 }
239 };
240
241 BluetoothManagerService(Context context) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700242 mHandler = new BluetoothHandler(IoThread.get().getLooper());
Zhihai Xu40874a02012-10-08 17:57:03 -0700243
fredc0f420372012-04-12 00:02:00 -0700244 mContext = context;
245 mBluetooth = 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();
fredcd6883532012-04-25 17:46:13 -0700257 mCallbacks = new RemoteCallbackList<IBluetoothManagerCallback>();
258 mStateChangeCallbacks = new RemoteCallbackList<IBluetoothStateChangeCallback>();
Zhihai Xu401202b2012-12-03 11:36:21 -0800259 IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
Matthew Xie6fde3092012-07-11 17:10:07 -0700260 filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
Zhihai Xu40874a02012-10-08 17:57:03 -0700261 filter.addAction(Intent.ACTION_USER_SWITCHED);
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 {
272 sysUiUid = mContext.getPackageManager().getPackageUid("com.android.systemui",
273 UserHandle.USER_OWNER);
274 } catch (PackageManager.NameNotFoundException e) {
275 Log.wtf(TAG, "Unable to resolve SystemUI's UID.", e);
276 }
277 mSystemUiUid = sysUiUid;
fredc0f420372012-04-12 00:02:00 -0700278 }
279
fredc649fe492012-04-19 01:07:18 -0700280 /**
281 * Returns true if airplane mode is currently on
282 */
283 private final boolean isAirplaneModeOn() {
Christopher Tatec09cdce2012-09-10 16:50:14 -0700284 return Settings.Global.getInt(mContext.getContentResolver(),
285 Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
fredc649fe492012-04-19 01:07:18 -0700286 }
287
288 /**
289 * Returns true if the Bluetooth saved state is "on"
290 */
291 private final boolean isBluetoothPersistedStateOn() {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700292 return Settings.Global.getInt(mContentResolver,
Zhihai Xu401202b2012-12-03 11:36:21 -0800293 Settings.Global.BLUETOOTH_ON, 0) != BLUETOOTH_OFF;
294 }
295
296 /**
297 * Returns true if the Bluetooth saved state is BLUETOOTH_ON_BLUETOOTH
298 */
299 private final boolean isBluetoothPersistedStateOnBluetooth() {
300 return Settings.Global.getInt(mContentResolver,
301 Settings.Global.BLUETOOTH_ON, 0) == BLUETOOTH_ON_BLUETOOTH;
fredc649fe492012-04-19 01:07:18 -0700302 }
303
304 /**
305 * Save the Bluetooth on/off state
306 *
307 */
Zhihai Xu401202b2012-12-03 11:36:21 -0800308 private void persistBluetoothSetting(int value) {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700309 Settings.Global.putInt(mContext.getContentResolver(),
310 Settings.Global.BLUETOOTH_ON,
Zhihai Xu401202b2012-12-03 11:36:21 -0800311 value);
fredc649fe492012-04-19 01:07:18 -0700312 }
313
314 /**
315 * Returns true if the Bluetooth Adapter's name and address is
316 * locally cached
317 * @return
318 */
fredc0f420372012-04-12 00:02:00 -0700319 private boolean isNameAndAddressSet() {
320 return mName !=null && mAddress!= null && mName.length()>0 && mAddress.length()>0;
321 }
322
fredc649fe492012-04-19 01:07:18 -0700323 /**
324 * Retrieve the Bluetooth Adapter's name and address and save it in
325 * in the local cache
326 */
fredc0f420372012-04-12 00:02:00 -0700327 private void loadStoredNameAndAddress() {
328 if (DBG) Log.d(TAG, "Loading stored name and address");
Zhihai Xud31c3222012-10-31 16:08:57 -0700329 if (mContext.getResources().getBoolean
330 (com.android.internal.R.bool.config_bluetooth_address_validation) &&
331 Settings.Secure.getInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 0) == 0) {
332 // if the valid flag is not set, don't load the address and name
333 if (DBG) Log.d(TAG, "invalid bluetooth name and address stored");
334 return;
335 }
fredc0f420372012-04-12 00:02:00 -0700336 mName = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME);
337 mAddress = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS);
Zhihai Xud31c3222012-10-31 16:08:57 -0700338 if (DBG) Log.d(TAG, "Stored bluetooth Name=" + mName + ",Address=" + mAddress);
fredc0f420372012-04-12 00:02:00 -0700339 }
340
fredc649fe492012-04-19 01:07:18 -0700341 /**
342 * Save the Bluetooth name and address in the persistent store.
343 * Only non-null values will be saved.
344 * @param name
345 * @param address
346 */
fredc0f420372012-04-12 00:02:00 -0700347 private void storeNameAndAddress(String name, String address) {
348 if (name != null) {
349 Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME, name);
fredc0f420372012-04-12 00:02:00 -0700350 mName = name;
fredc649fe492012-04-19 01:07:18 -0700351 if (DBG) Log.d(TAG,"Stored Bluetooth name: " +
352 Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_NAME));
fredc0f420372012-04-12 00:02:00 -0700353 }
354
355 if (address != null) {
356 Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS, address);
fredc0f420372012-04-12 00:02:00 -0700357 mAddress=address;
fredc649fe492012-04-19 01:07:18 -0700358 if (DBG) Log.d(TAG,"Stored Bluetoothaddress: " +
359 Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_ADDRESS));
fredc0f420372012-04-12 00:02:00 -0700360 }
Zhihai Xud31c3222012-10-31 16:08:57 -0700361
362 if ((name != null) && (address != null)) {
363 Settings.Secure.putInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 1);
364 }
fredc0f420372012-04-12 00:02:00 -0700365 }
366
367 public IBluetooth registerAdapter(IBluetoothManagerCallback callback){
Natalie Silvanovich55db6462014-05-01 16:12:23 -0700368 if (callback == null) {
369 Log.w(TAG, "Callback is null in registerAdapter");
370 return null;
371 }
fredc0f420372012-04-12 00:02:00 -0700372 Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_ADAPTER);
373 msg.obj = callback;
374 mHandler.sendMessage(msg);
375 synchronized(mConnection) {
376 return mBluetooth;
377 }
378 }
379
380 public void unregisterAdapter(IBluetoothManagerCallback callback) {
Natalie Silvanovich55db6462014-05-01 16:12:23 -0700381 if (callback == null) {
382 Log.w(TAG, "Callback is null in unregisterAdapter");
383 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())) {
411 Log.w(TAG,"isEnabled(): not allowed for non-active and non system user");
Zhihai Xu40874a02012-10-08 17:57:03 -0700412 return false;
413 }
414
fredc0f420372012-04-12 00:02:00 -0700415 synchronized(mConnection) {
416 try {
417 return (mBluetooth != null && mBluetooth.isEnabled());
418 } catch (RemoteException e) {
419 Log.e(TAG, "isEnabled()", e);
420 }
421 }
422 return false;
423 }
424
Nitin Arorad055adb2015-03-02 15:03:51 -0800425 class ClientDeathRecipient implements IBinder.DeathRecipient {
426 public void binderDied() {
427 if (DBG) Log.d(TAG, "Binder is dead - unregister Ble App");
428 if (mBleAppCount > 0) --mBleAppCount;
429
430 if (mBleAppCount == 0) {
431 if (DBG) Log.d(TAG, "Disabling LE only mode after application crash");
432 try {
433 if (mBluetooth != null) {
434 mBluetooth.onBrEdrDown();
435 }
436 } catch(RemoteException e) {
437 Log.e(TAG,"Unable to call onBrEdrDown", e);
438 }
439 }
440 }
441 }
442
443 /** Internal death rec list */
444 Map<IBinder, ClientDeathRecipient> mBleApps = new HashMap<IBinder, ClientDeathRecipient>();
445
446 public int updateBleAppCount(IBinder token, boolean enable) {
447 if (enable) {
448 ClientDeathRecipient r = mBleApps.get(token);
449 if (r == null) {
450 ClientDeathRecipient deathRec = new ClientDeathRecipient();
451 try {
452 token.linkToDeath(deathRec, 0);
453 } catch (RemoteException ex) {
454 throw new IllegalArgumentException("Wake lock is already dead.");
455 }
456 mBleApps.put(token, deathRec);
457 synchronized (this) {
458 ++mBleAppCount;
459 }
460 if (DBG) Log.d(TAG, "Registered for death Notification");
461 }
462
463 } else {
464 ClientDeathRecipient r = mBleApps.get(token);
465 if (r != null) {
466 try {
467 token.linkToDeath(r, 0);
468 } catch (RemoteException ex) {
469 throw new IllegalArgumentException("Wake lock is already dead.");
470 }
471 mBleApps.remove(token);
472 synchronized (this) {
473 if (mBleAppCount > 0) --mBleAppCount;
474 }
475 if (DBG) Log.d(TAG, "Unregistered for death Notification");
476 }
477 }
478 if (DBG) Log.d(TAG, "Updated BleAppCount" + mBleAppCount);
479 if (mBleAppCount == 0 && mEnable) {
480 try {
481 if (mBluetooth != null && (mBluetooth.getState() != BluetoothAdapter.STATE_ON)) {
482 if (DBG) Log.d(TAG, "Reseting the mEnable flag for clean disable");
483 mEnable = false;
484 }
485 } catch (RemoteException e) {
486 Log.e(TAG, "getState()", e);
487 }
488 }
489 return mBleAppCount;
490 }
491
492 /** @hide*/
493 public boolean isBleAppPresent() {
494 if (DBG) Log.d(TAG, "isBleAppPresent() count: " + mBleAppCount);
495 return (mBleAppCount > 0);
496 }
497
498 /**
499 * Action taken when GattService is turned off
500 */
501 private void onBluetoothGattServiceUp() {
502 if (DBG) Log.d(TAG,"BluetoothGatt Service is Up");
503 try{
504 if (isBleAppPresent() == false && mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON) {
505 mBluetooth.onLeServiceUp();
506
507 // waive WRITE_SECURE_SETTINGS permission check
508 long callingIdentity = Binder.clearCallingIdentity();
509 persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);
510 Binder.restoreCallingIdentity(callingIdentity);
511 }
512 } catch(RemoteException e) {
513 Log.e(TAG,"Unable to call onServiceUp", e);
514 }
515 }
516
517 /**
518 * Inform BluetoothAdapter instances that BREDR part is down
519 * and turn off all service and stack if no LE app needs it
520 */
521 private void sendBrEdrDownCallback() {
522 if (DBG) Log.d(TAG,"Calling sendBrEdrDownCallback callbacks");
523 int n = mCallbacks.beginBroadcast();
524
525 if (isBleAppPresent() == false) {
526 try {
527 mBluetooth.onBrEdrDown();
528 } catch(RemoteException e) {
529 Log.e(TAG,"Unable to call onBrEdrDown", e);
530 }
531 }
532 else{//need to stay at BLE ON. disconnect all Gatt connections
533 try{
534 mBluetoothGatt.unregAll();//disconnectAll();
535 } catch(RemoteException e) {
536 Log.e(TAG,"Unable to disconn all", e);
537 }
538 }
539
540 Log.d(TAG,"Broadcasting onBrEdrDown() to " + n + " receivers.");
541 for (int i=0; i <n; i++) {
542 try {
543 mCallbacks.getBroadcastItem(i).onBrEdrDown();
544 } catch (RemoteException e) {
545 Log.e(TAG, "Unable to call sendBrEdrDownCallback() on callback #" + i, e);
546 }
547 }
548 mCallbacks.finishBroadcast();
549 }
550
551 /** @hide*/
fredc0f420372012-04-12 00:02:00 -0700552 public void getNameAndAddress() {
fredcf2458862012-04-16 15:18:27 -0700553 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700554 Log.d(TAG,"getNameAndAddress(): mBluetooth = " + mBluetooth +
555 " mBinding = " + mBinding);
fredcf2458862012-04-16 15:18:27 -0700556 }
fredc0f420372012-04-12 00:02:00 -0700557 Message msg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
558 mHandler.sendMessage(msg);
559 }
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700560 public boolean enableNoAutoConnect()
561 {
562 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
563 "Need BLUETOOTH ADMIN permission");
Zhihai Xu40874a02012-10-08 17:57:03 -0700564
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700565 if (DBG) {
566 Log.d(TAG,"enableNoAutoConnect(): mBluetooth =" + mBluetooth +
567 " mBinding = " + mBinding);
568 }
Martijn Coenen8385c5a2012-11-29 10:14:16 -0800569 int callingAppId = UserHandle.getAppId(Binder.getCallingUid());
570
571 if (callingAppId != Process.NFC_UID) {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700572 throw new SecurityException("no permission to enable Bluetooth quietly");
573 }
Martijn Coenen8385c5a2012-11-29 10:14:16 -0800574
Zhihai Xu401202b2012-12-03 11:36:21 -0800575 synchronized(mReceiver) {
576 mQuietEnableExternal = true;
577 mEnableExternal = true;
578 sendEnableMsg(true);
579 }
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700580 return true;
581
582 }
fredc0f420372012-04-12 00:02:00 -0700583 public boolean enable() {
Zhihai Xu6eb76522012-11-29 15:41:04 -0800584 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
585 (!checkIfCallerIsForegroundUser())) {
586 Log.w(TAG,"enable(): not allowed for non-active and non system user");
Zhihai Xu40874a02012-10-08 17:57:03 -0700587 return false;
fredcf2458862012-04-16 15:18:27 -0700588 }
589
Zhihai Xu401202b2012-12-03 11:36:21 -0800590 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
591 "Need BLUETOOTH ADMIN permission");
592 if (DBG) {
593 Log.d(TAG,"enable(): mBluetooth =" + mBluetooth +
594 " mBinding = " + mBinding);
595 }
596
597 synchronized(mReceiver) {
598 mQuietEnableExternal = false;
599 mEnableExternal = true;
600 // waive WRITE_SECURE_SETTINGS permission check
Zhihai Xu401202b2012-12-03 11:36:21 -0800601 sendEnableMsg(false);
602 }
Nitin Arorad055adb2015-03-02 15:03:51 -0800603 if (DBG) Log.d(TAG, "enable returning");
Zhihai Xu401202b2012-12-03 11:36:21 -0800604 return true;
fredc0f420372012-04-12 00:02:00 -0700605 }
606
607 public boolean disable(boolean persist) {
608 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
609 "Need BLUETOOTH ADMIN permissicacheNameAndAddresson");
Zhihai Xu40874a02012-10-08 17:57:03 -0700610
Zhihai Xu6eb76522012-11-29 15:41:04 -0800611 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
612 (!checkIfCallerIsForegroundUser())) {
613 Log.w(TAG,"disable(): not allowed for non-active and non system user");
Zhihai Xu40874a02012-10-08 17:57:03 -0700614 return false;
615 }
616
fredcf2458862012-04-16 15:18:27 -0700617 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700618 Log.d(TAG,"disable(): mBluetooth = " + mBluetooth +
619 " mBinding = " + mBinding);
620 }
fredcf2458862012-04-16 15:18:27 -0700621
Zhihai Xu401202b2012-12-03 11:36:21 -0800622 synchronized(mReceiver) {
623 if (persist) {
624 // waive WRITE_SECURE_SETTINGS permission check
625 long callingIdentity = Binder.clearCallingIdentity();
626 persistBluetoothSetting(BLUETOOTH_OFF);
627 Binder.restoreCallingIdentity(callingIdentity);
628 }
629 mEnableExternal = false;
630 sendDisableMsg();
631 }
fredc0f420372012-04-12 00:02:00 -0700632 return true;
633 }
634
fredc649fe492012-04-19 01:07:18 -0700635 public void unbindAndFinish() {
fredcf2458862012-04-16 15:18:27 -0700636 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700637 Log.d(TAG,"unbindAndFinish(): " + mBluetooth +
638 " mBinding = " + mBinding);
fredcf2458862012-04-16 15:18:27 -0700639 }
640
fredc0f420372012-04-12 00:02:00 -0700641 synchronized (mConnection) {
642 if (mUnbinding) return;
643 mUnbinding = true;
Zhihai Xu40874a02012-10-08 17:57:03 -0700644 if (mBluetooth != null) {
fredcbf072a72012-05-09 16:52:50 -0700645 if (!mConnection.isGetNameAddressOnly()) {
646 //Unregister callback object
647 try {
648 mBluetooth.unregisterCallback(mBluetoothCallback);
649 } catch (RemoteException re) {
Zhihai Xu40874a02012-10-08 17:57:03 -0700650 Log.e(TAG, "Unable to unregister BluetoothCallback",re);
fredcbf072a72012-05-09 16:52:50 -0700651 }
652 }
fredc0f420372012-04-12 00:02:00 -0700653 if (DBG) Log.d(TAG, "Sending unbind request.");
fredcd6883532012-04-25 17:46:13 -0700654 mBluetooth = null;
655 //Unbind
fredc0f420372012-04-12 00:02:00 -0700656 mContext.unbindService(mConnection);
fredcd6883532012-04-25 17:46:13 -0700657 mUnbinding = false;
Zhihai Xu40874a02012-10-08 17:57:03 -0700658 mBinding = false;
fredcf2458862012-04-16 15:18:27 -0700659 } else {
660 mUnbinding=false;
fredc0f420372012-04-12 00:02:00 -0700661 }
Nitin Arorad055adb2015-03-02 15:03:51 -0800662 mBluetoothGatt = null;
fredc0f420372012-04-12 00:02:00 -0700663 }
664 }
665
Matthew Xieddf7e472013-03-01 18:41:02 -0800666 public IBluetoothGatt getBluetoothGatt() {
667 // sync protection
668 return mBluetoothGatt;
669 }
670
Benjamin Franze8b98922014-11-12 15:57:54 +0000671 @Override
672 public boolean bindBluetoothProfileService(int bluetoothProfile,
673 IBluetoothProfileServiceConnection proxy) {
674 if (!mEnable) {
675 if (DBG) {
676 Log.d(TAG, "Trying to bind to profile: " + bluetoothProfile +
677 ", while Bluetooth was disabled");
678 }
679 return false;
680 }
681 synchronized (mProfileServices) {
682 ProfileServiceConnections psc = mProfileServices.get(new Integer(bluetoothProfile));
683 if (psc == null) {
684 if (DBG) {
685 Log.d(TAG, "Creating new ProfileServiceConnections object for"
686 + " profile: " + bluetoothProfile);
687 }
Benjamin Franz5b614592014-12-09 18:58:45 +0000688
689 if (bluetoothProfile != BluetoothProfile.HEADSET) return false;
690
691 Intent intent = new Intent(IBluetoothHeadset.class.getName());
Benjamin Franze8b98922014-11-12 15:57:54 +0000692 psc = new ProfileServiceConnections(intent);
Benjamin Franz5b614592014-12-09 18:58:45 +0000693 if (!psc.bindService()) return false;
694
Benjamin Franze8b98922014-11-12 15:57:54 +0000695 mProfileServices.put(new Integer(bluetoothProfile), psc);
Benjamin Franze8b98922014-11-12 15:57:54 +0000696 }
697 }
698
699 // Introducing a delay to give the client app time to prepare
700 Message addProxyMsg = mHandler.obtainMessage(MESSAGE_ADD_PROXY_DELAYED);
701 addProxyMsg.arg1 = bluetoothProfile;
702 addProxyMsg.obj = proxy;
703 mHandler.sendMessageDelayed(addProxyMsg, ADD_PROXY_DELAY_MS);
704 return true;
705 }
706
707 @Override
708 public void unbindBluetoothProfileService(int bluetoothProfile,
709 IBluetoothProfileServiceConnection proxy) {
710 synchronized (mProfileServices) {
711 ProfileServiceConnections psc = mProfileServices.get(new Integer(bluetoothProfile));
712 if (psc == null) {
713 return;
714 }
715 psc.removeProxy(proxy);
716 }
717 }
718
719 private void unbindAllBluetoothProfileServices() {
720 synchronized (mProfileServices) {
721 for (Integer i : mProfileServices.keySet()) {
722 ProfileServiceConnections psc = mProfileServices.get(i);
Benjamin Franz5b614592014-12-09 18:58:45 +0000723 try {
724 mContext.unbindService(psc);
725 } catch (IllegalArgumentException e) {
726 Log.e(TAG, "Unable to unbind service with intent: " + psc.mIntent, e);
727 }
Benjamin Franze8b98922014-11-12 15:57:54 +0000728 psc.removeAllProxies();
729 }
730 mProfileServices.clear();
731 }
732 }
733
734 /**
735 * This class manages the clients connected to a given ProfileService
736 * and maintains the connection with that service.
737 */
738 final private class ProfileServiceConnections implements ServiceConnection,
739 IBinder.DeathRecipient {
740 final RemoteCallbackList<IBluetoothProfileServiceConnection> mProxies =
741 new RemoteCallbackList <IBluetoothProfileServiceConnection>();
742 IBinder mService;
743 ComponentName mClassName;
744 Intent mIntent;
745
746 ProfileServiceConnections(Intent intent) {
747 mService = null;
748 mClassName = null;
749 mIntent = intent;
750 }
751
Benjamin Franz5b614592014-12-09 18:58:45 +0000752 private boolean bindService() {
753 if (mIntent != null && mService == null &&
754 doBind(mIntent, this, 0, UserHandle.CURRENT_OR_SELF)) {
Benjamin Franze8b98922014-11-12 15:57:54 +0000755 Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE);
756 msg.obj = this;
757 mHandler.sendMessageDelayed(msg, TIMEOUT_BIND_MS);
Benjamin Franz5b614592014-12-09 18:58:45 +0000758 return true;
Benjamin Franze8b98922014-11-12 15:57:54 +0000759 }
Benjamin Franz5b614592014-12-09 18:58:45 +0000760 Log.w(TAG, "Unable to bind with intent: " + mIntent);
761 return false;
Benjamin Franze8b98922014-11-12 15:57:54 +0000762 }
763
764 private void addProxy(IBluetoothProfileServiceConnection proxy) {
765 mProxies.register(proxy);
766 if (mService != null) {
767 try{
768 proxy.onServiceConnected(mClassName, mService);
769 } catch (RemoteException e) {
770 Log.e(TAG, "Unable to connect to proxy", e);
771 }
772 } else {
773 if (!mHandler.hasMessages(MESSAGE_BIND_PROFILE_SERVICE, this)) {
774 Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE);
775 msg.obj = this;
776 mHandler.sendMessage(msg);
777 }
778 }
779 }
780
781 private void removeProxy(IBluetoothProfileServiceConnection proxy) {
782 if (proxy != null) {
783 if (mProxies.unregister(proxy)) {
784 try {
785 proxy.onServiceDisconnected(mClassName);
786 } catch (RemoteException e) {
787 Log.e(TAG, "Unable to disconnect proxy", e);
788 }
789 }
790 } else {
791 Log.w(TAG, "Trying to remove a null proxy");
792 }
793 }
794
795 private void removeAllProxies() {
796 onServiceDisconnected(mClassName);
797 mProxies.kill();
798 }
799
800 @Override
801 public void onServiceConnected(ComponentName className, IBinder service) {
802 // remove timeout message
803 mHandler.removeMessages(MESSAGE_BIND_PROFILE_SERVICE, this);
804 mService = service;
805 mClassName = className;
806 try {
807 mService.linkToDeath(this, 0);
808 } catch (RemoteException e) {
809 Log.e(TAG, "Unable to linkToDeath", e);
810 }
811 int n = mProxies.beginBroadcast();
812 for (int i = 0; i < n; i++) {
813 try {
814 mProxies.getBroadcastItem(i).onServiceConnected(className, service);
815 } catch (RemoteException e) {
816 Log.e(TAG, "Unable to connect to proxy", e);
817 }
818 }
819 mProxies.finishBroadcast();
820 }
821
822 @Override
823 public void onServiceDisconnected(ComponentName className) {
824 if (mService == null) {
825 return;
826 }
827 mService.unlinkToDeath(this, 0);
828 mService = null;
829 mClassName = null;
830 int n = mProxies.beginBroadcast();
831 for (int i = 0; i < n; i++) {
832 try {
833 mProxies.getBroadcastItem(i).onServiceDisconnected(className);
834 } catch (RemoteException e) {
835 Log.e(TAG, "Unable to disconnect from proxy", e);
836 }
837 }
838 mProxies.finishBroadcast();
839 }
840
841 @Override
842 public void binderDied() {
843 if (DBG) {
844 Log.w(TAG, "Profile service for profile: " + mClassName
845 + " died.");
846 }
847 onServiceDisconnected(mClassName);
848 // Trigger rebind
849 Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE);
850 msg.obj = this;
851 mHandler.sendMessageDelayed(msg, TIMEOUT_BIND_MS);
852 }
853 }
854
fredcbf072a72012-05-09 16:52:50 -0700855 private void sendBluetoothStateCallback(boolean isUp) {
856 int n = mStateChangeCallbacks.beginBroadcast();
Freda8c6df02012-07-11 10:25:23 -0700857 if (DBG) Log.d(TAG,"Broadcasting onBluetoothStateChange("+isUp+") to " + n + " receivers.");
fredcbf072a72012-05-09 16:52:50 -0700858 for (int i=0; i <n;i++) {
859 try {
860 mStateChangeCallbacks.getBroadcastItem(i).onBluetoothStateChange(isUp);
861 } catch (RemoteException e) {
862 Log.e(TAG, "Unable to call onBluetoothStateChange() on callback #" + i , e);
863 }
864 }
865 mStateChangeCallbacks.finishBroadcast();
866 }
867
868 /**
Zhihai Xu40874a02012-10-08 17:57:03 -0700869 * Inform BluetoothAdapter instances that Adapter service is up
870 */
871 private void sendBluetoothServiceUpCallback() {
872 if (!mConnection.isGetNameAddressOnly()) {
873 if (DBG) Log.d(TAG,"Calling onBluetoothServiceUp callbacks");
874 int n = mCallbacks.beginBroadcast();
875 Log.d(TAG,"Broadcasting onBluetoothServiceUp() to " + n + " receivers.");
876 for (int i=0; i <n;i++) {
877 try {
878 mCallbacks.getBroadcastItem(i).onBluetoothServiceUp(mBluetooth);
879 } catch (RemoteException e) {
880 Log.e(TAG, "Unable to call onBluetoothServiceUp() on callback #" + i, e);
881 }
882 }
883 mCallbacks.finishBroadcast();
884 }
885 }
886 /**
fredcbf072a72012-05-09 16:52:50 -0700887 * Inform BluetoothAdapter instances that Adapter service is down
888 */
889 private void sendBluetoothServiceDownCallback() {
fredcd6883532012-04-25 17:46:13 -0700890 if (!mConnection.isGetNameAddressOnly()) {
891 if (DBG) Log.d(TAG,"Calling onBluetoothServiceDown callbacks");
892 int n = mCallbacks.beginBroadcast();
893 Log.d(TAG,"Broadcasting onBluetoothServiceDown() to " + n + " receivers.");
894 for (int i=0; i <n;i++) {
895 try {
896 mCallbacks.getBroadcastItem(i).onBluetoothServiceDown();
897 } catch (RemoteException e) {
898 Log.e(TAG, "Unable to call onBluetoothServiceDown() on callback #" + i, e);
899 }
900 }
901 mCallbacks.finishBroadcast();
902 }
903 }
fredc0f420372012-04-12 00:02:00 -0700904 public String getAddress() {
Matthew Xieaf5ddbf2012-12-04 10:47:43 -0800905 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
906 "Need BLUETOOTH permission");
Zhihai Xu40874a02012-10-08 17:57:03 -0700907
Zhihai Xu6eb76522012-11-29 15:41:04 -0800908 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
909 (!checkIfCallerIsForegroundUser())) {
910 Log.w(TAG,"getAddress(): not allowed for non-active and non system user");
911 return null;
Zhihai Xu40874a02012-10-08 17:57:03 -0700912 }
913
fredc116d1d462012-04-20 14:47:08 -0700914 synchronized(mConnection) {
915 if (mBluetooth != null) {
916 try {
917 return mBluetooth.getAddress();
918 } catch (RemoteException e) {
919 Log.e(TAG, "getAddress(): Unable to retrieve address remotely..Returning cached address",e);
920 }
921 }
922 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700923 // mAddress is accessed from outside.
924 // It is alright without a lock. Here, bluetooth is off, no other thread is
925 // changing mAddress
fredc0f420372012-04-12 00:02:00 -0700926 return mAddress;
927 }
fredc649fe492012-04-19 01:07:18 -0700928
fredc0f420372012-04-12 00:02:00 -0700929 public String getName() {
Matthew Xieaf5ddbf2012-12-04 10:47:43 -0800930 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
931 "Need BLUETOOTH permission");
Zhihai Xu40874a02012-10-08 17:57:03 -0700932
Zhihai Xu6eb76522012-11-29 15:41:04 -0800933 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
934 (!checkIfCallerIsForegroundUser())) {
935 Log.w(TAG,"getName(): not allowed for non-active and non system user");
936 return null;
Zhihai Xu40874a02012-10-08 17:57:03 -0700937 }
938
fredc116d1d462012-04-20 14:47:08 -0700939 synchronized(mConnection) {
940 if (mBluetooth != null) {
941 try {
942 return mBluetooth.getName();
943 } catch (RemoteException e) {
944 Log.e(TAG, "getName(): Unable to retrieve name remotely..Returning cached name",e);
945 }
946 }
947 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700948 // mName is accessed from outside.
949 // It alright without a lock. Here, bluetooth is off, no other thread is
950 // changing mName
fredc0f420372012-04-12 00:02:00 -0700951 return mName;
952 }
953
fredc0f420372012-04-12 00:02:00 -0700954 private class BluetoothServiceConnection implements ServiceConnection {
955
956 private boolean mGetNameAddressOnly;
957
958 public void setGetNameAddressOnly(boolean getOnly) {
959 mGetNameAddressOnly = getOnly;
960 }
961
962 public boolean isGetNameAddressOnly() {
963 return mGetNameAddressOnly;
964 }
965
966 public void onServiceConnected(ComponentName className, IBinder service) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800967 if (DBG) Log.d(TAG, "BluetoothServiceConnection: " + className.getClassName());
fredc0f420372012-04-12 00:02:00 -0700968 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED);
Matthew Xieddf7e472013-03-01 18:41:02 -0800969 // TBD if (className.getClassName().equals(IBluetooth.class.getName())) {
970 if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) {
971 msg.arg1 = SERVICE_IBLUETOOTH;
972 // } else if (className.getClassName().equals(IBluetoothGatt.class.getName())) {
973 } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) {
974 msg.arg1 = SERVICE_IBLUETOOTHGATT;
975 } else {
976 Log.e(TAG, "Unknown service connected: " + className.getClassName());
977 return;
978 }
fredc0f420372012-04-12 00:02:00 -0700979 msg.obj = service;
980 mHandler.sendMessage(msg);
981 }
982
983 public void onServiceDisconnected(ComponentName className) {
fredc0f420372012-04-12 00:02:00 -0700984 // Called if we unexpected disconnected.
Matthew Xieddf7e472013-03-01 18:41:02 -0800985 if (DBG) Log.d(TAG, "BluetoothServiceConnection, disconnected: " +
986 className.getClassName());
fredc0f420372012-04-12 00:02:00 -0700987 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED);
Matthew Xieddf7e472013-03-01 18:41:02 -0800988 if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) {
989 msg.arg1 = SERVICE_IBLUETOOTH;
990 } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) {
991 msg.arg1 = SERVICE_IBLUETOOTHGATT;
992 } else {
993 Log.e(TAG, "Unknown service disconnected: " + className.getClassName());
994 return;
995 }
fredc0f420372012-04-12 00:02:00 -0700996 mHandler.sendMessage(msg);
997 }
998 }
999
1000 private BluetoothServiceConnection mConnection = new BluetoothServiceConnection();
1001
Zhihai Xu40874a02012-10-08 17:57:03 -07001002 private class BluetoothHandler extends Handler {
1003 public BluetoothHandler(Looper looper) {
1004 super(looper);
1005 }
1006
fredc0f420372012-04-12 00:02:00 -07001007 @Override
1008 public void handleMessage(Message msg) {
1009 if (DBG) Log.d (TAG, "Message: " + msg.what);
fredc0f420372012-04-12 00:02:00 -07001010 switch (msg.what) {
1011 case MESSAGE_GET_NAME_AND_ADDRESS: {
fredc649fe492012-04-19 01:07:18 -07001012 if (DBG) Log.d(TAG,"MESSAGE_GET_NAME_AND_ADDRESS");
Matthew Xiecdce0b92012-07-12 19:06:15 -07001013 synchronized(mConnection) {
fredc0f420372012-04-12 00:02:00 -07001014 //Start bind request
Zhihai Xu40874a02012-10-08 17:57:03 -07001015 if ((mBluetooth == null) && (!mBinding)) {
fredc0f420372012-04-12 00:02:00 -07001016 if (DBG) Log.d(TAG, "Binding to service to get name and address");
1017 mConnection.setGetNameAddressOnly(true);
1018 //Start bind timeout and bind
1019 Message timeoutMsg = mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
1020 mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS);
1021 Intent i = new Intent(IBluetooth.class.getName());
Dianne Hackborn221ea892013-08-04 16:50:16 -07001022 if (!doBind(i, mConnection,
Dianne Hackbornce09f5a2014-10-10 15:03:13 -07001023 Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT,
1024 UserHandle.CURRENT)) {
fredc0f420372012-04-12 00:02:00 -07001025 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
Zhihai Xu40874a02012-10-08 17:57:03 -07001026 } else {
1027 mBinding = true;
fredc0f420372012-04-12 00:02:00 -07001028 }
1029 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001030 else {
1031 Message saveMsg= mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS);
Zhihai Xu40874a02012-10-08 17:57:03 -07001032 saveMsg.arg1 = 0;
1033 if (mBluetooth != null) {
1034 mHandler.sendMessage(saveMsg);
1035 } else {
1036 // if enable is also called to bind the service
1037 // wait for MESSAGE_BLUETOOTH_SERVICE_CONNECTED
1038 mHandler.sendMessageDelayed(saveMsg, TIMEOUT_SAVE_MS);
1039 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001040 }
fredc0f420372012-04-12 00:02:00 -07001041 }
fredc649fe492012-04-19 01:07:18 -07001042 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001043 }
fredc0f420372012-04-12 00:02:00 -07001044 case MESSAGE_SAVE_NAME_AND_ADDRESS: {
Zhihai Xud31c3222012-10-31 16:08:57 -07001045 boolean unbind = false;
fredc649fe492012-04-19 01:07:18 -07001046 if (DBG) Log.d(TAG,"MESSAGE_SAVE_NAME_AND_ADDRESS");
Matthew Xiecdce0b92012-07-12 19:06:15 -07001047 synchronized(mConnection) {
Zhihai Xud31c3222012-10-31 16:08:57 -07001048 if (!mEnable && mBluetooth != null) {
1049 try {
1050 mBluetooth.enable();
1051 } catch (RemoteException e) {
1052 Log.e(TAG,"Unable to call enable()",e);
1053 }
1054 }
1055 }
1056 if (mBluetooth != null) waitForOnOff(true, false);
1057 synchronized(mConnection) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001058 if (mBluetooth != null) {
1059 String name = null;
1060 String address = null;
1061 try {
1062 name = mBluetooth.getName();
1063 address = mBluetooth.getAddress();
1064 } catch (RemoteException re) {
1065 Log.e(TAG,"",re);
1066 }
fredc0f420372012-04-12 00:02:00 -07001067
Matthew Xiecdce0b92012-07-12 19:06:15 -07001068 if (name != null && address != null) {
1069 storeNameAndAddress(name,address);
Zhihai Xu40874a02012-10-08 17:57:03 -07001070 if (mConnection.isGetNameAddressOnly()) {
Zhihai Xud31c3222012-10-31 16:08:57 -07001071 unbind = true;
Zhihai Xu40874a02012-10-08 17:57:03 -07001072 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001073 } else {
1074 if (msg.arg1 < MAX_SAVE_RETRIES) {
1075 Message retryMsg = mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS);
1076 retryMsg.arg1= 1+msg.arg1;
1077 if (DBG) Log.d(TAG,"Retrying name/address remote retrieval and save.....Retry count =" + retryMsg.arg1);
1078 mHandler.sendMessageDelayed(retryMsg, TIMEOUT_SAVE_MS);
1079 } else {
1080 Log.w(TAG,"Maximum name/address remote retrieval retry exceeded");
Zhihai Xu40874a02012-10-08 17:57:03 -07001081 if (mConnection.isGetNameAddressOnly()) {
Zhihai Xud31c3222012-10-31 16:08:57 -07001082 unbind = true;
Zhihai Xu40874a02012-10-08 17:57:03 -07001083 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001084 }
fredc0f420372012-04-12 00:02:00 -07001085 }
Zhihai Xud31c3222012-10-31 16:08:57 -07001086 if (!mEnable) {
1087 try {
1088 mBluetooth.disable();
1089 } catch (RemoteException e) {
1090 Log.e(TAG,"Unable to call disable()",e);
1091 }
1092 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001093 } else {
1094 // rebind service by Request GET NAME AND ADDRESS
1095 // if service is unbinded by disable or
1096 // MESSAGE_BLUETOOTH_SERVICE_CONNECTED is not received
1097 Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
1098 mHandler.sendMessage(getMsg);
fredc0f420372012-04-12 00:02:00 -07001099 }
1100 }
Zhihai Xud31c3222012-10-31 16:08:57 -07001101 if (!mEnable && mBluetooth != null) waitForOnOff(false, true);
1102 if (unbind) {
1103 unbindAndFinish();
1104 }
fredc649fe492012-04-19 01:07:18 -07001105 break;
fredc649fe492012-04-19 01:07:18 -07001106 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001107 case MESSAGE_ENABLE:
fredcf2458862012-04-16 15:18:27 -07001108 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001109 Log.d(TAG, "MESSAGE_ENABLE: mBluetooth = " + mBluetooth);
fredc649fe492012-04-19 01:07:18 -07001110 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001111 mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
1112 mEnable = true;
Zhihai Xu401202b2012-12-03 11:36:21 -08001113 handleEnable(msg.arg1 == 1);
fredc649fe492012-04-19 01:07:18 -07001114 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001115
fredc0f420372012-04-12 00:02:00 -07001116 case MESSAGE_DISABLE:
Zhihai Xu40874a02012-10-08 17:57:03 -07001117 mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
1118 if (mEnable && mBluetooth != null) {
1119 waitForOnOff(true, false);
1120 mEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -08001121 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -07001122 waitForOnOff(false, false);
1123 } else {
1124 mEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -08001125 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -07001126 }
fredc0f420372012-04-12 00:02:00 -07001127 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001128
fredc0f420372012-04-12 00:02:00 -07001129 case MESSAGE_REGISTER_ADAPTER:
1130 {
1131 IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -07001132 boolean added = mCallbacks.register(callback);
1133 Log.d(TAG,"Added callback: " + (callback == null? "null": callback) +":" +added );
fredc0f420372012-04-12 00:02:00 -07001134 }
1135 break;
1136 case MESSAGE_UNREGISTER_ADAPTER:
1137 {
1138 IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -07001139 boolean removed = mCallbacks.unregister(callback);
1140 Log.d(TAG,"Removed callback: " + (callback == null? "null": callback) +":" + removed);
fredc0f420372012-04-12 00:02:00 -07001141 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001142 }
fredc0f420372012-04-12 00:02:00 -07001143 case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK:
1144 {
1145 IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj;
Matthew Xie9b693992013-10-10 11:21:40 -07001146 if (callback != null) {
1147 mStateChangeCallbacks.register(callback);
1148 }
fredc0f420372012-04-12 00:02:00 -07001149 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001150 }
fredc0f420372012-04-12 00:02:00 -07001151 case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK:
1152 {
1153 IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj;
Matthew Xie9b693992013-10-10 11:21:40 -07001154 if (callback != null) {
1155 mStateChangeCallbacks.unregister(callback);
1156 }
fredc0f420372012-04-12 00:02:00 -07001157 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001158 }
Benjamin Franze8b98922014-11-12 15:57:54 +00001159 case MESSAGE_ADD_PROXY_DELAYED:
1160 {
1161 ProfileServiceConnections psc = mProfileServices.get(
1162 new Integer(msg.arg1));
1163 if (psc == null) {
1164 break;
1165 }
1166 IBluetoothProfileServiceConnection proxy =
1167 (IBluetoothProfileServiceConnection) msg.obj;
1168 psc.addProxy(proxy);
1169 break;
1170 }
1171 case MESSAGE_BIND_PROFILE_SERVICE:
1172 {
1173 ProfileServiceConnections psc = (ProfileServiceConnections) msg.obj;
1174 removeMessages(MESSAGE_BIND_PROFILE_SERVICE, msg.obj);
1175 if (psc == null) {
1176 break;
1177 }
1178 psc.bindService();
1179 break;
1180 }
fredc0f420372012-04-12 00:02:00 -07001181 case MESSAGE_BLUETOOTH_SERVICE_CONNECTED:
1182 {
Matthew Xieddf7e472013-03-01 18:41:02 -08001183 if (DBG) Log.d(TAG,"MESSAGE_BLUETOOTH_SERVICE_CONNECTED: " + msg.arg1);
fredc0f420372012-04-12 00:02:00 -07001184
1185 IBinder service = (IBinder) msg.obj;
1186 synchronized(mConnection) {
Matthew Xieddf7e472013-03-01 18:41:02 -08001187 if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
1188 mBluetoothGatt = IBluetoothGatt.Stub.asInterface(service);
Nitin Arorad055adb2015-03-02 15:03:51 -08001189 onBluetoothGattServiceUp();
Matthew Xieddf7e472013-03-01 18:41:02 -08001190 break;
1191 } // else must be SERVICE_IBLUETOOTH
1192
1193 //Remove timeout
Zhihai Xuaf5971e2013-06-10 20:28:31 -07001194 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
Matthew Xieddf7e472013-03-01 18:41:02 -08001195
fredc0f420372012-04-12 00:02:00 -07001196 mBinding = false;
1197 mBluetooth = IBluetooth.Stub.asInterface(service);
fredc0f420372012-04-12 00:02:00 -07001198
Zhihai Xuaf5971e2013-06-10 20:28:31 -07001199 try {
1200 boolean enableHciSnoopLog = (Settings.Secure.getInt(mContentResolver,
1201 Settings.Secure.BLUETOOTH_HCI_LOG, 0) == 1);
1202 if (!mBluetooth.configHciSnoopLog(enableHciSnoopLog)) {
1203 Log.e(TAG,"IBluetooth.configHciSnoopLog return false");
1204 }
1205 } catch (RemoteException e) {
1206 Log.e(TAG,"Unable to call configHciSnoopLog", e);
1207 }
1208
Matthew Xiecdce0b92012-07-12 19:06:15 -07001209 if (mConnection.isGetNameAddressOnly()) {
1210 //Request GET NAME AND ADDRESS
1211 Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
1212 mHandler.sendMessage(getMsg);
Zhihai Xu40874a02012-10-08 17:57:03 -07001213 if (!mEnable) return;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001214 }
fredc0f420372012-04-12 00:02:00 -07001215
Zhihai Xu40874a02012-10-08 17:57:03 -07001216 mConnection.setGetNameAddressOnly(false);
Matthew Xiecdce0b92012-07-12 19:06:15 -07001217 //Register callback object
fredcbf072a72012-05-09 16:52:50 -07001218 try {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001219 mBluetooth.registerCallback(mBluetoothCallback);
1220 } catch (RemoteException re) {
1221 Log.e(TAG, "Unable to register BluetoothCallback",re);
fredcbf072a72012-05-09 16:52:50 -07001222 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001223 //Inform BluetoothAdapter instances that service is up
Zhihai Xu40874a02012-10-08 17:57:03 -07001224 sendBluetoothServiceUpCallback();
1225
Matthew Xiecdce0b92012-07-12 19:06:15 -07001226 //Do enable request
1227 try {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001228 if (mQuietEnable == false) {
1229 if(!mBluetooth.enable()) {
1230 Log.e(TAG,"IBluetooth.enable() returned false");
1231 }
1232 }
1233 else
1234 {
1235 if(!mBluetooth.enableNoAutoConnect()) {
1236 Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false");
1237 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001238 }
1239 } catch (RemoteException e) {
1240 Log.e(TAG,"Unable to call enable()",e);
1241 }
Freda8c6df02012-07-11 10:25:23 -07001242 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001243
1244 if (!mEnable) {
1245 waitForOnOff(true, false);
Zhihai Xu401202b2012-12-03 11:36:21 -08001246 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -07001247 waitForOnOff(false, false);
1248 }
fredc649fe492012-04-19 01:07:18 -07001249 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001250 }
fredc649fe492012-04-19 01:07:18 -07001251 case MESSAGE_TIMEOUT_BIND: {
1252 Log.e(TAG, "MESSAGE_TIMEOUT_BIND");
fredc0f420372012-04-12 00:02:00 -07001253 synchronized(mConnection) {
1254 mBinding = false;
1255 }
fredc649fe492012-04-19 01:07:18 -07001256 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001257 }
fredcbf072a72012-05-09 16:52:50 -07001258 case MESSAGE_BLUETOOTH_STATE_CHANGE:
fredc0f420372012-04-12 00:02:00 -07001259 {
fredcbf072a72012-05-09 16:52:50 -07001260 int prevState = msg.arg1;
1261 int newState = msg.arg2;
1262 if (DBG) Log.d(TAG, "MESSAGE_BLUETOOTH_STATE_CHANGE: prevState = " + prevState + ", newState=" + newState);
Zhihai Xu40874a02012-10-08 17:57:03 -07001263 mState = newState;
1264 bluetoothStateChangeHandler(prevState, newState);
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001265 // handle error state transition case from TURNING_ON to OFF
1266 // unbind and rebind bluetooth service and enable bluetooth
Nitin Arorad055adb2015-03-02 15:03:51 -08001267 if ((prevState == BluetoothAdapter.STATE_BLE_TURNING_ON) &&
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001268 (newState == BluetoothAdapter.STATE_OFF) &&
1269 (mBluetooth != null) && mEnable) {
1270 recoverBluetoothServiceFromError();
1271 }
Nitin Arorad055adb2015-03-02 15:03:51 -08001272 if ((prevState == BluetoothAdapter.STATE_TURNING_ON) &&
1273 (newState == BluetoothAdapter.STATE_BLE_ON) &&
1274 (mBluetooth != null) && mEnable) {
1275 recoverBluetoothServiceFromError();
1276 }
1277 if (newState == BluetoothAdapter.STATE_ON ||
1278 newState == BluetoothAdapter.STATE_BLE_ON) {
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001279 // bluetooth is working, reset the counter
1280 if (mErrorRecoveryRetryCounter != 0) {
1281 Log.w(TAG, "bluetooth is recovered from error");
1282 mErrorRecoveryRetryCounter = 0;
1283 }
1284 }
fredc649fe492012-04-19 01:07:18 -07001285 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001286 }
fredc0f420372012-04-12 00:02:00 -07001287 case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED:
1288 {
Matthew Xieddf7e472013-03-01 18:41:02 -08001289 Log.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: " + msg.arg1);
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301290 synchronized(mConnection) {
Matthew Xieddf7e472013-03-01 18:41:02 -08001291 if (msg.arg1 == SERVICE_IBLUETOOTH) {
1292 // if service is unbinded already, do nothing and return
1293 if (mBluetooth == null) break;
1294 mBluetooth = null;
1295 } else if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
1296 mBluetoothGatt = null;
1297 break;
1298 } else {
1299 Log.e(TAG, "Bad msg.arg1: " + msg.arg1);
1300 break;
1301 }
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301302 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001303
1304 if (mEnable) {
1305 mEnable = false;
1306 // Send a Bluetooth Restart message
1307 Message restartMsg = mHandler.obtainMessage(
1308 MESSAGE_RESTART_BLUETOOTH_SERVICE);
1309 mHandler.sendMessageDelayed(restartMsg,
1310 SERVICE_RESTART_TIME_MS);
1311 }
1312
1313 if (!mConnection.isGetNameAddressOnly()) {
1314 sendBluetoothServiceDownCallback();
1315
1316 // Send BT state broadcast to update
1317 // the BT icon correctly
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001318 if ((mState == BluetoothAdapter.STATE_TURNING_ON) ||
1319 (mState == BluetoothAdapter.STATE_ON)) {
1320 bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,
1321 BluetoothAdapter.STATE_TURNING_OFF);
1322 mState = BluetoothAdapter.STATE_TURNING_OFF;
1323 }
1324 if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
1325 bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
1326 BluetoothAdapter.STATE_OFF);
1327 }
1328
1329 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
Zhihai Xu40874a02012-10-08 17:57:03 -07001330 mState = BluetoothAdapter.STATE_OFF;
1331 }
fredc649fe492012-04-19 01:07:18 -07001332 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001333 }
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301334 case MESSAGE_RESTART_BLUETOOTH_SERVICE:
1335 {
1336 Log.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE:"
1337 +" Restart IBluetooth service");
1338 /* Enable without persisting the setting as
1339 it doesnt change when IBluetooth
1340 service restarts */
Zhihai Xu40874a02012-10-08 17:57:03 -07001341 mEnable = true;
Zhihai Xu401202b2012-12-03 11:36:21 -08001342 handleEnable(mQuietEnable);
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301343 break;
1344 }
1345
fredc0f420372012-04-12 00:02:00 -07001346 case MESSAGE_TIMEOUT_UNBIND:
1347 {
fredc649fe492012-04-19 01:07:18 -07001348 Log.e(TAG, "MESSAGE_TIMEOUT_UNBIND");
fredc0f420372012-04-12 00:02:00 -07001349 synchronized(mConnection) {
1350 mUnbinding = false;
1351 }
fredc649fe492012-04-19 01:07:18 -07001352 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001353 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001354
1355 case MESSAGE_USER_SWITCHED:
1356 {
1357 if (DBG) {
1358 Log.d(TAG, "MESSAGE_USER_SWITCHED");
1359 }
1360 mHandler.removeMessages(MESSAGE_USER_SWITCHED);
1361 /* disable and enable BT when detect a user switch */
1362 if (mEnable && mBluetooth != null) {
1363 synchronized (mConnection) {
1364 if (mBluetooth != null) {
1365 //Unregister callback object
1366 try {
1367 mBluetooth.unregisterCallback(mBluetoothCallback);
1368 } catch (RemoteException re) {
1369 Log.e(TAG, "Unable to unregister",re);
1370 }
1371 }
1372 }
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001373
1374 if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
1375 // MESSAGE_USER_SWITCHED happened right after MESSAGE_ENABLE
1376 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_OFF);
1377 mState = BluetoothAdapter.STATE_OFF;
1378 }
1379 if (mState == BluetoothAdapter.STATE_OFF) {
1380 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_TURNING_ON);
1381 mState = BluetoothAdapter.STATE_TURNING_ON;
1382 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001383
1384 waitForOnOff(true, false);
1385
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001386 if (mState == BluetoothAdapter.STATE_TURNING_ON) {
1387 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_ON);
1388 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001389
Benjamin Franze8b98922014-11-12 15:57:54 +00001390 unbindAllBluetoothProfileServices();
Zhihai Xu40874a02012-10-08 17:57:03 -07001391 // disable
Zhihai Xu401202b2012-12-03 11:36:21 -08001392 handleDisable();
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001393 // Pbap service need receive STATE_TURNING_OFF intent to close
1394 bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,
1395 BluetoothAdapter.STATE_TURNING_OFF);
Zhihai Xu40874a02012-10-08 17:57:03 -07001396
1397 waitForOnOff(false, true);
1398
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001399 bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
Zhihai Xu40874a02012-10-08 17:57:03 -07001400 BluetoothAdapter.STATE_OFF);
Zhihai Xu40874a02012-10-08 17:57:03 -07001401 sendBluetoothServiceDownCallback();
1402 synchronized (mConnection) {
1403 if (mBluetooth != null) {
1404 mBluetooth = null;
1405 //Unbind
1406 mContext.unbindService(mConnection);
1407 }
1408 }
1409 SystemClock.sleep(100);
1410
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001411 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
1412 mState = BluetoothAdapter.STATE_OFF;
Zhihai Xu40874a02012-10-08 17:57:03 -07001413 // enable
Zhihai Xu401202b2012-12-03 11:36:21 -08001414 handleEnable(mQuietEnable);
John Spurlock8a985d22014-02-25 09:40:05 -05001415 } else if (mBinding || mBluetooth != null) {
Zhihai Xu40874a02012-10-08 17:57:03 -07001416 Message userMsg = mHandler.obtainMessage(MESSAGE_USER_SWITCHED);
1417 userMsg.arg2 = 1 + msg.arg2;
1418 // if user is switched when service is being binding
1419 // delay sending MESSAGE_USER_SWITCHED
1420 mHandler.sendMessageDelayed(userMsg, USER_SWITCHED_TIME_MS);
1421 if (DBG) {
1422 Log.d(TAG, "delay MESSAGE_USER_SWITCHED " + userMsg.arg2);
1423 }
John Spurlock8a985d22014-02-25 09:40:05 -05001424 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001425 break;
1426 }
fredc0f420372012-04-12 00:02:00 -07001427 }
1428 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001429 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001430
Zhihai Xu401202b2012-12-03 11:36:21 -08001431 private void handleEnable(boolean quietMode) {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001432 mQuietEnable = quietMode;
1433
Matthew Xiecdce0b92012-07-12 19:06:15 -07001434 synchronized(mConnection) {
Zhihai Xu40874a02012-10-08 17:57:03 -07001435 if ((mBluetooth == null) && (!mBinding)) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001436 //Start bind timeout and bind
1437 Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
1438 mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS);
1439 mConnection.setGetNameAddressOnly(false);
1440 Intent i = new Intent(IBluetooth.class.getName());
Dianne Hackbornce09f5a2014-10-10 15:03:13 -07001441 if (!doBind(i, mConnection,Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT,
1442 UserHandle.CURRENT)) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001443 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
Zhihai Xu40874a02012-10-08 17:57:03 -07001444 } else {
1445 mBinding = true;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001446 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001447 } else if (mBluetooth != null) {
1448 if (mConnection.isGetNameAddressOnly()) {
1449 // if GetNameAddressOnly is set, we can clear this flag,
1450 // so the service won't be unbind
1451 // after name and address are saved
1452 mConnection.setGetNameAddressOnly(false);
1453 //Register callback object
1454 try {
1455 mBluetooth.registerCallback(mBluetoothCallback);
1456 } catch (RemoteException re) {
1457 Log.e(TAG, "Unable to register BluetoothCallback",re);
1458 }
1459 //Inform BluetoothAdapter instances that service is up
1460 sendBluetoothServiceUpCallback();
1461 }
1462
Matthew Xiecdce0b92012-07-12 19:06:15 -07001463 //Enable bluetooth
1464 try {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001465 if (!mQuietEnable) {
1466 if(!mBluetooth.enable()) {
1467 Log.e(TAG,"IBluetooth.enable() returned false");
1468 }
1469 }
1470 else {
1471 if(!mBluetooth.enableNoAutoConnect()) {
1472 Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false");
1473 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001474 }
1475 } catch (RemoteException e) {
1476 Log.e(TAG,"Unable to call enable()",e);
1477 }
1478 }
1479 }
1480 }
1481
Dianne Hackborn221ea892013-08-04 16:50:16 -07001482 boolean doBind(Intent intent, ServiceConnection conn, int flags, UserHandle user) {
1483 ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
1484 intent.setComponent(comp);
1485 if (comp == null || !mContext.bindServiceAsUser(intent, conn, flags, user)) {
1486 Log.e(TAG, "Fail to bind to: " + intent);
1487 return false;
1488 }
1489 return true;
1490 }
1491
Zhihai Xu401202b2012-12-03 11:36:21 -08001492 private void handleDisable() {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001493 synchronized(mConnection) {
Zhihai Xu40874a02012-10-08 17:57:03 -07001494 // don't need to disable if GetNameAddressOnly is set,
1495 // service will be unbinded after Name and Address are saved
1496 if ((mBluetooth != null) && (!mConnection.isGetNameAddressOnly())) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001497 if (DBG) Log.d(TAG,"Sending off request.");
1498
1499 try {
1500 if(!mBluetooth.disable()) {
1501 Log.e(TAG,"IBluetooth.disable() returned false");
1502 }
1503 } catch (RemoteException e) {
1504 Log.e(TAG,"Unable to call disable()",e);
1505 }
1506 }
1507 }
1508 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001509
1510 private boolean checkIfCallerIsForegroundUser() {
1511 int foregroundUser;
1512 int callingUser = UserHandle.getCallingUserId();
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001513 int callingUid = Binder.getCallingUid();
Zhihai Xu40874a02012-10-08 17:57:03 -07001514 long callingIdentity = Binder.clearCallingIdentity();
Benjamin Franze8b98922014-11-12 15:57:54 +00001515 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
1516 UserInfo ui = um.getProfileParent(callingUser);
1517 int parentUser = (ui != null) ? ui.id : UserHandle.USER_NULL;
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001518 int callingAppId = UserHandle.getAppId(callingUid);
Zhihai Xu40874a02012-10-08 17:57:03 -07001519 boolean valid = false;
1520 try {
1521 foregroundUser = ActivityManager.getCurrentUser();
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001522 valid = (callingUser == foregroundUser) ||
Benjamin Franze8b98922014-11-12 15:57:54 +00001523 parentUser == foregroundUser ||
Adrian Roosbd9a9a52014-08-18 15:31:57 +02001524 callingAppId == Process.NFC_UID ||
1525 callingAppId == mSystemUiUid;
Zhihai Xu40874a02012-10-08 17:57:03 -07001526 if (DBG) {
1527 Log.d(TAG, "checkIfCallerIsForegroundUser: valid=" + valid
1528 + " callingUser=" + callingUser
Benjamin Franze8b98922014-11-12 15:57:54 +00001529 + " parentUser=" + parentUser
Zhihai Xu40874a02012-10-08 17:57:03 -07001530 + " foregroundUser=" + foregroundUser);
1531 }
1532 } finally {
1533 Binder.restoreCallingIdentity(callingIdentity);
1534 }
1535 return valid;
1536 }
1537
Nitin Arorad055adb2015-03-02 15:03:51 -08001538 private void sendBleStateChanged(int prevState, int newState) {
1539 if (DBG) Log.d(TAG,"BLE State Change Intent: " + prevState + " -> " + newState);
1540 // Send broadcast message to everyone else
1541 Intent intent = new Intent(BluetoothAdapter.ACTION_BLE_STATE_CHANGED);
1542 intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState);
1543 intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState);
1544 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1545 mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM);
1546 }
1547
Zhihai Xu40874a02012-10-08 17:57:03 -07001548 private void bluetoothStateChangeHandler(int prevState, int newState) {
Nitin Arorad055adb2015-03-02 15:03:51 -08001549 boolean isStandardBroadcast = true;
Zhihai Xu40874a02012-10-08 17:57:03 -07001550 if (prevState != newState) {
1551 //Notify all proxy objects first of adapter state change
Nitin Arorad055adb2015-03-02 15:03:51 -08001552 if (newState == BluetoothAdapter.STATE_BLE_ON
1553 || newState == BluetoothAdapter.STATE_OFF) {
1554 boolean intermediate_off = (prevState == BluetoothAdapter.STATE_TURNING_OFF
1555 && newState == BluetoothAdapter.STATE_BLE_ON);
Zhihai Xu40874a02012-10-08 17:57:03 -07001556
Nitin Arorad055adb2015-03-02 15:03:51 -08001557 if (newState == BluetoothAdapter.STATE_OFF) {
1558 // If Bluetooth is off, send service down event to proxy objects, and unbind
1559 if (DBG) Log.d(TAG, "Bluetooth is complete turn off");
1560 if (canUnbindBluetoothService()) {
1561 if (DBG) Log.d(TAG, "Good to unbind!");
Matthew Xieddf7e472013-03-01 18:41:02 -08001562 sendBluetoothServiceDownCallback();
1563 unbindAndFinish();
Nitin Arorad055adb2015-03-02 15:03:51 -08001564 sendBleStateChanged(prevState, newState);
1565 // Don't broadcast as it has already been broadcast before
1566 isStandardBroadcast = false;
Matthew Xieddf7e472013-03-01 18:41:02 -08001567 }
Nitin Arorad055adb2015-03-02 15:03:51 -08001568
1569 } else if (!intermediate_off) {
1570 // connect to GattService
1571 if (DBG) Log.d(TAG, "Bluetooth is in LE only mode");
1572 if (mBluetoothGatt != null) {
1573 if (DBG) Log.d(TAG, "Calling BluetoothGattServiceUp");
1574 onBluetoothGattServiceUp();
1575 } else {
1576 if (DBG) Log.d(TAG, "Binding Bluetooth GATT service");
1577 if (mContext.getPackageManager().hasSystemFeature(
1578 PackageManager.FEATURE_BLUETOOTH_LE)) {
1579 Intent i = new Intent(IBluetoothGatt.class.getName());
1580 doBind(i, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, UserHandle.CURRENT);
1581 }
1582 }
1583 sendBleStateChanged(prevState, newState);
1584 //Don't broadcase this as std intent
1585 isStandardBroadcast = false;
1586
1587 } else if (intermediate_off){
1588 if (DBG) Log.d(TAG, "Intermediate off, back to LE only mode");
1589 // For LE only mode, broadcast as is
1590 sendBleStateChanged(prevState, newState);
1591 sendBluetoothStateCallback(false); // BT is OFF for general users
1592 // Broadcast as STATE_OFF
1593 newState = BluetoothAdapter.STATE_OFF;
1594 sendBrEdrDownCallback();
Zhihai Xu40874a02012-10-08 17:57:03 -07001595 }
Nitin Arorad055adb2015-03-02 15:03:51 -08001596 } else if (newState == BluetoothAdapter.STATE_ON) {
1597 boolean isUp = (newState==BluetoothAdapter.STATE_ON);
1598 sendBluetoothStateCallback(isUp);
1599 sendBleStateChanged(prevState, newState);
1600
1601 } else if (newState == BluetoothAdapter.STATE_BLE_TURNING_ON
1602 || newState == BluetoothAdapter.STATE_BLE_TURNING_OFF ) {
1603 sendBleStateChanged(prevState, newState);
1604 isStandardBroadcast = false;
1605
1606 } else if (newState == BluetoothAdapter.STATE_TURNING_ON
1607 || newState == BluetoothAdapter.STATE_TURNING_OFF) {
1608 sendBleStateChanged(prevState, newState);
Zhihai Xu40874a02012-10-08 17:57:03 -07001609 }
1610
Nitin Arorad055adb2015-03-02 15:03:51 -08001611 if (isStandardBroadcast) {
1612 if (prevState == BluetoothAdapter.STATE_BLE_ON) {
1613 // Show prevState of BLE_ON as OFF to standard users
1614 prevState = BluetoothAdapter.STATE_OFF;
1615 }
1616 Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
1617 intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState);
1618 intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState);
1619 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1620 mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM);
1621 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001622 }
1623 }
1624
1625 /**
1626 * if on is true, wait for state become ON
1627 * if off is true, wait for state become OFF
1628 * if both on and off are false, wait for state not ON
1629 */
1630 private boolean waitForOnOff(boolean on, boolean off) {
1631 int i = 0;
1632 while (i < 10) {
1633 synchronized(mConnection) {
1634 try {
1635 if (mBluetooth == null) break;
1636 if (on) {
1637 if (mBluetooth.getState() == BluetoothAdapter.STATE_ON) return true;
1638 } else if (off) {
1639 if (mBluetooth.getState() == BluetoothAdapter.STATE_OFF) return true;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001640 } else {
Zhihai Xu40874a02012-10-08 17:57:03 -07001641 if (mBluetooth.getState() != BluetoothAdapter.STATE_ON) return true;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001642 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001643 } catch (RemoteException e) {
1644 Log.e(TAG, "getState()", e);
1645 break;
1646 }
1647 }
1648 if (on || off) {
1649 SystemClock.sleep(300);
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001650 } else {
Zhihai Xu40874a02012-10-08 17:57:03 -07001651 SystemClock.sleep(50);
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001652 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001653 i++;
1654 }
1655 Log.e(TAG,"waitForOnOff time out");
1656 return false;
1657 }
Zhihai Xu681ae7f2012-11-12 15:14:18 -08001658
Zhihai Xu401202b2012-12-03 11:36:21 -08001659 private void sendDisableMsg() {
1660 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISABLE));
1661 }
1662
1663 private void sendEnableMsg(boolean quietMode) {
1664 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_ENABLE,
1665 quietMode ? 1 : 0, 0));
1666 }
1667
Zhihai Xu681ae7f2012-11-12 15:14:18 -08001668 private boolean canUnbindBluetoothService() {
1669 synchronized(mConnection) {
1670 //Only unbind with mEnable flag not set
1671 //For race condition: disable and enable back-to-back
1672 //Avoid unbind right after enable due to callback from disable
1673 //Only unbind with Bluetooth at OFF state
1674 //Only unbind without any MESSAGE_BLUETOOTH_STATE_CHANGE message
1675 try {
1676 if (mEnable || (mBluetooth == null)) return false;
1677 if (mHandler.hasMessages(MESSAGE_BLUETOOTH_STATE_CHANGE)) return false;
1678 return (mBluetooth.getState() == BluetoothAdapter.STATE_OFF);
1679 } catch (RemoteException e) {
1680 Log.e(TAG, "getState()", e);
1681 }
1682 }
1683 return false;
1684 }
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001685
1686 private void recoverBluetoothServiceFromError() {
1687 Log.e(TAG,"recoverBluetoothServiceFromError");
1688 synchronized (mConnection) {
1689 if (mBluetooth != null) {
1690 //Unregister callback object
1691 try {
1692 mBluetooth.unregisterCallback(mBluetoothCallback);
1693 } catch (RemoteException re) {
1694 Log.e(TAG, "Unable to unregister",re);
1695 }
1696 }
1697 }
1698
1699 SystemClock.sleep(500);
1700
1701 // disable
1702 handleDisable();
1703
1704 waitForOnOff(false, true);
1705
1706 sendBluetoothServiceDownCallback();
1707 synchronized (mConnection) {
1708 if (mBluetooth != null) {
1709 mBluetooth = null;
1710 //Unbind
1711 mContext.unbindService(mConnection);
1712 }
1713 }
1714
1715 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
1716 mState = BluetoothAdapter.STATE_OFF;
1717
1718 mEnable = false;
1719
1720 if (mErrorRecoveryRetryCounter++ < MAX_ERROR_RESTART_RETRIES) {
1721 // Send a Bluetooth Restart message to reenable bluetooth
1722 Message restartMsg = mHandler.obtainMessage(
1723 MESSAGE_RESTART_BLUETOOTH_SERVICE);
1724 mHandler.sendMessageDelayed(restartMsg, ERROR_RESTART_TIME_MS);
1725 } else {
1726 // todo: notify user to power down and power up phone to make bluetooth work.
1727 }
1728 }
Mike Lockwood726d4de2014-10-28 14:06:28 -07001729
1730 @Override
1731 public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Mike Lockwood75b52bb2014-12-18 14:16:36 -08001732 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
1733
Mike Lockwood726d4de2014-10-28 14:06:28 -07001734 writer.println("enabled: " + mEnable);
1735 writer.println("state: " + mState);
1736 writer.println("address: " + mAddress);
1737 writer.println("name: " + mName);
1738 if (mBluetooth == null) {
1739 writer.println("Bluetooth Service not connected");
1740 } else {
1741 try {
1742 writer.println(mBluetooth.dump());
1743 } catch (RemoteException re) {
1744 writer.println("RemoteException while calling Bluetooth Service");
1745 }
1746 }
1747 }
fredc0f420372012-04-12 00:02:00 -07001748}