blob: f0164cbd0139071728c8138fbe447540f9234ee5 [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;
21import android.bluetooth.IBluetooth;
Matthew Xieddf7e472013-03-01 18:41:02 -080022import android.bluetooth.IBluetoothGatt;
fredcbf072a72012-05-09 16:52:50 -070023import android.bluetooth.IBluetoothCallback;
fredc0f420372012-04-12 00:02:00 -070024import android.bluetooth.IBluetoothManager;
25import android.bluetooth.IBluetoothManagerCallback;
26import android.bluetooth.IBluetoothStateChangeCallback;
fredc0f420372012-04-12 00:02:00 -070027import android.content.BroadcastReceiver;
28import android.content.ComponentName;
29import android.content.ContentResolver;
30import android.content.Context;
31import android.content.Intent;
32import android.content.IntentFilter;
33import android.content.ServiceConnection;
Matthew Xie32ab77b2013-05-08 19:26:57 -070034import android.content.pm.PackageManager;
Zhihai Xu40874a02012-10-08 17:57:03 -070035import android.os.Binder;
fredc0f420372012-04-12 00:02:00 -070036import android.os.Handler;
fredc0f420372012-04-12 00:02:00 -070037import android.os.IBinder;
Zhihai Xu40874a02012-10-08 17:57:03 -070038import android.os.Looper;
fredc0f420372012-04-12 00:02:00 -070039import android.os.Message;
Zhihai Xu40874a02012-10-08 17:57:03 -070040import android.os.Process;
fredcd6883532012-04-25 17:46:13 -070041import android.os.RemoteCallbackList;
fredc0f420372012-04-12 00:02:00 -070042import android.os.RemoteException;
Zhihai Xu40874a02012-10-08 17:57:03 -070043import android.os.SystemClock;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070044import android.os.UserHandle;
fredc0f420372012-04-12 00:02:00 -070045import android.provider.Settings;
46import android.util.Log;
fredc0f420372012-04-12 00:02:00 -070047class BluetoothManagerService extends IBluetoothManager.Stub {
48 private static final String TAG = "BluetoothManagerService";
49 private static final boolean DBG = true;
50
fredc0f420372012-04-12 00:02:00 -070051 private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN;
52 private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
fredc0f420372012-04-12 00:02:00 -070053 private static final String ACTION_SERVICE_STATE_CHANGED="com.android.bluetooth.btservice.action.STATE_CHANGED";
54 private static final String EXTRA_ACTION="action";
Zhihai Xud31c3222012-10-31 16:08:57 -070055 private static final String SECURE_SETTINGS_BLUETOOTH_ADDR_VALID="bluetooth_addr_valid";
fredc0f420372012-04-12 00:02:00 -070056 private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address";
57 private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name";
fredc0f420372012-04-12 00:02:00 -070058 private static final int TIMEOUT_BIND_MS = 3000; //Maximum msec to wait for a bind
59 private static final int TIMEOUT_SAVE_MS = 500; //Maximum msec to wait for a save
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +053060 //Maximum msec to wait for service restart
61 private static final int SERVICE_RESTART_TIME_MS = 200;
Zhihai Xudd9d17d2013-01-08 17:05:58 -080062 //Maximum msec to wait for restart due to error
63 private static final int ERROR_RESTART_TIME_MS = 3000;
Zhihai Xu40874a02012-10-08 17:57:03 -070064 //Maximum msec to delay MESSAGE_USER_SWITCHED
65 private static final int USER_SWITCHED_TIME_MS = 200;
fredc0f420372012-04-12 00:02:00 -070066
67 private static final int MESSAGE_ENABLE = 1;
68 private static final int MESSAGE_DISABLE = 2;
fredc649fe492012-04-19 01:07:18 -070069 private static final int MESSAGE_REGISTER_ADAPTER = 20;
70 private static final int MESSAGE_UNREGISTER_ADAPTER = 21;
71 private static final int MESSAGE_REGISTER_STATE_CHANGE_CALLBACK = 30;
72 private static final int MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK = 31;
73 private static final int MESSAGE_BLUETOOTH_SERVICE_CONNECTED = 40;
74 private static final int MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED = 41;
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +053075 private static final int MESSAGE_RESTART_BLUETOOTH_SERVICE = 42;
fredcbf072a72012-05-09 16:52:50 -070076 private static final int MESSAGE_BLUETOOTH_STATE_CHANGE=60;
fredc0f420372012-04-12 00:02:00 -070077 private static final int MESSAGE_TIMEOUT_BIND =100;
78 private static final int MESSAGE_TIMEOUT_UNBIND =101;
79 private static final int MESSAGE_GET_NAME_AND_ADDRESS=200;
80 private static final int MESSAGE_SAVE_NAME_AND_ADDRESS=201;
Zhihai Xu40874a02012-10-08 17:57:03 -070081 private static final int MESSAGE_USER_SWITCHED = 300;
fredc0f420372012-04-12 00:02:00 -070082 private static final int MAX_SAVE_RETRIES=3;
Zhihai Xudd9d17d2013-01-08 17:05:58 -080083 private static final int MAX_ERROR_RESTART_RETRIES=6;
84
Zhihai Xu401202b2012-12-03 11:36:21 -080085 // Bluetooth persisted setting is off
86 private static final int BLUETOOTH_OFF=0;
87 // Bluetooth persisted setting is on
88 // and Airplane mode won't affect Bluetooth state at start up
89 private static final int BLUETOOTH_ON_BLUETOOTH=1;
90 // Bluetooth persisted setting is on
91 // but Airplane mode will affect Bluetooth state at start up
92 // and Airplane mode will have higher priority.
93 private static final int BLUETOOTH_ON_AIRPLANE=2;
fredc0f420372012-04-12 00:02:00 -070094
Matthew Xieddf7e472013-03-01 18:41:02 -080095 private static final int SERVICE_IBLUETOOTH = 1;
96 private static final int SERVICE_IBLUETOOTHGATT = 2;
97
fredc0f420372012-04-12 00:02:00 -070098 private final Context mContext;
Matthew Xiecdce0b92012-07-12 19:06:15 -070099
100 // Locks are not provided for mName and mAddress.
101 // They are accessed in handler or broadcast receiver, same thread context.
fredc0f420372012-04-12 00:02:00 -0700102 private String mAddress;
103 private String mName;
Matthew Xie6fde3092012-07-11 17:10:07 -0700104 private final ContentResolver mContentResolver;
105 private final RemoteCallbackList<IBluetoothManagerCallback> mCallbacks;
106 private final RemoteCallbackList<IBluetoothStateChangeCallback> mStateChangeCallbacks;
fredc649fe492012-04-19 01:07:18 -0700107 private IBluetooth mBluetooth;
Matthew Xieddf7e472013-03-01 18:41:02 -0800108 private IBluetoothGatt mBluetoothGatt;
fredc649fe492012-04-19 01:07:18 -0700109 private boolean mBinding;
110 private boolean mUnbinding;
Zhihai Xu401202b2012-12-03 11:36:21 -0800111 // used inside handler thread
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700112 private boolean mQuietEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -0800113 // configuarion from external IBinder call which is used to
114 // synchronize with broadcast receiver.
115 private boolean mQuietEnableExternal;
116 // configuarion from external IBinder call which is used to
117 // synchronize with broadcast receiver.
118 private boolean mEnableExternal;
119 // used inside handler thread
Zhihai Xu40874a02012-10-08 17:57:03 -0700120 private boolean mEnable;
121 private int mState;
Zhihai Xu40874a02012-10-08 17:57:03 -0700122 private final BluetoothHandler mHandler;
Zhihai Xudd9d17d2013-01-08 17:05:58 -0800123 private int mErrorRecoveryRetryCounter;
fredc0f420372012-04-12 00:02:00 -0700124
fredc649fe492012-04-19 01:07:18 -0700125 private void registerForAirplaneMode(IntentFilter filter) {
126 final ContentResolver resolver = mContext.getContentResolver();
Christopher Tatec09cdce2012-09-10 16:50:14 -0700127 final String airplaneModeRadios = Settings.Global.getString(resolver,
128 Settings.Global.AIRPLANE_MODE_RADIOS);
129 final String toggleableRadios = Settings.Global.getString(resolver,
130 Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
fredc649fe492012-04-19 01:07:18 -0700131 boolean mIsAirplaneSensitive = airplaneModeRadios == null ? true :
Christopher Tatec09cdce2012-09-10 16:50:14 -0700132 airplaneModeRadios.contains(Settings.Global.RADIO_BLUETOOTH);
fredc649fe492012-04-19 01:07:18 -0700133 if (mIsAirplaneSensitive) {
134 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
135 }
136 }
137
fredcbf072a72012-05-09 16:52:50 -0700138 private final IBluetoothCallback mBluetoothCallback = new IBluetoothCallback.Stub() {
139 @Override
140 public void onBluetoothStateChange(int prevState, int newState) throws RemoteException {
141 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_STATE_CHANGE,prevState,newState);
142 mHandler.sendMessage(msg);
143 }
144 };
145
146 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
fredc0f420372012-04-12 00:02:00 -0700147 @Override
148 public void onReceive(Context context, Intent intent) {
149 String action = intent.getAction();
fredcbf072a72012-05-09 16:52:50 -0700150 if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action)) {
fredc0f420372012-04-12 00:02:00 -0700151 String newName = intent.getStringExtra(BluetoothAdapter.EXTRA_LOCAL_NAME);
Freda8c6df02012-07-11 10:25:23 -0700152 if (DBG) Log.d(TAG, "Bluetooth Adapter name changed to " + newName);
fredc0f420372012-04-12 00:02:00 -0700153 if (newName != null) {
154 storeNameAndAddress(newName, null);
155 }
fredc649fe492012-04-19 01:07:18 -0700156 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
Zhihai Xu401202b2012-12-03 11:36:21 -0800157 synchronized(mReceiver) {
158 if (isBluetoothPersistedStateOn()) {
159 if (isAirplaneModeOn()) {
160 persistBluetoothSetting(BLUETOOTH_ON_AIRPLANE);
161 } else {
162 persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);
163 }
164 }
165 if (isAirplaneModeOn()) {
166 // disable without persisting the setting
167 sendDisableMsg();
168 } else if (mEnableExternal) {
169 // enable without persisting the setting
170 sendEnableMsg(mQuietEnableExternal);
171 }
fredc649fe492012-04-19 01:07:18 -0700172 }
Zhihai Xu40874a02012-10-08 17:57:03 -0700173 } else if (Intent.ACTION_USER_SWITCHED.equals(action)) {
174 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_USER_SWITCHED,
175 intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0), 0));
Zhihai Xu401202b2012-12-03 11:36:21 -0800176 } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
177 synchronized(mReceiver) {
178 if (mEnableExternal && isBluetoothPersistedStateOnBluetooth()) {
179 //Enable
180 if (DBG) Log.d(TAG, "Auto-enabling Bluetooth.");
181 sendEnableMsg(mQuietEnableExternal);
182 }
183 }
184
185 if (!isNameAndAddressSet()) {
186 //Sync the Bluetooth name and address from the Bluetooth Adapter
187 if (DBG) Log.d(TAG,"Retrieving Bluetooth Adapter name and address...");
188 getNameAndAddress();
189 }
fredc0f420372012-04-12 00:02:00 -0700190 }
191 }
192 };
193
194 BluetoothManagerService(Context context) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700195 mHandler = new BluetoothHandler(IoThread.get().getLooper());
Zhihai Xu40874a02012-10-08 17:57:03 -0700196
fredc0f420372012-04-12 00:02:00 -0700197 mContext = context;
198 mBluetooth = null;
199 mBinding = false;
200 mUnbinding = false;
Zhihai Xu40874a02012-10-08 17:57:03 -0700201 mEnable = false;
202 mState = BluetoothAdapter.STATE_OFF;
Zhihai Xu401202b2012-12-03 11:36:21 -0800203 mQuietEnableExternal = false;
204 mEnableExternal = false;
fredc0f420372012-04-12 00:02:00 -0700205 mAddress = null;
206 mName = null;
Zhihai Xudd9d17d2013-01-08 17:05:58 -0800207 mErrorRecoveryRetryCounter = 0;
fredc0f420372012-04-12 00:02:00 -0700208 mContentResolver = context.getContentResolver();
fredcd6883532012-04-25 17:46:13 -0700209 mCallbacks = new RemoteCallbackList<IBluetoothManagerCallback>();
210 mStateChangeCallbacks = new RemoteCallbackList<IBluetoothStateChangeCallback>();
Zhihai Xu401202b2012-12-03 11:36:21 -0800211 IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
Matthew Xie6fde3092012-07-11 17:10:07 -0700212 filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
Zhihai Xu40874a02012-10-08 17:57:03 -0700213 filter.addAction(Intent.ACTION_USER_SWITCHED);
Matthew Xie6fde3092012-07-11 17:10:07 -0700214 registerForAirplaneMode(filter);
215 mContext.registerReceiver(mReceiver, filter);
fredc0f420372012-04-12 00:02:00 -0700216 loadStoredNameAndAddress();
Zhihai Xu401202b2012-12-03 11:36:21 -0800217 if (isBluetoothPersistedStateOn()) {
218 mEnableExternal = true;
fredc0f420372012-04-12 00:02:00 -0700219 }
220 }
221
fredc649fe492012-04-19 01:07:18 -0700222 /**
223 * Returns true if airplane mode is currently on
224 */
225 private final boolean isAirplaneModeOn() {
Christopher Tatec09cdce2012-09-10 16:50:14 -0700226 return Settings.Global.getInt(mContext.getContentResolver(),
227 Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
fredc649fe492012-04-19 01:07:18 -0700228 }
229
230 /**
231 * Returns true if the Bluetooth saved state is "on"
232 */
233 private final boolean isBluetoothPersistedStateOn() {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700234 return Settings.Global.getInt(mContentResolver,
Zhihai Xu401202b2012-12-03 11:36:21 -0800235 Settings.Global.BLUETOOTH_ON, 0) != BLUETOOTH_OFF;
236 }
237
238 /**
239 * Returns true if the Bluetooth saved state is BLUETOOTH_ON_BLUETOOTH
240 */
241 private final boolean isBluetoothPersistedStateOnBluetooth() {
242 return Settings.Global.getInt(mContentResolver,
243 Settings.Global.BLUETOOTH_ON, 0) == BLUETOOTH_ON_BLUETOOTH;
fredc649fe492012-04-19 01:07:18 -0700244 }
245
246 /**
247 * Save the Bluetooth on/off state
248 *
249 */
Zhihai Xu401202b2012-12-03 11:36:21 -0800250 private void persistBluetoothSetting(int value) {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700251 Settings.Global.putInt(mContext.getContentResolver(),
252 Settings.Global.BLUETOOTH_ON,
Zhihai Xu401202b2012-12-03 11:36:21 -0800253 value);
fredc649fe492012-04-19 01:07:18 -0700254 }
255
256 /**
257 * Returns true if the Bluetooth Adapter's name and address is
258 * locally cached
259 * @return
260 */
fredc0f420372012-04-12 00:02:00 -0700261 private boolean isNameAndAddressSet() {
262 return mName !=null && mAddress!= null && mName.length()>0 && mAddress.length()>0;
263 }
264
fredc649fe492012-04-19 01:07:18 -0700265 /**
266 * Retrieve the Bluetooth Adapter's name and address and save it in
267 * in the local cache
268 */
fredc0f420372012-04-12 00:02:00 -0700269 private void loadStoredNameAndAddress() {
270 if (DBG) Log.d(TAG, "Loading stored name and address");
Zhihai Xud31c3222012-10-31 16:08:57 -0700271 if (mContext.getResources().getBoolean
272 (com.android.internal.R.bool.config_bluetooth_address_validation) &&
273 Settings.Secure.getInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 0) == 0) {
274 // if the valid flag is not set, don't load the address and name
275 if (DBG) Log.d(TAG, "invalid bluetooth name and address stored");
276 return;
277 }
fredc0f420372012-04-12 00:02:00 -0700278 mName = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME);
279 mAddress = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS);
Zhihai Xud31c3222012-10-31 16:08:57 -0700280 if (DBG) Log.d(TAG, "Stored bluetooth Name=" + mName + ",Address=" + mAddress);
fredc0f420372012-04-12 00:02:00 -0700281 }
282
fredc649fe492012-04-19 01:07:18 -0700283 /**
284 * Save the Bluetooth name and address in the persistent store.
285 * Only non-null values will be saved.
286 * @param name
287 * @param address
288 */
fredc0f420372012-04-12 00:02:00 -0700289 private void storeNameAndAddress(String name, String address) {
290 if (name != null) {
291 Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME, name);
fredc0f420372012-04-12 00:02:00 -0700292 mName = name;
fredc649fe492012-04-19 01:07:18 -0700293 if (DBG) Log.d(TAG,"Stored Bluetooth name: " +
294 Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_NAME));
fredc0f420372012-04-12 00:02:00 -0700295 }
296
297 if (address != null) {
298 Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS, address);
fredc0f420372012-04-12 00:02:00 -0700299 mAddress=address;
fredc649fe492012-04-19 01:07:18 -0700300 if (DBG) Log.d(TAG,"Stored Bluetoothaddress: " +
301 Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_ADDRESS));
fredc0f420372012-04-12 00:02:00 -0700302 }
Zhihai Xud31c3222012-10-31 16:08:57 -0700303
304 if ((name != null) && (address != null)) {
305 Settings.Secure.putInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 1);
306 }
fredc0f420372012-04-12 00:02:00 -0700307 }
308
309 public IBluetooth registerAdapter(IBluetoothManagerCallback callback){
fredc0f420372012-04-12 00:02:00 -0700310 Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_ADAPTER);
311 msg.obj = callback;
312 mHandler.sendMessage(msg);
313 synchronized(mConnection) {
314 return mBluetooth;
315 }
316 }
317
318 public void unregisterAdapter(IBluetoothManagerCallback callback) {
319 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
320 "Need BLUETOOTH permission");
321 Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_ADAPTER);
322 msg.obj = callback;
323 mHandler.sendMessage(msg);
324 }
325
326 public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) {
327 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
328 "Need BLUETOOTH permission");
329 Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_STATE_CHANGE_CALLBACK);
330 msg.obj = callback;
331 mHandler.sendMessage(msg);
332 }
333
334 public void unregisterStateChangeCallback(IBluetoothStateChangeCallback callback) {
335 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
336 "Need BLUETOOTH permission");
337 Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK);
338 msg.obj = callback;
339 mHandler.sendMessage(msg);
340 }
341
342 public boolean isEnabled() {
Zhihai Xu6eb76522012-11-29 15:41:04 -0800343 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
344 (!checkIfCallerIsForegroundUser())) {
345 Log.w(TAG,"isEnabled(): not allowed for non-active and non system user");
Zhihai Xu40874a02012-10-08 17:57:03 -0700346 return false;
347 }
348
fredc0f420372012-04-12 00:02:00 -0700349 synchronized(mConnection) {
350 try {
351 return (mBluetooth != null && mBluetooth.isEnabled());
352 } catch (RemoteException e) {
353 Log.e(TAG, "isEnabled()", e);
354 }
355 }
356 return false;
357 }
358
359 public void getNameAndAddress() {
fredcf2458862012-04-16 15:18:27 -0700360 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700361 Log.d(TAG,"getNameAndAddress(): mBluetooth = " + mBluetooth +
362 " mBinding = " + mBinding);
fredcf2458862012-04-16 15:18:27 -0700363 }
fredc0f420372012-04-12 00:02:00 -0700364 Message msg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
365 mHandler.sendMessage(msg);
366 }
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700367 public boolean enableNoAutoConnect()
368 {
369 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
370 "Need BLUETOOTH ADMIN permission");
Zhihai Xu40874a02012-10-08 17:57:03 -0700371
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700372 if (DBG) {
373 Log.d(TAG,"enableNoAutoConnect(): mBluetooth =" + mBluetooth +
374 " mBinding = " + mBinding);
375 }
Martijn Coenen8385c5a2012-11-29 10:14:16 -0800376 int callingAppId = UserHandle.getAppId(Binder.getCallingUid());
377
378 if (callingAppId != Process.NFC_UID) {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700379 throw new SecurityException("no permission to enable Bluetooth quietly");
380 }
Martijn Coenen8385c5a2012-11-29 10:14:16 -0800381
Zhihai Xu401202b2012-12-03 11:36:21 -0800382 synchronized(mReceiver) {
383 mQuietEnableExternal = true;
384 mEnableExternal = true;
385 sendEnableMsg(true);
386 }
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700387 return true;
388
389 }
fredc0f420372012-04-12 00:02:00 -0700390 public boolean enable() {
Zhihai Xu6eb76522012-11-29 15:41:04 -0800391 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
392 (!checkIfCallerIsForegroundUser())) {
393 Log.w(TAG,"enable(): not allowed for non-active and non system user");
Zhihai Xu40874a02012-10-08 17:57:03 -0700394 return false;
fredcf2458862012-04-16 15:18:27 -0700395 }
396
Zhihai Xu401202b2012-12-03 11:36:21 -0800397 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
398 "Need BLUETOOTH ADMIN permission");
399 if (DBG) {
400 Log.d(TAG,"enable(): mBluetooth =" + mBluetooth +
401 " mBinding = " + mBinding);
402 }
403
404 synchronized(mReceiver) {
405 mQuietEnableExternal = false;
406 mEnableExternal = true;
407 // waive WRITE_SECURE_SETTINGS permission check
408 long callingIdentity = Binder.clearCallingIdentity();
409 persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);
410 Binder.restoreCallingIdentity(callingIdentity);
411 sendEnableMsg(false);
412 }
413 return true;
fredc0f420372012-04-12 00:02:00 -0700414 }
415
416 public boolean disable(boolean persist) {
417 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
418 "Need BLUETOOTH ADMIN permissicacheNameAndAddresson");
Zhihai Xu40874a02012-10-08 17:57:03 -0700419
Zhihai Xu6eb76522012-11-29 15:41:04 -0800420 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
421 (!checkIfCallerIsForegroundUser())) {
422 Log.w(TAG,"disable(): not allowed for non-active and non system user");
Zhihai Xu40874a02012-10-08 17:57:03 -0700423 return false;
424 }
425
fredcf2458862012-04-16 15:18:27 -0700426 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700427 Log.d(TAG,"disable(): mBluetooth = " + mBluetooth +
428 " mBinding = " + mBinding);
429 }
fredcf2458862012-04-16 15:18:27 -0700430
Zhihai Xu401202b2012-12-03 11:36:21 -0800431 synchronized(mReceiver) {
432 if (persist) {
433 // waive WRITE_SECURE_SETTINGS permission check
434 long callingIdentity = Binder.clearCallingIdentity();
435 persistBluetoothSetting(BLUETOOTH_OFF);
436 Binder.restoreCallingIdentity(callingIdentity);
437 }
438 mEnableExternal = false;
439 sendDisableMsg();
440 }
fredc0f420372012-04-12 00:02:00 -0700441 return true;
442 }
443
fredc649fe492012-04-19 01:07:18 -0700444 public void unbindAndFinish() {
fredcf2458862012-04-16 15:18:27 -0700445 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700446 Log.d(TAG,"unbindAndFinish(): " + mBluetooth +
447 " mBinding = " + mBinding);
fredcf2458862012-04-16 15:18:27 -0700448 }
449
fredc0f420372012-04-12 00:02:00 -0700450 synchronized (mConnection) {
451 if (mUnbinding) return;
452 mUnbinding = true;
Zhihai Xu40874a02012-10-08 17:57:03 -0700453 if (mBluetooth != null) {
fredcbf072a72012-05-09 16:52:50 -0700454 if (!mConnection.isGetNameAddressOnly()) {
455 //Unregister callback object
456 try {
457 mBluetooth.unregisterCallback(mBluetoothCallback);
458 } catch (RemoteException re) {
Zhihai Xu40874a02012-10-08 17:57:03 -0700459 Log.e(TAG, "Unable to unregister BluetoothCallback",re);
fredcbf072a72012-05-09 16:52:50 -0700460 }
461 }
fredc0f420372012-04-12 00:02:00 -0700462 if (DBG) Log.d(TAG, "Sending unbind request.");
fredcd6883532012-04-25 17:46:13 -0700463 mBluetooth = null;
464 //Unbind
fredc0f420372012-04-12 00:02:00 -0700465 mContext.unbindService(mConnection);
fredcd6883532012-04-25 17:46:13 -0700466 mUnbinding = false;
Zhihai Xu40874a02012-10-08 17:57:03 -0700467 mBinding = false;
fredcf2458862012-04-16 15:18:27 -0700468 } else {
469 mUnbinding=false;
fredc0f420372012-04-12 00:02:00 -0700470 }
471 }
472 }
473
Matthew Xieddf7e472013-03-01 18:41:02 -0800474 public IBluetoothGatt getBluetoothGatt() {
475 // sync protection
476 return mBluetoothGatt;
477 }
478
fredcbf072a72012-05-09 16:52:50 -0700479 private void sendBluetoothStateCallback(boolean isUp) {
480 int n = mStateChangeCallbacks.beginBroadcast();
Freda8c6df02012-07-11 10:25:23 -0700481 if (DBG) Log.d(TAG,"Broadcasting onBluetoothStateChange("+isUp+") to " + n + " receivers.");
fredcbf072a72012-05-09 16:52:50 -0700482 for (int i=0; i <n;i++) {
483 try {
484 mStateChangeCallbacks.getBroadcastItem(i).onBluetoothStateChange(isUp);
485 } catch (RemoteException e) {
486 Log.e(TAG, "Unable to call onBluetoothStateChange() on callback #" + i , e);
487 }
488 }
489 mStateChangeCallbacks.finishBroadcast();
490 }
491
492 /**
Zhihai Xu40874a02012-10-08 17:57:03 -0700493 * Inform BluetoothAdapter instances that Adapter service is up
494 */
495 private void sendBluetoothServiceUpCallback() {
496 if (!mConnection.isGetNameAddressOnly()) {
497 if (DBG) Log.d(TAG,"Calling onBluetoothServiceUp callbacks");
498 int n = mCallbacks.beginBroadcast();
499 Log.d(TAG,"Broadcasting onBluetoothServiceUp() to " + n + " receivers.");
500 for (int i=0; i <n;i++) {
501 try {
502 mCallbacks.getBroadcastItem(i).onBluetoothServiceUp(mBluetooth);
503 } catch (RemoteException e) {
504 Log.e(TAG, "Unable to call onBluetoothServiceUp() on callback #" + i, e);
505 }
506 }
507 mCallbacks.finishBroadcast();
508 }
509 }
510 /**
fredcbf072a72012-05-09 16:52:50 -0700511 * Inform BluetoothAdapter instances that Adapter service is down
512 */
513 private void sendBluetoothServiceDownCallback() {
fredcd6883532012-04-25 17:46:13 -0700514 if (!mConnection.isGetNameAddressOnly()) {
515 if (DBG) Log.d(TAG,"Calling onBluetoothServiceDown callbacks");
516 int n = mCallbacks.beginBroadcast();
517 Log.d(TAG,"Broadcasting onBluetoothServiceDown() to " + n + " receivers.");
518 for (int i=0; i <n;i++) {
519 try {
520 mCallbacks.getBroadcastItem(i).onBluetoothServiceDown();
521 } catch (RemoteException e) {
522 Log.e(TAG, "Unable to call onBluetoothServiceDown() on callback #" + i, e);
523 }
524 }
525 mCallbacks.finishBroadcast();
526 }
527 }
fredc0f420372012-04-12 00:02:00 -0700528 public String getAddress() {
Matthew Xieaf5ddbf2012-12-04 10:47:43 -0800529 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
530 "Need BLUETOOTH permission");
Zhihai Xu40874a02012-10-08 17:57:03 -0700531
Zhihai Xu6eb76522012-11-29 15:41:04 -0800532 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
533 (!checkIfCallerIsForegroundUser())) {
534 Log.w(TAG,"getAddress(): not allowed for non-active and non system user");
535 return null;
Zhihai Xu40874a02012-10-08 17:57:03 -0700536 }
537
fredc116d1d462012-04-20 14:47:08 -0700538 synchronized(mConnection) {
539 if (mBluetooth != null) {
540 try {
541 return mBluetooth.getAddress();
542 } catch (RemoteException e) {
543 Log.e(TAG, "getAddress(): Unable to retrieve address remotely..Returning cached address",e);
544 }
545 }
546 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700547 // mAddress is accessed from outside.
548 // It is alright without a lock. Here, bluetooth is off, no other thread is
549 // changing mAddress
fredc0f420372012-04-12 00:02:00 -0700550 return mAddress;
551 }
fredc649fe492012-04-19 01:07:18 -0700552
fredc0f420372012-04-12 00:02:00 -0700553 public String getName() {
Matthew Xieaf5ddbf2012-12-04 10:47:43 -0800554 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
555 "Need BLUETOOTH permission");
Zhihai Xu40874a02012-10-08 17:57:03 -0700556
Zhihai Xu6eb76522012-11-29 15:41:04 -0800557 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
558 (!checkIfCallerIsForegroundUser())) {
559 Log.w(TAG,"getName(): not allowed for non-active and non system user");
560 return null;
Zhihai Xu40874a02012-10-08 17:57:03 -0700561 }
562
fredc116d1d462012-04-20 14:47:08 -0700563 synchronized(mConnection) {
564 if (mBluetooth != null) {
565 try {
566 return mBluetooth.getName();
567 } catch (RemoteException e) {
568 Log.e(TAG, "getName(): Unable to retrieve name remotely..Returning cached name",e);
569 }
570 }
571 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700572 // mName is accessed from outside.
573 // It alright without a lock. Here, bluetooth is off, no other thread is
574 // changing mName
fredc0f420372012-04-12 00:02:00 -0700575 return mName;
576 }
577
fredc0f420372012-04-12 00:02:00 -0700578 private class BluetoothServiceConnection implements ServiceConnection {
579
580 private boolean mGetNameAddressOnly;
581
582 public void setGetNameAddressOnly(boolean getOnly) {
583 mGetNameAddressOnly = getOnly;
584 }
585
586 public boolean isGetNameAddressOnly() {
587 return mGetNameAddressOnly;
588 }
589
590 public void onServiceConnected(ComponentName className, IBinder service) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800591 if (DBG) Log.d(TAG, "BluetoothServiceConnection: " + className.getClassName());
fredc0f420372012-04-12 00:02:00 -0700592 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED);
Matthew Xieddf7e472013-03-01 18:41:02 -0800593 // TBD if (className.getClassName().equals(IBluetooth.class.getName())) {
594 if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) {
595 msg.arg1 = SERVICE_IBLUETOOTH;
596 // } else if (className.getClassName().equals(IBluetoothGatt.class.getName())) {
597 } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) {
598 msg.arg1 = SERVICE_IBLUETOOTHGATT;
599 } else {
600 Log.e(TAG, "Unknown service connected: " + className.getClassName());
601 return;
602 }
fredc0f420372012-04-12 00:02:00 -0700603 msg.obj = service;
604 mHandler.sendMessage(msg);
605 }
606
607 public void onServiceDisconnected(ComponentName className) {
fredc0f420372012-04-12 00:02:00 -0700608 // Called if we unexpected disconnected.
Matthew Xieddf7e472013-03-01 18:41:02 -0800609 if (DBG) Log.d(TAG, "BluetoothServiceConnection, disconnected: " +
610 className.getClassName());
fredc0f420372012-04-12 00:02:00 -0700611 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED);
Matthew Xieddf7e472013-03-01 18:41:02 -0800612 if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) {
613 msg.arg1 = SERVICE_IBLUETOOTH;
614 } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) {
615 msg.arg1 = SERVICE_IBLUETOOTHGATT;
616 } else {
617 Log.e(TAG, "Unknown service disconnected: " + className.getClassName());
618 return;
619 }
fredc0f420372012-04-12 00:02:00 -0700620 mHandler.sendMessage(msg);
621 }
622 }
623
624 private BluetoothServiceConnection mConnection = new BluetoothServiceConnection();
625
Zhihai Xu40874a02012-10-08 17:57:03 -0700626 private class BluetoothHandler extends Handler {
627 public BluetoothHandler(Looper looper) {
628 super(looper);
629 }
630
fredc0f420372012-04-12 00:02:00 -0700631 @Override
632 public void handleMessage(Message msg) {
633 if (DBG) Log.d (TAG, "Message: " + msg.what);
fredc0f420372012-04-12 00:02:00 -0700634 switch (msg.what) {
635 case MESSAGE_GET_NAME_AND_ADDRESS: {
fredc649fe492012-04-19 01:07:18 -0700636 if (DBG) Log.d(TAG,"MESSAGE_GET_NAME_AND_ADDRESS");
Matthew Xiecdce0b92012-07-12 19:06:15 -0700637 synchronized(mConnection) {
fredc0f420372012-04-12 00:02:00 -0700638 //Start bind request
Zhihai Xu40874a02012-10-08 17:57:03 -0700639 if ((mBluetooth == null) && (!mBinding)) {
fredc0f420372012-04-12 00:02:00 -0700640 if (DBG) Log.d(TAG, "Binding to service to get name and address");
641 mConnection.setGetNameAddressOnly(true);
642 //Start bind timeout and bind
643 Message timeoutMsg = mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
644 mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS);
645 Intent i = new Intent(IBluetooth.class.getName());
Amith Yamasani27b89e62013-01-16 12:30:11 -0800646 if (!mContext.bindServiceAsUser(i, mConnection,
647 Context.BIND_AUTO_CREATE, UserHandle.CURRENT)) {
fredc0f420372012-04-12 00:02:00 -0700648 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
649 Log.e(TAG, "fail to bind to: " + IBluetooth.class.getName());
Zhihai Xu40874a02012-10-08 17:57:03 -0700650 } else {
651 mBinding = true;
fredc0f420372012-04-12 00:02:00 -0700652 }
653 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700654 else {
655 Message saveMsg= mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS);
Zhihai Xu40874a02012-10-08 17:57:03 -0700656 saveMsg.arg1 = 0;
657 if (mBluetooth != null) {
658 mHandler.sendMessage(saveMsg);
659 } else {
660 // if enable is also called to bind the service
661 // wait for MESSAGE_BLUETOOTH_SERVICE_CONNECTED
662 mHandler.sendMessageDelayed(saveMsg, TIMEOUT_SAVE_MS);
663 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700664 }
fredc0f420372012-04-12 00:02:00 -0700665 }
fredc649fe492012-04-19 01:07:18 -0700666 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700667 }
fredc0f420372012-04-12 00:02:00 -0700668 case MESSAGE_SAVE_NAME_AND_ADDRESS: {
Zhihai Xud31c3222012-10-31 16:08:57 -0700669 boolean unbind = false;
fredc649fe492012-04-19 01:07:18 -0700670 if (DBG) Log.d(TAG,"MESSAGE_SAVE_NAME_AND_ADDRESS");
Matthew Xiecdce0b92012-07-12 19:06:15 -0700671 synchronized(mConnection) {
Zhihai Xud31c3222012-10-31 16:08:57 -0700672 if (!mEnable && mBluetooth != null) {
673 try {
674 mBluetooth.enable();
675 } catch (RemoteException e) {
676 Log.e(TAG,"Unable to call enable()",e);
677 }
678 }
679 }
680 if (mBluetooth != null) waitForOnOff(true, false);
681 synchronized(mConnection) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700682 if (mBluetooth != null) {
683 String name = null;
684 String address = null;
685 try {
686 name = mBluetooth.getName();
687 address = mBluetooth.getAddress();
688 } catch (RemoteException re) {
689 Log.e(TAG,"",re);
690 }
fredc0f420372012-04-12 00:02:00 -0700691
Matthew Xiecdce0b92012-07-12 19:06:15 -0700692 if (name != null && address != null) {
693 storeNameAndAddress(name,address);
Zhihai Xu40874a02012-10-08 17:57:03 -0700694 if (mConnection.isGetNameAddressOnly()) {
Zhihai Xud31c3222012-10-31 16:08:57 -0700695 unbind = true;
Zhihai Xu40874a02012-10-08 17:57:03 -0700696 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700697 } else {
698 if (msg.arg1 < MAX_SAVE_RETRIES) {
699 Message retryMsg = mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS);
700 retryMsg.arg1= 1+msg.arg1;
701 if (DBG) Log.d(TAG,"Retrying name/address remote retrieval and save.....Retry count =" + retryMsg.arg1);
702 mHandler.sendMessageDelayed(retryMsg, TIMEOUT_SAVE_MS);
703 } else {
704 Log.w(TAG,"Maximum name/address remote retrieval retry exceeded");
Zhihai Xu40874a02012-10-08 17:57:03 -0700705 if (mConnection.isGetNameAddressOnly()) {
Zhihai Xud31c3222012-10-31 16:08:57 -0700706 unbind = true;
Zhihai Xu40874a02012-10-08 17:57:03 -0700707 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700708 }
fredc0f420372012-04-12 00:02:00 -0700709 }
Zhihai Xud31c3222012-10-31 16:08:57 -0700710 if (!mEnable) {
711 try {
712 mBluetooth.disable();
713 } catch (RemoteException e) {
714 Log.e(TAG,"Unable to call disable()",e);
715 }
716 }
Zhihai Xu40874a02012-10-08 17:57:03 -0700717 } else {
718 // rebind service by Request GET NAME AND ADDRESS
719 // if service is unbinded by disable or
720 // MESSAGE_BLUETOOTH_SERVICE_CONNECTED is not received
721 Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
722 mHandler.sendMessage(getMsg);
fredc0f420372012-04-12 00:02:00 -0700723 }
724 }
Zhihai Xud31c3222012-10-31 16:08:57 -0700725 if (!mEnable && mBluetooth != null) waitForOnOff(false, true);
726 if (unbind) {
727 unbindAndFinish();
728 }
fredc649fe492012-04-19 01:07:18 -0700729 break;
fredc649fe492012-04-19 01:07:18 -0700730 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700731 case MESSAGE_ENABLE:
fredcf2458862012-04-16 15:18:27 -0700732 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700733 Log.d(TAG, "MESSAGE_ENABLE: mBluetooth = " + mBluetooth);
fredc649fe492012-04-19 01:07:18 -0700734 }
Zhihai Xu40874a02012-10-08 17:57:03 -0700735 mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
736 mEnable = true;
Zhihai Xu401202b2012-12-03 11:36:21 -0800737 handleEnable(msg.arg1 == 1);
fredc649fe492012-04-19 01:07:18 -0700738 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700739
fredc0f420372012-04-12 00:02:00 -0700740 case MESSAGE_DISABLE:
Zhihai Xu40874a02012-10-08 17:57:03 -0700741 mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
742 if (mEnable && mBluetooth != null) {
743 waitForOnOff(true, false);
744 mEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -0800745 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -0700746 waitForOnOff(false, false);
747 } else {
748 mEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -0800749 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -0700750 }
fredc0f420372012-04-12 00:02:00 -0700751 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700752
fredc0f420372012-04-12 00:02:00 -0700753 case MESSAGE_REGISTER_ADAPTER:
754 {
755 IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -0700756 boolean added = mCallbacks.register(callback);
757 Log.d(TAG,"Added callback: " + (callback == null? "null": callback) +":" +added );
fredc0f420372012-04-12 00:02:00 -0700758 }
759 break;
760 case MESSAGE_UNREGISTER_ADAPTER:
761 {
762 IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -0700763 boolean removed = mCallbacks.unregister(callback);
764 Log.d(TAG,"Removed callback: " + (callback == null? "null": callback) +":" + removed);
fredc0f420372012-04-12 00:02:00 -0700765 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700766 }
fredc0f420372012-04-12 00:02:00 -0700767 case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK:
768 {
769 IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -0700770 mStateChangeCallbacks.register(callback);
fredc0f420372012-04-12 00:02:00 -0700771 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700772 }
fredc0f420372012-04-12 00:02:00 -0700773 case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK:
774 {
775 IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -0700776 mStateChangeCallbacks.unregister(callback);
fredc0f420372012-04-12 00:02:00 -0700777 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700778 }
fredc0f420372012-04-12 00:02:00 -0700779 case MESSAGE_BLUETOOTH_SERVICE_CONNECTED:
780 {
Matthew Xieddf7e472013-03-01 18:41:02 -0800781 if (DBG) Log.d(TAG,"MESSAGE_BLUETOOTH_SERVICE_CONNECTED: " + msg.arg1);
fredc0f420372012-04-12 00:02:00 -0700782
783 IBinder service = (IBinder) msg.obj;
784 synchronized(mConnection) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800785 if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
786 mBluetoothGatt = IBluetoothGatt.Stub.asInterface(service);
787 break;
788 } // else must be SERVICE_IBLUETOOTH
789
790 //Remove timeout
791 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
792
fredc0f420372012-04-12 00:02:00 -0700793 mBinding = false;
794 mBluetooth = IBluetooth.Stub.asInterface(service);
fredc0f420372012-04-12 00:02:00 -0700795
Matthew Xiecdce0b92012-07-12 19:06:15 -0700796 if (mConnection.isGetNameAddressOnly()) {
797 //Request GET NAME AND ADDRESS
798 Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
799 mHandler.sendMessage(getMsg);
Zhihai Xu40874a02012-10-08 17:57:03 -0700800 if (!mEnable) return;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700801 }
fredc0f420372012-04-12 00:02:00 -0700802
Zhihai Xu40874a02012-10-08 17:57:03 -0700803 mConnection.setGetNameAddressOnly(false);
Matthew Xiecdce0b92012-07-12 19:06:15 -0700804 //Register callback object
fredcbf072a72012-05-09 16:52:50 -0700805 try {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700806 mBluetooth.registerCallback(mBluetoothCallback);
807 } catch (RemoteException re) {
808 Log.e(TAG, "Unable to register BluetoothCallback",re);
fredcbf072a72012-05-09 16:52:50 -0700809 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700810 //Inform BluetoothAdapter instances that service is up
Zhihai Xu40874a02012-10-08 17:57:03 -0700811 sendBluetoothServiceUpCallback();
812
Matthew Xiecdce0b92012-07-12 19:06:15 -0700813 //Do enable request
814 try {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700815 if (mQuietEnable == false) {
816 if(!mBluetooth.enable()) {
817 Log.e(TAG,"IBluetooth.enable() returned false");
818 }
819 }
820 else
821 {
822 if(!mBluetooth.enableNoAutoConnect()) {
823 Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false");
824 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700825 }
826 } catch (RemoteException e) {
827 Log.e(TAG,"Unable to call enable()",e);
828 }
Freda8c6df02012-07-11 10:25:23 -0700829 }
Zhihai Xu40874a02012-10-08 17:57:03 -0700830
831 if (!mEnable) {
832 waitForOnOff(true, false);
Zhihai Xu401202b2012-12-03 11:36:21 -0800833 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -0700834 waitForOnOff(false, false);
835 }
fredc649fe492012-04-19 01:07:18 -0700836 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700837 }
fredc649fe492012-04-19 01:07:18 -0700838 case MESSAGE_TIMEOUT_BIND: {
839 Log.e(TAG, "MESSAGE_TIMEOUT_BIND");
fredc0f420372012-04-12 00:02:00 -0700840 synchronized(mConnection) {
841 mBinding = false;
842 }
fredc649fe492012-04-19 01:07:18 -0700843 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700844 }
fredcbf072a72012-05-09 16:52:50 -0700845 case MESSAGE_BLUETOOTH_STATE_CHANGE:
fredc0f420372012-04-12 00:02:00 -0700846 {
fredcbf072a72012-05-09 16:52:50 -0700847 int prevState = msg.arg1;
848 int newState = msg.arg2;
849 if (DBG) Log.d(TAG, "MESSAGE_BLUETOOTH_STATE_CHANGE: prevState = " + prevState + ", newState=" + newState);
Zhihai Xu40874a02012-10-08 17:57:03 -0700850 mState = newState;
851 bluetoothStateChangeHandler(prevState, newState);
Zhihai Xudd9d17d2013-01-08 17:05:58 -0800852 // handle error state transition case from TURNING_ON to OFF
853 // unbind and rebind bluetooth service and enable bluetooth
854 if ((prevState == BluetoothAdapter.STATE_TURNING_ON) &&
855 (newState == BluetoothAdapter.STATE_OFF) &&
856 (mBluetooth != null) && mEnable) {
857 recoverBluetoothServiceFromError();
858 }
859 if (newState == BluetoothAdapter.STATE_ON) {
860 // bluetooth is working, reset the counter
861 if (mErrorRecoveryRetryCounter != 0) {
862 Log.w(TAG, "bluetooth is recovered from error");
863 mErrorRecoveryRetryCounter = 0;
864 }
865 }
fredc649fe492012-04-19 01:07:18 -0700866 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700867 }
fredc0f420372012-04-12 00:02:00 -0700868 case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED:
869 {
Matthew Xieddf7e472013-03-01 18:41:02 -0800870 Log.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: " + msg.arg1);
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +0530871 synchronized(mConnection) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800872 if (msg.arg1 == SERVICE_IBLUETOOTH) {
873 // if service is unbinded already, do nothing and return
874 if (mBluetooth == null) break;
875 mBluetooth = null;
876 } else if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
877 mBluetoothGatt = null;
878 break;
879 } else {
880 Log.e(TAG, "Bad msg.arg1: " + msg.arg1);
881 break;
882 }
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +0530883 }
Zhihai Xu40874a02012-10-08 17:57:03 -0700884
885 if (mEnable) {
886 mEnable = false;
887 // Send a Bluetooth Restart message
888 Message restartMsg = mHandler.obtainMessage(
889 MESSAGE_RESTART_BLUETOOTH_SERVICE);
890 mHandler.sendMessageDelayed(restartMsg,
891 SERVICE_RESTART_TIME_MS);
892 }
893
894 if (!mConnection.isGetNameAddressOnly()) {
895 sendBluetoothServiceDownCallback();
896
897 // Send BT state broadcast to update
898 // the BT icon correctly
Zhihai Xu4e22ad32012-11-13 15:11:26 -0800899 if ((mState == BluetoothAdapter.STATE_TURNING_ON) ||
900 (mState == BluetoothAdapter.STATE_ON)) {
901 bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,
902 BluetoothAdapter.STATE_TURNING_OFF);
903 mState = BluetoothAdapter.STATE_TURNING_OFF;
904 }
905 if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
906 bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
907 BluetoothAdapter.STATE_OFF);
908 }
909
910 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
Zhihai Xu40874a02012-10-08 17:57:03 -0700911 mState = BluetoothAdapter.STATE_OFF;
912 }
fredc649fe492012-04-19 01:07:18 -0700913 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700914 }
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +0530915 case MESSAGE_RESTART_BLUETOOTH_SERVICE:
916 {
917 Log.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE:"
918 +" Restart IBluetooth service");
919 /* Enable without persisting the setting as
920 it doesnt change when IBluetooth
921 service restarts */
Zhihai Xu40874a02012-10-08 17:57:03 -0700922 mEnable = true;
Zhihai Xu401202b2012-12-03 11:36:21 -0800923 handleEnable(mQuietEnable);
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +0530924 break;
925 }
926
fredc0f420372012-04-12 00:02:00 -0700927 case MESSAGE_TIMEOUT_UNBIND:
928 {
fredc649fe492012-04-19 01:07:18 -0700929 Log.e(TAG, "MESSAGE_TIMEOUT_UNBIND");
fredc0f420372012-04-12 00:02:00 -0700930 synchronized(mConnection) {
931 mUnbinding = false;
932 }
fredc649fe492012-04-19 01:07:18 -0700933 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700934 }
Zhihai Xu40874a02012-10-08 17:57:03 -0700935
936 case MESSAGE_USER_SWITCHED:
937 {
938 if (DBG) {
939 Log.d(TAG, "MESSAGE_USER_SWITCHED");
940 }
941 mHandler.removeMessages(MESSAGE_USER_SWITCHED);
942 /* disable and enable BT when detect a user switch */
943 if (mEnable && mBluetooth != null) {
944 synchronized (mConnection) {
945 if (mBluetooth != null) {
946 //Unregister callback object
947 try {
948 mBluetooth.unregisterCallback(mBluetoothCallback);
949 } catch (RemoteException re) {
950 Log.e(TAG, "Unable to unregister",re);
951 }
952 }
953 }
Zhihai Xu4e22ad32012-11-13 15:11:26 -0800954
955 if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
956 // MESSAGE_USER_SWITCHED happened right after MESSAGE_ENABLE
957 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_OFF);
958 mState = BluetoothAdapter.STATE_OFF;
959 }
960 if (mState == BluetoothAdapter.STATE_OFF) {
961 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_TURNING_ON);
962 mState = BluetoothAdapter.STATE_TURNING_ON;
963 }
Zhihai Xu40874a02012-10-08 17:57:03 -0700964
965 waitForOnOff(true, false);
966
Zhihai Xu4e22ad32012-11-13 15:11:26 -0800967 if (mState == BluetoothAdapter.STATE_TURNING_ON) {
968 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_ON);
969 }
Zhihai Xu40874a02012-10-08 17:57:03 -0700970
971 // disable
Zhihai Xu401202b2012-12-03 11:36:21 -0800972 handleDisable();
Zhihai Xu4e22ad32012-11-13 15:11:26 -0800973 // Pbap service need receive STATE_TURNING_OFF intent to close
974 bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,
975 BluetoothAdapter.STATE_TURNING_OFF);
Zhihai Xu40874a02012-10-08 17:57:03 -0700976
977 waitForOnOff(false, true);
978
Zhihai Xu4e22ad32012-11-13 15:11:26 -0800979 bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
Zhihai Xu40874a02012-10-08 17:57:03 -0700980 BluetoothAdapter.STATE_OFF);
Zhihai Xu40874a02012-10-08 17:57:03 -0700981 sendBluetoothServiceDownCallback();
982 synchronized (mConnection) {
983 if (mBluetooth != null) {
984 mBluetooth = null;
985 //Unbind
986 mContext.unbindService(mConnection);
987 }
988 }
989 SystemClock.sleep(100);
990
Zhihai Xu4e22ad32012-11-13 15:11:26 -0800991 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
992 mState = BluetoothAdapter.STATE_OFF;
Zhihai Xu40874a02012-10-08 17:57:03 -0700993 // enable
Zhihai Xu401202b2012-12-03 11:36:21 -0800994 handleEnable(mQuietEnable);
Zhihai Xu40874a02012-10-08 17:57:03 -0700995 } else if (mBinding || mBluetooth != null) {
996 Message userMsg = mHandler.obtainMessage(MESSAGE_USER_SWITCHED);
997 userMsg.arg2 = 1 + msg.arg2;
998 // if user is switched when service is being binding
999 // delay sending MESSAGE_USER_SWITCHED
1000 mHandler.sendMessageDelayed(userMsg, USER_SWITCHED_TIME_MS);
1001 if (DBG) {
1002 Log.d(TAG, "delay MESSAGE_USER_SWITCHED " + userMsg.arg2);
1003 }
1004 }
1005 break;
1006 }
fredc0f420372012-04-12 00:02:00 -07001007 }
1008 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001009 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001010
Zhihai Xu401202b2012-12-03 11:36:21 -08001011 private void handleEnable(boolean quietMode) {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001012 mQuietEnable = quietMode;
1013
Matthew Xiecdce0b92012-07-12 19:06:15 -07001014 synchronized(mConnection) {
Zhihai Xu40874a02012-10-08 17:57:03 -07001015 if ((mBluetooth == null) && (!mBinding)) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001016 //Start bind timeout and bind
1017 Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
1018 mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS);
1019 mConnection.setGetNameAddressOnly(false);
1020 Intent i = new Intent(IBluetooth.class.getName());
Amith Yamasani27b89e62013-01-16 12:30:11 -08001021 if (!mContext.bindServiceAsUser(i, mConnection,Context.BIND_AUTO_CREATE,
1022 UserHandle.CURRENT)) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001023 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
1024 Log.e(TAG, "Fail to bind to: " + IBluetooth.class.getName());
Zhihai Xu40874a02012-10-08 17:57:03 -07001025 } else {
1026 mBinding = true;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001027 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001028 } else if (mBluetooth != null) {
1029 if (mConnection.isGetNameAddressOnly()) {
1030 // if GetNameAddressOnly is set, we can clear this flag,
1031 // so the service won't be unbind
1032 // after name and address are saved
1033 mConnection.setGetNameAddressOnly(false);
1034 //Register callback object
1035 try {
1036 mBluetooth.registerCallback(mBluetoothCallback);
1037 } catch (RemoteException re) {
1038 Log.e(TAG, "Unable to register BluetoothCallback",re);
1039 }
1040 //Inform BluetoothAdapter instances that service is up
1041 sendBluetoothServiceUpCallback();
1042 }
1043
Matthew Xiecdce0b92012-07-12 19:06:15 -07001044 //Enable bluetooth
1045 try {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001046 if (!mQuietEnable) {
1047 if(!mBluetooth.enable()) {
1048 Log.e(TAG,"IBluetooth.enable() returned false");
1049 }
1050 }
1051 else {
1052 if(!mBluetooth.enableNoAutoConnect()) {
1053 Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false");
1054 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001055 }
1056 } catch (RemoteException e) {
1057 Log.e(TAG,"Unable to call enable()",e);
1058 }
1059 }
1060 }
1061 }
1062
Zhihai Xu401202b2012-12-03 11:36:21 -08001063 private void handleDisable() {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001064 synchronized(mConnection) {
Zhihai Xu40874a02012-10-08 17:57:03 -07001065 // don't need to disable if GetNameAddressOnly is set,
1066 // service will be unbinded after Name and Address are saved
1067 if ((mBluetooth != null) && (!mConnection.isGetNameAddressOnly())) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001068 if (DBG) Log.d(TAG,"Sending off request.");
1069
1070 try {
1071 if(!mBluetooth.disable()) {
1072 Log.e(TAG,"IBluetooth.disable() returned false");
1073 }
1074 } catch (RemoteException e) {
1075 Log.e(TAG,"Unable to call disable()",e);
1076 }
1077 }
1078 }
1079 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001080
1081 private boolean checkIfCallerIsForegroundUser() {
1082 int foregroundUser;
1083 int callingUser = UserHandle.getCallingUserId();
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001084 int callingUid = Binder.getCallingUid();
Zhihai Xu40874a02012-10-08 17:57:03 -07001085 long callingIdentity = Binder.clearCallingIdentity();
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001086 int callingAppId = UserHandle.getAppId(callingUid);
Zhihai Xu40874a02012-10-08 17:57:03 -07001087 boolean valid = false;
1088 try {
1089 foregroundUser = ActivityManager.getCurrentUser();
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001090 valid = (callingUser == foregroundUser) ||
1091 callingAppId == Process.NFC_UID;
Zhihai Xu40874a02012-10-08 17:57:03 -07001092 if (DBG) {
1093 Log.d(TAG, "checkIfCallerIsForegroundUser: valid=" + valid
1094 + " callingUser=" + callingUser
1095 + " foregroundUser=" + foregroundUser);
1096 }
1097 } finally {
1098 Binder.restoreCallingIdentity(callingIdentity);
1099 }
1100 return valid;
1101 }
1102
Zhihai Xu40874a02012-10-08 17:57:03 -07001103 private void bluetoothStateChangeHandler(int prevState, int newState) {
1104 if (prevState != newState) {
1105 //Notify all proxy objects first of adapter state change
1106 if (newState == BluetoothAdapter.STATE_ON || newState == BluetoothAdapter.STATE_OFF) {
1107 boolean isUp = (newState==BluetoothAdapter.STATE_ON);
1108 sendBluetoothStateCallback(isUp);
1109
Matthew Xieddf7e472013-03-01 18:41:02 -08001110 if (isUp) {
1111 // connect to GattService
Matthew Xie32ab77b2013-05-08 19:26:57 -07001112 if (mContext.getPackageManager().hasSystemFeature(
1113 PackageManager.FEATURE_BLUETOOTH_LE)) {
1114 Intent i = new Intent(IBluetoothGatt.class.getName());
1115 if (!mContext.bindServiceAsUser(i, mConnection, Context.BIND_AUTO_CREATE,
1116 UserHandle.CURRENT)) {
1117 Log.e(TAG, "Fail to bind to: " + IBluetoothGatt.class.getName());
1118 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001119 }
1120 } else {
1121 //If Bluetooth is off, send service down event to proxy objects, and unbind
1122 if (!isUp && canUnbindBluetoothService()) {
1123 sendBluetoothServiceDownCallback();
1124 unbindAndFinish();
1125 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001126 }
1127 }
1128
1129 //Send broadcast message to everyone else
1130 Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
1131 intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState);
1132 intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState);
1133 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1134 if (DBG) Log.d(TAG,"Bluetooth State Change Intent: " + prevState + " -> " + newState);
1135 mContext.sendBroadcastAsUser(intent, UserHandle.ALL,
1136 BLUETOOTH_PERM);
1137 }
1138 }
1139
1140 /**
1141 * if on is true, wait for state become ON
1142 * if off is true, wait for state become OFF
1143 * if both on and off are false, wait for state not ON
1144 */
1145 private boolean waitForOnOff(boolean on, boolean off) {
1146 int i = 0;
1147 while (i < 10) {
1148 synchronized(mConnection) {
1149 try {
1150 if (mBluetooth == null) break;
1151 if (on) {
1152 if (mBluetooth.getState() == BluetoothAdapter.STATE_ON) return true;
1153 } else if (off) {
1154 if (mBluetooth.getState() == BluetoothAdapter.STATE_OFF) return true;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001155 } else {
Zhihai Xu40874a02012-10-08 17:57:03 -07001156 if (mBluetooth.getState() != BluetoothAdapter.STATE_ON) return true;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001157 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001158 } catch (RemoteException e) {
1159 Log.e(TAG, "getState()", e);
1160 break;
1161 }
1162 }
1163 if (on || off) {
1164 SystemClock.sleep(300);
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001165 } else {
Zhihai Xu40874a02012-10-08 17:57:03 -07001166 SystemClock.sleep(50);
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001167 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001168 i++;
1169 }
1170 Log.e(TAG,"waitForOnOff time out");
1171 return false;
1172 }
Zhihai Xu681ae7f2012-11-12 15:14:18 -08001173
Zhihai Xu401202b2012-12-03 11:36:21 -08001174 private void sendDisableMsg() {
1175 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISABLE));
1176 }
1177
1178 private void sendEnableMsg(boolean quietMode) {
1179 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_ENABLE,
1180 quietMode ? 1 : 0, 0));
1181 }
1182
Zhihai Xu681ae7f2012-11-12 15:14:18 -08001183 private boolean canUnbindBluetoothService() {
1184 synchronized(mConnection) {
1185 //Only unbind with mEnable flag not set
1186 //For race condition: disable and enable back-to-back
1187 //Avoid unbind right after enable due to callback from disable
1188 //Only unbind with Bluetooth at OFF state
1189 //Only unbind without any MESSAGE_BLUETOOTH_STATE_CHANGE message
1190 try {
1191 if (mEnable || (mBluetooth == null)) return false;
1192 if (mHandler.hasMessages(MESSAGE_BLUETOOTH_STATE_CHANGE)) return false;
1193 return (mBluetooth.getState() == BluetoothAdapter.STATE_OFF);
1194 } catch (RemoteException e) {
1195 Log.e(TAG, "getState()", e);
1196 }
1197 }
1198 return false;
1199 }
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001200
1201 private void recoverBluetoothServiceFromError() {
1202 Log.e(TAG,"recoverBluetoothServiceFromError");
1203 synchronized (mConnection) {
1204 if (mBluetooth != null) {
1205 //Unregister callback object
1206 try {
1207 mBluetooth.unregisterCallback(mBluetoothCallback);
1208 } catch (RemoteException re) {
1209 Log.e(TAG, "Unable to unregister",re);
1210 }
1211 }
1212 }
1213
1214 SystemClock.sleep(500);
1215
1216 // disable
1217 handleDisable();
1218
1219 waitForOnOff(false, true);
1220
1221 sendBluetoothServiceDownCallback();
1222 synchronized (mConnection) {
1223 if (mBluetooth != null) {
1224 mBluetooth = null;
1225 //Unbind
1226 mContext.unbindService(mConnection);
1227 }
1228 }
1229
1230 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
1231 mState = BluetoothAdapter.STATE_OFF;
1232
1233 mEnable = false;
1234
1235 if (mErrorRecoveryRetryCounter++ < MAX_ERROR_RESTART_RETRIES) {
1236 // Send a Bluetooth Restart message to reenable bluetooth
1237 Message restartMsg = mHandler.obtainMessage(
1238 MESSAGE_RESTART_BLUETOOTH_SERVICE);
1239 mHandler.sendMessageDelayed(restartMsg, ERROR_RESTART_TIME_MS);
1240 } else {
1241 // todo: notify user to power down and power up phone to make bluetooth work.
1242 }
1243 }
fredc0f420372012-04-12 00:02:00 -07001244}