blob: bea2ccacbdc553ddf241cd444c8e3a3855785040 [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;
Zhihai Xu40874a02012-10-08 17:57:03 -070037import android.os.HandlerThread;
fredc0f420372012-04-12 00:02:00 -070038import android.os.IBinder;
Zhihai Xu40874a02012-10-08 17:57:03 -070039import android.os.Looper;
fredc0f420372012-04-12 00:02:00 -070040import android.os.Message;
Zhihai Xu40874a02012-10-08 17:57:03 -070041import android.os.Process;
fredcd6883532012-04-25 17:46:13 -070042import android.os.RemoteCallbackList;
fredc0f420372012-04-12 00:02:00 -070043import android.os.RemoteException;
Zhihai Xu40874a02012-10-08 17:57:03 -070044import android.os.SystemClock;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070045import android.os.UserHandle;
fredc0f420372012-04-12 00:02:00 -070046import android.provider.Settings;
47import android.util.Log;
fredc0f420372012-04-12 00:02:00 -070048class BluetoothManagerService extends IBluetoothManager.Stub {
49 private static final String TAG = "BluetoothManagerService";
50 private static final boolean DBG = true;
51
fredc0f420372012-04-12 00:02:00 -070052 private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN;
53 private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
fredc0f420372012-04-12 00:02:00 -070054 private static final String ACTION_SERVICE_STATE_CHANGED="com.android.bluetooth.btservice.action.STATE_CHANGED";
55 private static final String EXTRA_ACTION="action";
Zhihai Xud31c3222012-10-31 16:08:57 -070056 private static final String SECURE_SETTINGS_BLUETOOTH_ADDR_VALID="bluetooth_addr_valid";
fredc0f420372012-04-12 00:02:00 -070057 private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address";
58 private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name";
fredc0f420372012-04-12 00:02:00 -070059 private static final int TIMEOUT_BIND_MS = 3000; //Maximum msec to wait for a bind
60 private static final int TIMEOUT_SAVE_MS = 500; //Maximum msec to wait for a save
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +053061 //Maximum msec to wait for service restart
62 private static final int SERVICE_RESTART_TIME_MS = 200;
Zhihai Xudd9d17d2013-01-08 17:05:58 -080063 //Maximum msec to wait for restart due to error
64 private static final int ERROR_RESTART_TIME_MS = 3000;
Zhihai Xu40874a02012-10-08 17:57:03 -070065 //Maximum msec to delay MESSAGE_USER_SWITCHED
66 private static final int USER_SWITCHED_TIME_MS = 200;
fredc0f420372012-04-12 00:02:00 -070067
68 private static final int MESSAGE_ENABLE = 1;
69 private static final int MESSAGE_DISABLE = 2;
fredc649fe492012-04-19 01:07:18 -070070 private static final int MESSAGE_REGISTER_ADAPTER = 20;
71 private static final int MESSAGE_UNREGISTER_ADAPTER = 21;
72 private static final int MESSAGE_REGISTER_STATE_CHANGE_CALLBACK = 30;
73 private static final int MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK = 31;
74 private static final int MESSAGE_BLUETOOTH_SERVICE_CONNECTED = 40;
75 private static final int MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED = 41;
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +053076 private static final int MESSAGE_RESTART_BLUETOOTH_SERVICE = 42;
fredcbf072a72012-05-09 16:52:50 -070077 private static final int MESSAGE_BLUETOOTH_STATE_CHANGE=60;
fredc0f420372012-04-12 00:02:00 -070078 private static final int MESSAGE_TIMEOUT_BIND =100;
79 private static final int MESSAGE_TIMEOUT_UNBIND =101;
80 private static final int MESSAGE_GET_NAME_AND_ADDRESS=200;
81 private static final int MESSAGE_SAVE_NAME_AND_ADDRESS=201;
Zhihai Xu40874a02012-10-08 17:57:03 -070082 private static final int MESSAGE_USER_SWITCHED = 300;
fredc0f420372012-04-12 00:02:00 -070083 private static final int MAX_SAVE_RETRIES=3;
Zhihai Xudd9d17d2013-01-08 17:05:58 -080084 private static final int MAX_ERROR_RESTART_RETRIES=6;
85
Zhihai Xu401202b2012-12-03 11:36:21 -080086 // Bluetooth persisted setting is off
87 private static final int BLUETOOTH_OFF=0;
88 // Bluetooth persisted setting is on
89 // and Airplane mode won't affect Bluetooth state at start up
90 private static final int BLUETOOTH_ON_BLUETOOTH=1;
91 // Bluetooth persisted setting is on
92 // but Airplane mode will affect Bluetooth state at start up
93 // and Airplane mode will have higher priority.
94 private static final int BLUETOOTH_ON_AIRPLANE=2;
fredc0f420372012-04-12 00:02:00 -070095
Matthew Xieddf7e472013-03-01 18:41:02 -080096 private static final int SERVICE_IBLUETOOTH = 1;
97 private static final int SERVICE_IBLUETOOTHGATT = 2;
98
fredc0f420372012-04-12 00:02:00 -070099 private final Context mContext;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700100
101 // Locks are not provided for mName and mAddress.
102 // They are accessed in handler or broadcast receiver, same thread context.
fredc0f420372012-04-12 00:02:00 -0700103 private String mAddress;
104 private String mName;
Matthew Xie6fde3092012-07-11 17:10:07 -0700105 private final ContentResolver mContentResolver;
106 private final RemoteCallbackList<IBluetoothManagerCallback> mCallbacks;
107 private final RemoteCallbackList<IBluetoothStateChangeCallback> mStateChangeCallbacks;
fredc649fe492012-04-19 01:07:18 -0700108 private IBluetooth mBluetooth;
Matthew Xieddf7e472013-03-01 18:41:02 -0800109 private IBluetoothGatt mBluetoothGatt;
fredc649fe492012-04-19 01:07:18 -0700110 private boolean mBinding;
111 private boolean mUnbinding;
Zhihai Xu401202b2012-12-03 11:36:21 -0800112 // used inside handler thread
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700113 private boolean mQuietEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -0800114 // configuarion from external IBinder call which is used to
115 // synchronize with broadcast receiver.
116 private boolean mQuietEnableExternal;
117 // configuarion from external IBinder call which is used to
118 // synchronize with broadcast receiver.
119 private boolean mEnableExternal;
120 // used inside handler thread
Zhihai Xu40874a02012-10-08 17:57:03 -0700121 private boolean mEnable;
122 private int mState;
123 private HandlerThread mThread;
124 private final BluetoothHandler mHandler;
Zhihai Xudd9d17d2013-01-08 17:05:58 -0800125 private int mErrorRecoveryRetryCounter;
fredc0f420372012-04-12 00:02:00 -0700126
fredc649fe492012-04-19 01:07:18 -0700127 private void registerForAirplaneMode(IntentFilter filter) {
128 final ContentResolver resolver = mContext.getContentResolver();
Christopher Tatec09cdce2012-09-10 16:50:14 -0700129 final String airplaneModeRadios = Settings.Global.getString(resolver,
130 Settings.Global.AIRPLANE_MODE_RADIOS);
131 final String toggleableRadios = Settings.Global.getString(resolver,
132 Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
fredc649fe492012-04-19 01:07:18 -0700133 boolean mIsAirplaneSensitive = airplaneModeRadios == null ? true :
Christopher Tatec09cdce2012-09-10 16:50:14 -0700134 airplaneModeRadios.contains(Settings.Global.RADIO_BLUETOOTH);
fredc649fe492012-04-19 01:07:18 -0700135 if (mIsAirplaneSensitive) {
136 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
137 }
138 }
139
fredcbf072a72012-05-09 16:52:50 -0700140 private final IBluetoothCallback mBluetoothCallback = new IBluetoothCallback.Stub() {
141 @Override
142 public void onBluetoothStateChange(int prevState, int newState) throws RemoteException {
143 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_STATE_CHANGE,prevState,newState);
144 mHandler.sendMessage(msg);
145 }
146 };
147
148 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
fredc0f420372012-04-12 00:02:00 -0700149 @Override
150 public void onReceive(Context context, Intent intent) {
151 String action = intent.getAction();
fredcbf072a72012-05-09 16:52:50 -0700152 if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action)) {
fredc0f420372012-04-12 00:02:00 -0700153 String newName = intent.getStringExtra(BluetoothAdapter.EXTRA_LOCAL_NAME);
Freda8c6df02012-07-11 10:25:23 -0700154 if (DBG) Log.d(TAG, "Bluetooth Adapter name changed to " + newName);
fredc0f420372012-04-12 00:02:00 -0700155 if (newName != null) {
156 storeNameAndAddress(newName, null);
157 }
fredc649fe492012-04-19 01:07:18 -0700158 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
Zhihai Xu401202b2012-12-03 11:36:21 -0800159 synchronized(mReceiver) {
160 if (isBluetoothPersistedStateOn()) {
161 if (isAirplaneModeOn()) {
162 persistBluetoothSetting(BLUETOOTH_ON_AIRPLANE);
163 } else {
164 persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);
165 }
166 }
167 if (isAirplaneModeOn()) {
168 // disable without persisting the setting
169 sendDisableMsg();
170 } else if (mEnableExternal) {
171 // enable without persisting the setting
172 sendEnableMsg(mQuietEnableExternal);
173 }
fredc649fe492012-04-19 01:07:18 -0700174 }
Zhihai Xu40874a02012-10-08 17:57:03 -0700175 } else if (Intent.ACTION_USER_SWITCHED.equals(action)) {
176 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_USER_SWITCHED,
177 intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0), 0));
Zhihai Xu401202b2012-12-03 11:36:21 -0800178 } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
179 synchronized(mReceiver) {
180 if (mEnableExternal && isBluetoothPersistedStateOnBluetooth()) {
181 //Enable
182 if (DBG) Log.d(TAG, "Auto-enabling Bluetooth.");
183 sendEnableMsg(mQuietEnableExternal);
184 }
185 }
186
187 if (!isNameAndAddressSet()) {
188 //Sync the Bluetooth name and address from the Bluetooth Adapter
189 if (DBG) Log.d(TAG,"Retrieving Bluetooth Adapter name and address...");
190 getNameAndAddress();
191 }
fredc0f420372012-04-12 00:02:00 -0700192 }
193 }
194 };
195
196 BluetoothManagerService(Context context) {
Zhihai Xu40874a02012-10-08 17:57:03 -0700197 mThread = new HandlerThread("BluetoothManager");
198 mThread.start();
199 mHandler = new BluetoothHandler(mThread.getLooper());
200
fredc0f420372012-04-12 00:02:00 -0700201 mContext = context;
202 mBluetooth = null;
203 mBinding = false;
204 mUnbinding = false;
Zhihai Xu40874a02012-10-08 17:57:03 -0700205 mEnable = false;
206 mState = BluetoothAdapter.STATE_OFF;
Zhihai Xu401202b2012-12-03 11:36:21 -0800207 mQuietEnableExternal = false;
208 mEnableExternal = false;
fredc0f420372012-04-12 00:02:00 -0700209 mAddress = null;
210 mName = null;
Zhihai Xudd9d17d2013-01-08 17:05:58 -0800211 mErrorRecoveryRetryCounter = 0;
fredc0f420372012-04-12 00:02:00 -0700212 mContentResolver = context.getContentResolver();
fredcd6883532012-04-25 17:46:13 -0700213 mCallbacks = new RemoteCallbackList<IBluetoothManagerCallback>();
214 mStateChangeCallbacks = new RemoteCallbackList<IBluetoothStateChangeCallback>();
Zhihai Xu401202b2012-12-03 11:36:21 -0800215 IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
Matthew Xie6fde3092012-07-11 17:10:07 -0700216 filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
Zhihai Xu40874a02012-10-08 17:57:03 -0700217 filter.addAction(Intent.ACTION_USER_SWITCHED);
Matthew Xie6fde3092012-07-11 17:10:07 -0700218 registerForAirplaneMode(filter);
219 mContext.registerReceiver(mReceiver, filter);
fredc0f420372012-04-12 00:02:00 -0700220 loadStoredNameAndAddress();
Zhihai Xu401202b2012-12-03 11:36:21 -0800221 if (isBluetoothPersistedStateOn()) {
222 mEnableExternal = true;
fredc0f420372012-04-12 00:02:00 -0700223 }
224 }
225
fredc649fe492012-04-19 01:07:18 -0700226 /**
227 * Returns true if airplane mode is currently on
228 */
229 private final boolean isAirplaneModeOn() {
Christopher Tatec09cdce2012-09-10 16:50:14 -0700230 return Settings.Global.getInt(mContext.getContentResolver(),
231 Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
fredc649fe492012-04-19 01:07:18 -0700232 }
233
234 /**
235 * Returns true if the Bluetooth saved state is "on"
236 */
237 private final boolean isBluetoothPersistedStateOn() {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700238 return Settings.Global.getInt(mContentResolver,
Zhihai Xu401202b2012-12-03 11:36:21 -0800239 Settings.Global.BLUETOOTH_ON, 0) != BLUETOOTH_OFF;
240 }
241
242 /**
243 * Returns true if the Bluetooth saved state is BLUETOOTH_ON_BLUETOOTH
244 */
245 private final boolean isBluetoothPersistedStateOnBluetooth() {
246 return Settings.Global.getInt(mContentResolver,
247 Settings.Global.BLUETOOTH_ON, 0) == BLUETOOTH_ON_BLUETOOTH;
fredc649fe492012-04-19 01:07:18 -0700248 }
249
250 /**
251 * Save the Bluetooth on/off state
252 *
253 */
Zhihai Xu401202b2012-12-03 11:36:21 -0800254 private void persistBluetoothSetting(int value) {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700255 Settings.Global.putInt(mContext.getContentResolver(),
256 Settings.Global.BLUETOOTH_ON,
Zhihai Xu401202b2012-12-03 11:36:21 -0800257 value);
fredc649fe492012-04-19 01:07:18 -0700258 }
259
260 /**
261 * Returns true if the Bluetooth Adapter's name and address is
262 * locally cached
263 * @return
264 */
fredc0f420372012-04-12 00:02:00 -0700265 private boolean isNameAndAddressSet() {
266 return mName !=null && mAddress!= null && mName.length()>0 && mAddress.length()>0;
267 }
268
fredc649fe492012-04-19 01:07:18 -0700269 /**
270 * Retrieve the Bluetooth Adapter's name and address and save it in
271 * in the local cache
272 */
fredc0f420372012-04-12 00:02:00 -0700273 private void loadStoredNameAndAddress() {
274 if (DBG) Log.d(TAG, "Loading stored name and address");
Zhihai Xud31c3222012-10-31 16:08:57 -0700275 if (mContext.getResources().getBoolean
276 (com.android.internal.R.bool.config_bluetooth_address_validation) &&
277 Settings.Secure.getInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 0) == 0) {
278 // if the valid flag is not set, don't load the address and name
279 if (DBG) Log.d(TAG, "invalid bluetooth name and address stored");
280 return;
281 }
fredc0f420372012-04-12 00:02:00 -0700282 mName = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME);
283 mAddress = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS);
Zhihai Xud31c3222012-10-31 16:08:57 -0700284 if (DBG) Log.d(TAG, "Stored bluetooth Name=" + mName + ",Address=" + mAddress);
fredc0f420372012-04-12 00:02:00 -0700285 }
286
fredc649fe492012-04-19 01:07:18 -0700287 /**
288 * Save the Bluetooth name and address in the persistent store.
289 * Only non-null values will be saved.
290 * @param name
291 * @param address
292 */
fredc0f420372012-04-12 00:02:00 -0700293 private void storeNameAndAddress(String name, String address) {
294 if (name != null) {
295 Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME, name);
fredc0f420372012-04-12 00:02:00 -0700296 mName = name;
fredc649fe492012-04-19 01:07:18 -0700297 if (DBG) Log.d(TAG,"Stored Bluetooth name: " +
298 Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_NAME));
fredc0f420372012-04-12 00:02:00 -0700299 }
300
301 if (address != null) {
302 Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS, address);
fredc0f420372012-04-12 00:02:00 -0700303 mAddress=address;
fredc649fe492012-04-19 01:07:18 -0700304 if (DBG) Log.d(TAG,"Stored Bluetoothaddress: " +
305 Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_ADDRESS));
fredc0f420372012-04-12 00:02:00 -0700306 }
Zhihai Xud31c3222012-10-31 16:08:57 -0700307
308 if ((name != null) && (address != null)) {
309 Settings.Secure.putInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 1);
310 }
fredc0f420372012-04-12 00:02:00 -0700311 }
312
313 public IBluetooth registerAdapter(IBluetoothManagerCallback callback){
fredc0f420372012-04-12 00:02:00 -0700314 Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_ADAPTER);
315 msg.obj = callback;
316 mHandler.sendMessage(msg);
317 synchronized(mConnection) {
318 return mBluetooth;
319 }
320 }
321
322 public void unregisterAdapter(IBluetoothManagerCallback callback) {
323 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
324 "Need BLUETOOTH permission");
325 Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_ADAPTER);
326 msg.obj = callback;
327 mHandler.sendMessage(msg);
328 }
329
330 public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) {
331 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
332 "Need BLUETOOTH permission");
333 Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_STATE_CHANGE_CALLBACK);
334 msg.obj = callback;
335 mHandler.sendMessage(msg);
336 }
337
338 public void unregisterStateChangeCallback(IBluetoothStateChangeCallback callback) {
339 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
340 "Need BLUETOOTH permission");
341 Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK);
342 msg.obj = callback;
343 mHandler.sendMessage(msg);
344 }
345
346 public boolean isEnabled() {
Zhihai Xu6eb76522012-11-29 15:41:04 -0800347 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
348 (!checkIfCallerIsForegroundUser())) {
349 Log.w(TAG,"isEnabled(): not allowed for non-active and non system user");
Zhihai Xu40874a02012-10-08 17:57:03 -0700350 return false;
351 }
352
fredc0f420372012-04-12 00:02:00 -0700353 synchronized(mConnection) {
354 try {
355 return (mBluetooth != null && mBluetooth.isEnabled());
356 } catch (RemoteException e) {
357 Log.e(TAG, "isEnabled()", e);
358 }
359 }
360 return false;
361 }
362
363 public void getNameAndAddress() {
fredcf2458862012-04-16 15:18:27 -0700364 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700365 Log.d(TAG,"getNameAndAddress(): mBluetooth = " + mBluetooth +
366 " mBinding = " + mBinding);
fredcf2458862012-04-16 15:18:27 -0700367 }
fredc0f420372012-04-12 00:02:00 -0700368 Message msg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
369 mHandler.sendMessage(msg);
370 }
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700371 public boolean enableNoAutoConnect()
372 {
373 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
374 "Need BLUETOOTH ADMIN permission");
Zhihai Xu40874a02012-10-08 17:57:03 -0700375
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700376 if (DBG) {
377 Log.d(TAG,"enableNoAutoConnect(): mBluetooth =" + mBluetooth +
378 " mBinding = " + mBinding);
379 }
Martijn Coenen8385c5a2012-11-29 10:14:16 -0800380 int callingAppId = UserHandle.getAppId(Binder.getCallingUid());
381
382 if (callingAppId != Process.NFC_UID) {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700383 throw new SecurityException("no permission to enable Bluetooth quietly");
384 }
Martijn Coenen8385c5a2012-11-29 10:14:16 -0800385
Zhihai Xu401202b2012-12-03 11:36:21 -0800386 synchronized(mReceiver) {
387 mQuietEnableExternal = true;
388 mEnableExternal = true;
389 sendEnableMsg(true);
390 }
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700391 return true;
392
393 }
fredc0f420372012-04-12 00:02:00 -0700394 public boolean enable() {
Zhihai Xu6eb76522012-11-29 15:41:04 -0800395 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
396 (!checkIfCallerIsForegroundUser())) {
397 Log.w(TAG,"enable(): not allowed for non-active and non system user");
Zhihai Xu40874a02012-10-08 17:57:03 -0700398 return false;
fredcf2458862012-04-16 15:18:27 -0700399 }
400
Zhihai Xu401202b2012-12-03 11:36:21 -0800401 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
402 "Need BLUETOOTH ADMIN permission");
403 if (DBG) {
404 Log.d(TAG,"enable(): mBluetooth =" + mBluetooth +
405 " mBinding = " + mBinding);
406 }
407
408 synchronized(mReceiver) {
409 mQuietEnableExternal = false;
410 mEnableExternal = true;
411 // waive WRITE_SECURE_SETTINGS permission check
412 long callingIdentity = Binder.clearCallingIdentity();
413 persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);
414 Binder.restoreCallingIdentity(callingIdentity);
415 sendEnableMsg(false);
416 }
417 return true;
fredc0f420372012-04-12 00:02:00 -0700418 }
419
420 public boolean disable(boolean persist) {
421 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
422 "Need BLUETOOTH ADMIN permissicacheNameAndAddresson");
Zhihai Xu40874a02012-10-08 17:57:03 -0700423
Zhihai Xu6eb76522012-11-29 15:41:04 -0800424 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
425 (!checkIfCallerIsForegroundUser())) {
426 Log.w(TAG,"disable(): not allowed for non-active and non system user");
Zhihai Xu40874a02012-10-08 17:57:03 -0700427 return false;
428 }
429
fredcf2458862012-04-16 15:18:27 -0700430 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700431 Log.d(TAG,"disable(): mBluetooth = " + mBluetooth +
432 " mBinding = " + mBinding);
433 }
fredcf2458862012-04-16 15:18:27 -0700434
Zhihai Xu401202b2012-12-03 11:36:21 -0800435 synchronized(mReceiver) {
436 if (persist) {
437 // waive WRITE_SECURE_SETTINGS permission check
438 long callingIdentity = Binder.clearCallingIdentity();
439 persistBluetoothSetting(BLUETOOTH_OFF);
440 Binder.restoreCallingIdentity(callingIdentity);
441 }
442 mEnableExternal = false;
443 sendDisableMsg();
444 }
fredc0f420372012-04-12 00:02:00 -0700445 return true;
446 }
447
fredc649fe492012-04-19 01:07:18 -0700448 public void unbindAndFinish() {
fredcf2458862012-04-16 15:18:27 -0700449 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700450 Log.d(TAG,"unbindAndFinish(): " + mBluetooth +
451 " mBinding = " + mBinding);
fredcf2458862012-04-16 15:18:27 -0700452 }
453
fredc0f420372012-04-12 00:02:00 -0700454 synchronized (mConnection) {
455 if (mUnbinding) return;
456 mUnbinding = true;
Zhihai Xu40874a02012-10-08 17:57:03 -0700457 if (mBluetooth != null) {
fredcbf072a72012-05-09 16:52:50 -0700458 if (!mConnection.isGetNameAddressOnly()) {
459 //Unregister callback object
460 try {
461 mBluetooth.unregisterCallback(mBluetoothCallback);
462 } catch (RemoteException re) {
Zhihai Xu40874a02012-10-08 17:57:03 -0700463 Log.e(TAG, "Unable to unregister BluetoothCallback",re);
fredcbf072a72012-05-09 16:52:50 -0700464 }
465 }
fredc0f420372012-04-12 00:02:00 -0700466 if (DBG) Log.d(TAG, "Sending unbind request.");
fredcd6883532012-04-25 17:46:13 -0700467 mBluetooth = null;
468 //Unbind
fredc0f420372012-04-12 00:02:00 -0700469 mContext.unbindService(mConnection);
fredcd6883532012-04-25 17:46:13 -0700470 mUnbinding = false;
Zhihai Xu40874a02012-10-08 17:57:03 -0700471 mBinding = false;
fredcf2458862012-04-16 15:18:27 -0700472 } else {
473 mUnbinding=false;
fredc0f420372012-04-12 00:02:00 -0700474 }
475 }
476 }
477
Matthew Xieddf7e472013-03-01 18:41:02 -0800478 public IBluetoothGatt getBluetoothGatt() {
479 // sync protection
480 return mBluetoothGatt;
481 }
482
fredcbf072a72012-05-09 16:52:50 -0700483 private void sendBluetoothStateCallback(boolean isUp) {
484 int n = mStateChangeCallbacks.beginBroadcast();
Freda8c6df02012-07-11 10:25:23 -0700485 if (DBG) Log.d(TAG,"Broadcasting onBluetoothStateChange("+isUp+") to " + n + " receivers.");
fredcbf072a72012-05-09 16:52:50 -0700486 for (int i=0; i <n;i++) {
487 try {
488 mStateChangeCallbacks.getBroadcastItem(i).onBluetoothStateChange(isUp);
489 } catch (RemoteException e) {
490 Log.e(TAG, "Unable to call onBluetoothStateChange() on callback #" + i , e);
491 }
492 }
493 mStateChangeCallbacks.finishBroadcast();
494 }
495
496 /**
Zhihai Xu40874a02012-10-08 17:57:03 -0700497 * Inform BluetoothAdapter instances that Adapter service is up
498 */
499 private void sendBluetoothServiceUpCallback() {
500 if (!mConnection.isGetNameAddressOnly()) {
501 if (DBG) Log.d(TAG,"Calling onBluetoothServiceUp callbacks");
502 int n = mCallbacks.beginBroadcast();
503 Log.d(TAG,"Broadcasting onBluetoothServiceUp() to " + n + " receivers.");
504 for (int i=0; i <n;i++) {
505 try {
506 mCallbacks.getBroadcastItem(i).onBluetoothServiceUp(mBluetooth);
507 } catch (RemoteException e) {
508 Log.e(TAG, "Unable to call onBluetoothServiceUp() on callback #" + i, e);
509 }
510 }
511 mCallbacks.finishBroadcast();
512 }
513 }
514 /**
fredcbf072a72012-05-09 16:52:50 -0700515 * Inform BluetoothAdapter instances that Adapter service is down
516 */
517 private void sendBluetoothServiceDownCallback() {
fredcd6883532012-04-25 17:46:13 -0700518 if (!mConnection.isGetNameAddressOnly()) {
519 if (DBG) Log.d(TAG,"Calling onBluetoothServiceDown callbacks");
520 int n = mCallbacks.beginBroadcast();
521 Log.d(TAG,"Broadcasting onBluetoothServiceDown() to " + n + " receivers.");
522 for (int i=0; i <n;i++) {
523 try {
524 mCallbacks.getBroadcastItem(i).onBluetoothServiceDown();
525 } catch (RemoteException e) {
526 Log.e(TAG, "Unable to call onBluetoothServiceDown() on callback #" + i, e);
527 }
528 }
529 mCallbacks.finishBroadcast();
530 }
531 }
fredc0f420372012-04-12 00:02:00 -0700532 public String getAddress() {
Matthew Xieaf5ddbf2012-12-04 10:47:43 -0800533 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
534 "Need BLUETOOTH permission");
Zhihai Xu40874a02012-10-08 17:57:03 -0700535
Zhihai Xu6eb76522012-11-29 15:41:04 -0800536 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
537 (!checkIfCallerIsForegroundUser())) {
538 Log.w(TAG,"getAddress(): not allowed for non-active and non system user");
539 return null;
Zhihai Xu40874a02012-10-08 17:57:03 -0700540 }
541
fredc116d1d462012-04-20 14:47:08 -0700542 synchronized(mConnection) {
543 if (mBluetooth != null) {
544 try {
545 return mBluetooth.getAddress();
546 } catch (RemoteException e) {
547 Log.e(TAG, "getAddress(): Unable to retrieve address remotely..Returning cached address",e);
548 }
549 }
550 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700551 // mAddress is accessed from outside.
552 // It is alright without a lock. Here, bluetooth is off, no other thread is
553 // changing mAddress
fredc0f420372012-04-12 00:02:00 -0700554 return mAddress;
555 }
fredc649fe492012-04-19 01:07:18 -0700556
fredc0f420372012-04-12 00:02:00 -0700557 public String getName() {
Matthew Xieaf5ddbf2012-12-04 10:47:43 -0800558 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
559 "Need BLUETOOTH permission");
Zhihai Xu40874a02012-10-08 17:57:03 -0700560
Zhihai Xu6eb76522012-11-29 15:41:04 -0800561 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
562 (!checkIfCallerIsForegroundUser())) {
563 Log.w(TAG,"getName(): not allowed for non-active and non system user");
564 return null;
Zhihai Xu40874a02012-10-08 17:57:03 -0700565 }
566
fredc116d1d462012-04-20 14:47:08 -0700567 synchronized(mConnection) {
568 if (mBluetooth != null) {
569 try {
570 return mBluetooth.getName();
571 } catch (RemoteException e) {
572 Log.e(TAG, "getName(): Unable to retrieve name remotely..Returning cached name",e);
573 }
574 }
575 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700576 // mName is accessed from outside.
577 // It alright without a lock. Here, bluetooth is off, no other thread is
578 // changing mName
fredc0f420372012-04-12 00:02:00 -0700579 return mName;
580 }
581
fredc0f420372012-04-12 00:02:00 -0700582 private class BluetoothServiceConnection implements ServiceConnection {
583
584 private boolean mGetNameAddressOnly;
585
586 public void setGetNameAddressOnly(boolean getOnly) {
587 mGetNameAddressOnly = getOnly;
588 }
589
590 public boolean isGetNameAddressOnly() {
591 return mGetNameAddressOnly;
592 }
593
594 public void onServiceConnected(ComponentName className, IBinder service) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800595 if (DBG) Log.d(TAG, "BluetoothServiceConnection: " + className.getClassName());
fredc0f420372012-04-12 00:02:00 -0700596 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED);
Matthew Xieddf7e472013-03-01 18:41:02 -0800597 // TBD if (className.getClassName().equals(IBluetooth.class.getName())) {
598 if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) {
599 msg.arg1 = SERVICE_IBLUETOOTH;
600 // } else if (className.getClassName().equals(IBluetoothGatt.class.getName())) {
601 } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) {
602 msg.arg1 = SERVICE_IBLUETOOTHGATT;
603 } else {
604 Log.e(TAG, "Unknown service connected: " + className.getClassName());
605 return;
606 }
fredc0f420372012-04-12 00:02:00 -0700607 msg.obj = service;
608 mHandler.sendMessage(msg);
609 }
610
611 public void onServiceDisconnected(ComponentName className) {
fredc0f420372012-04-12 00:02:00 -0700612 // Called if we unexpected disconnected.
Matthew Xieddf7e472013-03-01 18:41:02 -0800613 if (DBG) Log.d(TAG, "BluetoothServiceConnection, disconnected: " +
614 className.getClassName());
fredc0f420372012-04-12 00:02:00 -0700615 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED);
Matthew Xieddf7e472013-03-01 18:41:02 -0800616 if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) {
617 msg.arg1 = SERVICE_IBLUETOOTH;
618 } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) {
619 msg.arg1 = SERVICE_IBLUETOOTHGATT;
620 } else {
621 Log.e(TAG, "Unknown service disconnected: " + className.getClassName());
622 return;
623 }
fredc0f420372012-04-12 00:02:00 -0700624 mHandler.sendMessage(msg);
625 }
626 }
627
628 private BluetoothServiceConnection mConnection = new BluetoothServiceConnection();
629
Zhihai Xu40874a02012-10-08 17:57:03 -0700630 private class BluetoothHandler extends Handler {
631 public BluetoothHandler(Looper looper) {
632 super(looper);
633 }
634
fredc0f420372012-04-12 00:02:00 -0700635 @Override
636 public void handleMessage(Message msg) {
637 if (DBG) Log.d (TAG, "Message: " + msg.what);
fredc0f420372012-04-12 00:02:00 -0700638 switch (msg.what) {
639 case MESSAGE_GET_NAME_AND_ADDRESS: {
fredc649fe492012-04-19 01:07:18 -0700640 if (DBG) Log.d(TAG,"MESSAGE_GET_NAME_AND_ADDRESS");
Matthew Xiecdce0b92012-07-12 19:06:15 -0700641 synchronized(mConnection) {
fredc0f420372012-04-12 00:02:00 -0700642 //Start bind request
Zhihai Xu40874a02012-10-08 17:57:03 -0700643 if ((mBluetooth == null) && (!mBinding)) {
fredc0f420372012-04-12 00:02:00 -0700644 if (DBG) Log.d(TAG, "Binding to service to get name and address");
645 mConnection.setGetNameAddressOnly(true);
646 //Start bind timeout and bind
647 Message timeoutMsg = mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
648 mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS);
649 Intent i = new Intent(IBluetooth.class.getName());
Amith Yamasani27b89e62013-01-16 12:30:11 -0800650 if (!mContext.bindServiceAsUser(i, mConnection,
651 Context.BIND_AUTO_CREATE, UserHandle.CURRENT)) {
fredc0f420372012-04-12 00:02:00 -0700652 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
653 Log.e(TAG, "fail to bind to: " + IBluetooth.class.getName());
Zhihai Xu40874a02012-10-08 17:57:03 -0700654 } else {
655 mBinding = true;
fredc0f420372012-04-12 00:02:00 -0700656 }
657 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700658 else {
659 Message saveMsg= mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS);
Zhihai Xu40874a02012-10-08 17:57:03 -0700660 saveMsg.arg1 = 0;
661 if (mBluetooth != null) {
662 mHandler.sendMessage(saveMsg);
663 } else {
664 // if enable is also called to bind the service
665 // wait for MESSAGE_BLUETOOTH_SERVICE_CONNECTED
666 mHandler.sendMessageDelayed(saveMsg, TIMEOUT_SAVE_MS);
667 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700668 }
fredc0f420372012-04-12 00:02:00 -0700669 }
fredc649fe492012-04-19 01:07:18 -0700670 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700671 }
fredc0f420372012-04-12 00:02:00 -0700672 case MESSAGE_SAVE_NAME_AND_ADDRESS: {
Zhihai Xud31c3222012-10-31 16:08:57 -0700673 boolean unbind = false;
fredc649fe492012-04-19 01:07:18 -0700674 if (DBG) Log.d(TAG,"MESSAGE_SAVE_NAME_AND_ADDRESS");
Matthew Xiecdce0b92012-07-12 19:06:15 -0700675 synchronized(mConnection) {
Zhihai Xud31c3222012-10-31 16:08:57 -0700676 if (!mEnable && mBluetooth != null) {
677 try {
678 mBluetooth.enable();
679 } catch (RemoteException e) {
680 Log.e(TAG,"Unable to call enable()",e);
681 }
682 }
683 }
684 if (mBluetooth != null) waitForOnOff(true, false);
685 synchronized(mConnection) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700686 if (mBluetooth != null) {
687 String name = null;
688 String address = null;
689 try {
690 name = mBluetooth.getName();
691 address = mBluetooth.getAddress();
692 } catch (RemoteException re) {
693 Log.e(TAG,"",re);
694 }
fredc0f420372012-04-12 00:02:00 -0700695
Matthew Xiecdce0b92012-07-12 19:06:15 -0700696 if (name != null && address != null) {
697 storeNameAndAddress(name,address);
Zhihai Xu40874a02012-10-08 17:57:03 -0700698 if (mConnection.isGetNameAddressOnly()) {
Zhihai Xud31c3222012-10-31 16:08:57 -0700699 unbind = true;
Zhihai Xu40874a02012-10-08 17:57:03 -0700700 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700701 } else {
702 if (msg.arg1 < MAX_SAVE_RETRIES) {
703 Message retryMsg = mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS);
704 retryMsg.arg1= 1+msg.arg1;
705 if (DBG) Log.d(TAG,"Retrying name/address remote retrieval and save.....Retry count =" + retryMsg.arg1);
706 mHandler.sendMessageDelayed(retryMsg, TIMEOUT_SAVE_MS);
707 } else {
708 Log.w(TAG,"Maximum name/address remote retrieval retry exceeded");
Zhihai Xu40874a02012-10-08 17:57:03 -0700709 if (mConnection.isGetNameAddressOnly()) {
Zhihai Xud31c3222012-10-31 16:08:57 -0700710 unbind = true;
Zhihai Xu40874a02012-10-08 17:57:03 -0700711 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700712 }
fredc0f420372012-04-12 00:02:00 -0700713 }
Zhihai Xud31c3222012-10-31 16:08:57 -0700714 if (!mEnable) {
715 try {
716 mBluetooth.disable();
717 } catch (RemoteException e) {
718 Log.e(TAG,"Unable to call disable()",e);
719 }
720 }
Zhihai Xu40874a02012-10-08 17:57:03 -0700721 } else {
722 // rebind service by Request GET NAME AND ADDRESS
723 // if service is unbinded by disable or
724 // MESSAGE_BLUETOOTH_SERVICE_CONNECTED is not received
725 Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
726 mHandler.sendMessage(getMsg);
fredc0f420372012-04-12 00:02:00 -0700727 }
728 }
Zhihai Xud31c3222012-10-31 16:08:57 -0700729 if (!mEnable && mBluetooth != null) waitForOnOff(false, true);
730 if (unbind) {
731 unbindAndFinish();
732 }
fredc649fe492012-04-19 01:07:18 -0700733 break;
fredc649fe492012-04-19 01:07:18 -0700734 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700735 case MESSAGE_ENABLE:
fredcf2458862012-04-16 15:18:27 -0700736 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700737 Log.d(TAG, "MESSAGE_ENABLE: mBluetooth = " + mBluetooth);
fredc649fe492012-04-19 01:07:18 -0700738 }
Zhihai Xu40874a02012-10-08 17:57:03 -0700739 mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
740 mEnable = true;
Zhihai Xu401202b2012-12-03 11:36:21 -0800741 handleEnable(msg.arg1 == 1);
fredc649fe492012-04-19 01:07:18 -0700742 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700743
fredc0f420372012-04-12 00:02:00 -0700744 case MESSAGE_DISABLE:
Zhihai Xu40874a02012-10-08 17:57:03 -0700745 mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
746 if (mEnable && mBluetooth != null) {
747 waitForOnOff(true, false);
748 mEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -0800749 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -0700750 waitForOnOff(false, false);
751 } else {
752 mEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -0800753 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -0700754 }
fredc0f420372012-04-12 00:02:00 -0700755 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700756
fredc0f420372012-04-12 00:02:00 -0700757 case MESSAGE_REGISTER_ADAPTER:
758 {
759 IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -0700760 boolean added = mCallbacks.register(callback);
761 Log.d(TAG,"Added callback: " + (callback == null? "null": callback) +":" +added );
fredc0f420372012-04-12 00:02:00 -0700762 }
763 break;
764 case MESSAGE_UNREGISTER_ADAPTER:
765 {
766 IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -0700767 boolean removed = mCallbacks.unregister(callback);
768 Log.d(TAG,"Removed callback: " + (callback == null? "null": callback) +":" + removed);
fredc0f420372012-04-12 00:02:00 -0700769 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700770 }
fredc0f420372012-04-12 00:02:00 -0700771 case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK:
772 {
773 IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -0700774 mStateChangeCallbacks.register(callback);
fredc0f420372012-04-12 00:02:00 -0700775 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700776 }
fredc0f420372012-04-12 00:02:00 -0700777 case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK:
778 {
779 IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -0700780 mStateChangeCallbacks.unregister(callback);
fredc0f420372012-04-12 00:02:00 -0700781 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700782 }
fredc0f420372012-04-12 00:02:00 -0700783 case MESSAGE_BLUETOOTH_SERVICE_CONNECTED:
784 {
Matthew Xieddf7e472013-03-01 18:41:02 -0800785 if (DBG) Log.d(TAG,"MESSAGE_BLUETOOTH_SERVICE_CONNECTED: " + msg.arg1);
fredc0f420372012-04-12 00:02:00 -0700786
787 IBinder service = (IBinder) msg.obj;
788 synchronized(mConnection) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800789 if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
790 mBluetoothGatt = IBluetoothGatt.Stub.asInterface(service);
791 break;
792 } // else must be SERVICE_IBLUETOOTH
793
794 //Remove timeout
795 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
796
fredc0f420372012-04-12 00:02:00 -0700797 mBinding = false;
798 mBluetooth = IBluetooth.Stub.asInterface(service);
fredc0f420372012-04-12 00:02:00 -0700799
Matthew Xiecdce0b92012-07-12 19:06:15 -0700800 if (mConnection.isGetNameAddressOnly()) {
801 //Request GET NAME AND ADDRESS
802 Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
803 mHandler.sendMessage(getMsg);
Zhihai Xu40874a02012-10-08 17:57:03 -0700804 if (!mEnable) return;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700805 }
fredc0f420372012-04-12 00:02:00 -0700806
Zhihai Xu40874a02012-10-08 17:57:03 -0700807 mConnection.setGetNameAddressOnly(false);
Matthew Xiecdce0b92012-07-12 19:06:15 -0700808 //Register callback object
fredcbf072a72012-05-09 16:52:50 -0700809 try {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700810 mBluetooth.registerCallback(mBluetoothCallback);
811 } catch (RemoteException re) {
812 Log.e(TAG, "Unable to register BluetoothCallback",re);
fredcbf072a72012-05-09 16:52:50 -0700813 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700814 //Inform BluetoothAdapter instances that service is up
Zhihai Xu40874a02012-10-08 17:57:03 -0700815 sendBluetoothServiceUpCallback();
816
Matthew Xiecdce0b92012-07-12 19:06:15 -0700817 //Do enable request
818 try {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700819 if (mQuietEnable == false) {
820 if(!mBluetooth.enable()) {
821 Log.e(TAG,"IBluetooth.enable() returned false");
822 }
823 }
824 else
825 {
826 if(!mBluetooth.enableNoAutoConnect()) {
827 Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false");
828 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700829 }
830 } catch (RemoteException e) {
831 Log.e(TAG,"Unable to call enable()",e);
832 }
Freda8c6df02012-07-11 10:25:23 -0700833 }
Zhihai Xu40874a02012-10-08 17:57:03 -0700834
835 if (!mEnable) {
836 waitForOnOff(true, false);
Zhihai Xu401202b2012-12-03 11:36:21 -0800837 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -0700838 waitForOnOff(false, false);
839 }
fredc649fe492012-04-19 01:07:18 -0700840 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700841 }
fredc649fe492012-04-19 01:07:18 -0700842 case MESSAGE_TIMEOUT_BIND: {
843 Log.e(TAG, "MESSAGE_TIMEOUT_BIND");
fredc0f420372012-04-12 00:02:00 -0700844 synchronized(mConnection) {
845 mBinding = false;
846 }
fredc649fe492012-04-19 01:07:18 -0700847 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700848 }
fredcbf072a72012-05-09 16:52:50 -0700849 case MESSAGE_BLUETOOTH_STATE_CHANGE:
fredc0f420372012-04-12 00:02:00 -0700850 {
fredcbf072a72012-05-09 16:52:50 -0700851 int prevState = msg.arg1;
852 int newState = msg.arg2;
853 if (DBG) Log.d(TAG, "MESSAGE_BLUETOOTH_STATE_CHANGE: prevState = " + prevState + ", newState=" + newState);
Zhihai Xu40874a02012-10-08 17:57:03 -0700854 mState = newState;
855 bluetoothStateChangeHandler(prevState, newState);
Zhihai Xudd9d17d2013-01-08 17:05:58 -0800856 // handle error state transition case from TURNING_ON to OFF
857 // unbind and rebind bluetooth service and enable bluetooth
858 if ((prevState == BluetoothAdapter.STATE_TURNING_ON) &&
859 (newState == BluetoothAdapter.STATE_OFF) &&
860 (mBluetooth != null) && mEnable) {
861 recoverBluetoothServiceFromError();
862 }
863 if (newState == BluetoothAdapter.STATE_ON) {
864 // bluetooth is working, reset the counter
865 if (mErrorRecoveryRetryCounter != 0) {
866 Log.w(TAG, "bluetooth is recovered from error");
867 mErrorRecoveryRetryCounter = 0;
868 }
869 }
fredc649fe492012-04-19 01:07:18 -0700870 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700871 }
fredc0f420372012-04-12 00:02:00 -0700872 case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED:
873 {
Matthew Xieddf7e472013-03-01 18:41:02 -0800874 Log.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: " + msg.arg1);
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +0530875 synchronized(mConnection) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800876 if (msg.arg1 == SERVICE_IBLUETOOTH) {
877 // if service is unbinded already, do nothing and return
878 if (mBluetooth == null) break;
879 mBluetooth = null;
880 } else if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
881 mBluetoothGatt = null;
882 break;
883 } else {
884 Log.e(TAG, "Bad msg.arg1: " + msg.arg1);
885 break;
886 }
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +0530887 }
Zhihai Xu40874a02012-10-08 17:57:03 -0700888
889 if (mEnable) {
890 mEnable = false;
891 // Send a Bluetooth Restart message
892 Message restartMsg = mHandler.obtainMessage(
893 MESSAGE_RESTART_BLUETOOTH_SERVICE);
894 mHandler.sendMessageDelayed(restartMsg,
895 SERVICE_RESTART_TIME_MS);
896 }
897
898 if (!mConnection.isGetNameAddressOnly()) {
899 sendBluetoothServiceDownCallback();
900
901 // Send BT state broadcast to update
902 // the BT icon correctly
Zhihai Xu4e22ad32012-11-13 15:11:26 -0800903 if ((mState == BluetoothAdapter.STATE_TURNING_ON) ||
904 (mState == BluetoothAdapter.STATE_ON)) {
905 bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,
906 BluetoothAdapter.STATE_TURNING_OFF);
907 mState = BluetoothAdapter.STATE_TURNING_OFF;
908 }
909 if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
910 bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
911 BluetoothAdapter.STATE_OFF);
912 }
913
914 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
Zhihai Xu40874a02012-10-08 17:57:03 -0700915 mState = BluetoothAdapter.STATE_OFF;
916 }
fredc649fe492012-04-19 01:07:18 -0700917 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700918 }
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +0530919 case MESSAGE_RESTART_BLUETOOTH_SERVICE:
920 {
921 Log.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE:"
922 +" Restart IBluetooth service");
923 /* Enable without persisting the setting as
924 it doesnt change when IBluetooth
925 service restarts */
Zhihai Xu40874a02012-10-08 17:57:03 -0700926 mEnable = true;
Zhihai Xu401202b2012-12-03 11:36:21 -0800927 handleEnable(mQuietEnable);
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +0530928 break;
929 }
930
fredc0f420372012-04-12 00:02:00 -0700931 case MESSAGE_TIMEOUT_UNBIND:
932 {
fredc649fe492012-04-19 01:07:18 -0700933 Log.e(TAG, "MESSAGE_TIMEOUT_UNBIND");
fredc0f420372012-04-12 00:02:00 -0700934 synchronized(mConnection) {
935 mUnbinding = false;
936 }
fredc649fe492012-04-19 01:07:18 -0700937 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700938 }
Zhihai Xu40874a02012-10-08 17:57:03 -0700939
940 case MESSAGE_USER_SWITCHED:
941 {
942 if (DBG) {
943 Log.d(TAG, "MESSAGE_USER_SWITCHED");
944 }
945 mHandler.removeMessages(MESSAGE_USER_SWITCHED);
946 /* disable and enable BT when detect a user switch */
947 if (mEnable && mBluetooth != null) {
948 synchronized (mConnection) {
949 if (mBluetooth != null) {
950 //Unregister callback object
951 try {
952 mBluetooth.unregisterCallback(mBluetoothCallback);
953 } catch (RemoteException re) {
954 Log.e(TAG, "Unable to unregister",re);
955 }
956 }
957 }
Zhihai Xu4e22ad32012-11-13 15:11:26 -0800958
959 if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
960 // MESSAGE_USER_SWITCHED happened right after MESSAGE_ENABLE
961 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_OFF);
962 mState = BluetoothAdapter.STATE_OFF;
963 }
964 if (mState == BluetoothAdapter.STATE_OFF) {
965 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_TURNING_ON);
966 mState = BluetoothAdapter.STATE_TURNING_ON;
967 }
Zhihai Xu40874a02012-10-08 17:57:03 -0700968
969 waitForOnOff(true, false);
970
Zhihai Xu4e22ad32012-11-13 15:11:26 -0800971 if (mState == BluetoothAdapter.STATE_TURNING_ON) {
972 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_ON);
973 }
Zhihai Xu40874a02012-10-08 17:57:03 -0700974
975 // disable
Zhihai Xu401202b2012-12-03 11:36:21 -0800976 handleDisable();
Zhihai Xu4e22ad32012-11-13 15:11:26 -0800977 // Pbap service need receive STATE_TURNING_OFF intent to close
978 bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,
979 BluetoothAdapter.STATE_TURNING_OFF);
Zhihai Xu40874a02012-10-08 17:57:03 -0700980
981 waitForOnOff(false, true);
982
Zhihai Xu4e22ad32012-11-13 15:11:26 -0800983 bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
Zhihai Xu40874a02012-10-08 17:57:03 -0700984 BluetoothAdapter.STATE_OFF);
Zhihai Xu40874a02012-10-08 17:57:03 -0700985 sendBluetoothServiceDownCallback();
986 synchronized (mConnection) {
987 if (mBluetooth != null) {
988 mBluetooth = null;
989 //Unbind
990 mContext.unbindService(mConnection);
991 }
992 }
993 SystemClock.sleep(100);
994
Zhihai Xu4e22ad32012-11-13 15:11:26 -0800995 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
996 mState = BluetoothAdapter.STATE_OFF;
Zhihai Xu40874a02012-10-08 17:57:03 -0700997 // enable
Zhihai Xu401202b2012-12-03 11:36:21 -0800998 handleEnable(mQuietEnable);
Zhihai Xu40874a02012-10-08 17:57:03 -0700999 } else if (mBinding || mBluetooth != null) {
1000 Message userMsg = mHandler.obtainMessage(MESSAGE_USER_SWITCHED);
1001 userMsg.arg2 = 1 + msg.arg2;
1002 // if user is switched when service is being binding
1003 // delay sending MESSAGE_USER_SWITCHED
1004 mHandler.sendMessageDelayed(userMsg, USER_SWITCHED_TIME_MS);
1005 if (DBG) {
1006 Log.d(TAG, "delay MESSAGE_USER_SWITCHED " + userMsg.arg2);
1007 }
1008 }
1009 break;
1010 }
fredc0f420372012-04-12 00:02:00 -07001011 }
1012 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001013 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001014
Zhihai Xu401202b2012-12-03 11:36:21 -08001015 private void handleEnable(boolean quietMode) {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001016 mQuietEnable = quietMode;
1017
Matthew Xiecdce0b92012-07-12 19:06:15 -07001018 synchronized(mConnection) {
Zhihai Xu40874a02012-10-08 17:57:03 -07001019 if ((mBluetooth == null) && (!mBinding)) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001020 //Start bind timeout and bind
1021 Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
1022 mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS);
1023 mConnection.setGetNameAddressOnly(false);
1024 Intent i = new Intent(IBluetooth.class.getName());
Amith Yamasani27b89e62013-01-16 12:30:11 -08001025 if (!mContext.bindServiceAsUser(i, mConnection,Context.BIND_AUTO_CREATE,
1026 UserHandle.CURRENT)) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001027 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
1028 Log.e(TAG, "Fail to bind to: " + IBluetooth.class.getName());
Zhihai Xu40874a02012-10-08 17:57:03 -07001029 } else {
1030 mBinding = true;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001031 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001032 } else if (mBluetooth != null) {
1033 if (mConnection.isGetNameAddressOnly()) {
1034 // if GetNameAddressOnly is set, we can clear this flag,
1035 // so the service won't be unbind
1036 // after name and address are saved
1037 mConnection.setGetNameAddressOnly(false);
1038 //Register callback object
1039 try {
1040 mBluetooth.registerCallback(mBluetoothCallback);
1041 } catch (RemoteException re) {
1042 Log.e(TAG, "Unable to register BluetoothCallback",re);
1043 }
1044 //Inform BluetoothAdapter instances that service is up
1045 sendBluetoothServiceUpCallback();
1046 }
1047
Matthew Xiecdce0b92012-07-12 19:06:15 -07001048 //Enable bluetooth
1049 try {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001050 if (!mQuietEnable) {
1051 if(!mBluetooth.enable()) {
1052 Log.e(TAG,"IBluetooth.enable() returned false");
1053 }
1054 }
1055 else {
1056 if(!mBluetooth.enableNoAutoConnect()) {
1057 Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false");
1058 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001059 }
1060 } catch (RemoteException e) {
1061 Log.e(TAG,"Unable to call enable()",e);
1062 }
1063 }
1064 }
1065 }
1066
Zhihai Xu401202b2012-12-03 11:36:21 -08001067 private void handleDisable() {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001068 synchronized(mConnection) {
Zhihai Xu40874a02012-10-08 17:57:03 -07001069 // don't need to disable if GetNameAddressOnly is set,
1070 // service will be unbinded after Name and Address are saved
1071 if ((mBluetooth != null) && (!mConnection.isGetNameAddressOnly())) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001072 if (DBG) Log.d(TAG,"Sending off request.");
1073
1074 try {
1075 if(!mBluetooth.disable()) {
1076 Log.e(TAG,"IBluetooth.disable() returned false");
1077 }
1078 } catch (RemoteException e) {
1079 Log.e(TAG,"Unable to call disable()",e);
1080 }
1081 }
1082 }
1083 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001084
1085 private boolean checkIfCallerIsForegroundUser() {
1086 int foregroundUser;
1087 int callingUser = UserHandle.getCallingUserId();
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001088 int callingUid = Binder.getCallingUid();
Zhihai Xu40874a02012-10-08 17:57:03 -07001089 long callingIdentity = Binder.clearCallingIdentity();
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001090 int callingAppId = UserHandle.getAppId(callingUid);
Zhihai Xu40874a02012-10-08 17:57:03 -07001091 boolean valid = false;
1092 try {
1093 foregroundUser = ActivityManager.getCurrentUser();
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001094 valid = (callingUser == foregroundUser) ||
1095 callingAppId == Process.NFC_UID;
Zhihai Xu40874a02012-10-08 17:57:03 -07001096 if (DBG) {
1097 Log.d(TAG, "checkIfCallerIsForegroundUser: valid=" + valid
1098 + " callingUser=" + callingUser
1099 + " foregroundUser=" + foregroundUser);
1100 }
1101 } finally {
1102 Binder.restoreCallingIdentity(callingIdentity);
1103 }
1104 return valid;
1105 }
1106
Zhihai Xu40874a02012-10-08 17:57:03 -07001107 private void bluetoothStateChangeHandler(int prevState, int newState) {
1108 if (prevState != newState) {
1109 //Notify all proxy objects first of adapter state change
1110 if (newState == BluetoothAdapter.STATE_ON || newState == BluetoothAdapter.STATE_OFF) {
1111 boolean isUp = (newState==BluetoothAdapter.STATE_ON);
1112 sendBluetoothStateCallback(isUp);
1113
Matthew Xieddf7e472013-03-01 18:41:02 -08001114 if (isUp) {
1115 // connect to GattService
Matthew Xie32ab77b2013-05-08 19:26:57 -07001116 if (mContext.getPackageManager().hasSystemFeature(
1117 PackageManager.FEATURE_BLUETOOTH_LE)) {
1118 Intent i = new Intent(IBluetoothGatt.class.getName());
1119 if (!mContext.bindServiceAsUser(i, mConnection, Context.BIND_AUTO_CREATE,
1120 UserHandle.CURRENT)) {
1121 Log.e(TAG, "Fail to bind to: " + IBluetoothGatt.class.getName());
1122 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001123 }
1124 } else {
1125 //If Bluetooth is off, send service down event to proxy objects, and unbind
1126 if (!isUp && canUnbindBluetoothService()) {
1127 sendBluetoothServiceDownCallback();
1128 unbindAndFinish();
1129 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001130 }
1131 }
1132
1133 //Send broadcast message to everyone else
1134 Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
1135 intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState);
1136 intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState);
1137 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1138 if (DBG) Log.d(TAG,"Bluetooth State Change Intent: " + prevState + " -> " + newState);
1139 mContext.sendBroadcastAsUser(intent, UserHandle.ALL,
1140 BLUETOOTH_PERM);
1141 }
1142 }
1143
1144 /**
1145 * if on is true, wait for state become ON
1146 * if off is true, wait for state become OFF
1147 * if both on and off are false, wait for state not ON
1148 */
1149 private boolean waitForOnOff(boolean on, boolean off) {
1150 int i = 0;
1151 while (i < 10) {
1152 synchronized(mConnection) {
1153 try {
1154 if (mBluetooth == null) break;
1155 if (on) {
1156 if (mBluetooth.getState() == BluetoothAdapter.STATE_ON) return true;
1157 } else if (off) {
1158 if (mBluetooth.getState() == BluetoothAdapter.STATE_OFF) return true;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001159 } else {
Zhihai Xu40874a02012-10-08 17:57:03 -07001160 if (mBluetooth.getState() != BluetoothAdapter.STATE_ON) return true;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001161 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001162 } catch (RemoteException e) {
1163 Log.e(TAG, "getState()", e);
1164 break;
1165 }
1166 }
1167 if (on || off) {
1168 SystemClock.sleep(300);
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001169 } else {
Zhihai Xu40874a02012-10-08 17:57:03 -07001170 SystemClock.sleep(50);
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001171 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001172 i++;
1173 }
1174 Log.e(TAG,"waitForOnOff time out");
1175 return false;
1176 }
Zhihai Xu681ae7f2012-11-12 15:14:18 -08001177
Zhihai Xu401202b2012-12-03 11:36:21 -08001178 private void sendDisableMsg() {
1179 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISABLE));
1180 }
1181
1182 private void sendEnableMsg(boolean quietMode) {
1183 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_ENABLE,
1184 quietMode ? 1 : 0, 0));
1185 }
1186
Zhihai Xu681ae7f2012-11-12 15:14:18 -08001187 private boolean canUnbindBluetoothService() {
1188 synchronized(mConnection) {
1189 //Only unbind with mEnable flag not set
1190 //For race condition: disable and enable back-to-back
1191 //Avoid unbind right after enable due to callback from disable
1192 //Only unbind with Bluetooth at OFF state
1193 //Only unbind without any MESSAGE_BLUETOOTH_STATE_CHANGE message
1194 try {
1195 if (mEnable || (mBluetooth == null)) return false;
1196 if (mHandler.hasMessages(MESSAGE_BLUETOOTH_STATE_CHANGE)) return false;
1197 return (mBluetooth.getState() == BluetoothAdapter.STATE_OFF);
1198 } catch (RemoteException e) {
1199 Log.e(TAG, "getState()", e);
1200 }
1201 }
1202 return false;
1203 }
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001204
1205 private void recoverBluetoothServiceFromError() {
1206 Log.e(TAG,"recoverBluetoothServiceFromError");
1207 synchronized (mConnection) {
1208 if (mBluetooth != null) {
1209 //Unregister callback object
1210 try {
1211 mBluetooth.unregisterCallback(mBluetoothCallback);
1212 } catch (RemoteException re) {
1213 Log.e(TAG, "Unable to unregister",re);
1214 }
1215 }
1216 }
1217
1218 SystemClock.sleep(500);
1219
1220 // disable
1221 handleDisable();
1222
1223 waitForOnOff(false, true);
1224
1225 sendBluetoothServiceDownCallback();
1226 synchronized (mConnection) {
1227 if (mBluetooth != null) {
1228 mBluetooth = null;
1229 //Unbind
1230 mContext.unbindService(mConnection);
1231 }
1232 }
1233
1234 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
1235 mState = BluetoothAdapter.STATE_OFF;
1236
1237 mEnable = false;
1238
1239 if (mErrorRecoveryRetryCounter++ < MAX_ERROR_RESTART_RETRIES) {
1240 // Send a Bluetooth Restart message to reenable bluetooth
1241 Message restartMsg = mHandler.obtainMessage(
1242 MESSAGE_RESTART_BLUETOOTH_SERVICE);
1243 mHandler.sendMessageDelayed(restartMsg, ERROR_RESTART_TIME_MS);
1244 } else {
1245 // todo: notify user to power down and power up phone to make bluetooth work.
1246 }
1247 }
fredc0f420372012-04-12 00:02:00 -07001248}