BluetoothManagerService.java revision bd9a9a53ed09ca355eb0911d2aeee526b964aa7b
1/*
2 * 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.
15 */
16
17package com.android.server;
18
19import android.app.ActivityManager;
20import android.bluetooth.BluetoothAdapter;
21import android.bluetooth.IBluetooth;
22import android.bluetooth.IBluetoothGatt;
23import android.bluetooth.IBluetoothCallback;
24import android.bluetooth.IBluetoothManager;
25import android.bluetooth.IBluetoothManagerCallback;
26import android.bluetooth.IBluetoothStateChangeCallback;
27import 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;
34import android.content.pm.PackageManager;
35import android.os.Binder;
36import android.os.Handler;
37import android.os.IBinder;
38import android.os.Looper;
39import android.os.Message;
40import android.os.Process;
41import android.os.RemoteCallbackList;
42import android.os.RemoteException;
43import android.os.SystemClock;
44import android.os.UserHandle;
45import android.provider.Settings;
46import android.util.Log;
47class BluetoothManagerService extends IBluetoothManager.Stub {
48    private static final String TAG = "BluetoothManagerService";
49    private static final boolean DBG = true;
50
51    private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN;
52    private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
53    private static final String ACTION_SERVICE_STATE_CHANGED="com.android.bluetooth.btservice.action.STATE_CHANGED";
54    private static final String EXTRA_ACTION="action";
55    private static final String SECURE_SETTINGS_BLUETOOTH_ADDR_VALID="bluetooth_addr_valid";
56    private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address";
57    private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name";
58    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
60    //Maximum msec to wait for service restart
61    private static final int SERVICE_RESTART_TIME_MS = 200;
62    //Maximum msec to wait for restart due to error
63    private static final int ERROR_RESTART_TIME_MS = 3000;
64    //Maximum msec to delay MESSAGE_USER_SWITCHED
65    private static final int USER_SWITCHED_TIME_MS = 200;
66
67    private static final int MESSAGE_ENABLE = 1;
68    private static final int MESSAGE_DISABLE = 2;
69    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;
75    private static final int MESSAGE_RESTART_BLUETOOTH_SERVICE = 42;
76    private static final int MESSAGE_BLUETOOTH_STATE_CHANGE=60;
77    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;
81    private static final int MESSAGE_USER_SWITCHED = 300;
82    private static final int MAX_SAVE_RETRIES=3;
83    private static final int MAX_ERROR_RESTART_RETRIES=6;
84
85    // 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;
94
95    private static final int SERVICE_IBLUETOOTH = 1;
96    private static final int SERVICE_IBLUETOOTHGATT = 2;
97
98    private final Context mContext;
99
100    // Locks are not provided for mName and mAddress.
101    // They are accessed in handler or broadcast receiver, same thread context.
102    private String mAddress;
103    private String mName;
104    private final ContentResolver mContentResolver;
105    private final RemoteCallbackList<IBluetoothManagerCallback> mCallbacks;
106    private final RemoteCallbackList<IBluetoothStateChangeCallback> mStateChangeCallbacks;
107    private IBluetooth mBluetooth;
108    private IBluetoothGatt mBluetoothGatt;
109    private boolean mBinding;
110    private boolean mUnbinding;
111    // used inside handler thread
112    private boolean mQuietEnable = false;
113    // 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
120    private boolean mEnable;
121    private int mState;
122    private final BluetoothHandler mHandler;
123    private int mErrorRecoveryRetryCounter;
124    private final int mSystemUiUid;
125
126    private void registerForAirplaneMode(IntentFilter filter) {
127        final ContentResolver resolver = mContext.getContentResolver();
128        final String airplaneModeRadios = Settings.Global.getString(resolver,
129                Settings.Global.AIRPLANE_MODE_RADIOS);
130        final String toggleableRadios = Settings.Global.getString(resolver,
131                Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
132        boolean mIsAirplaneSensitive = airplaneModeRadios == null ? true :
133                airplaneModeRadios.contains(Settings.Global.RADIO_BLUETOOTH);
134        if (mIsAirplaneSensitive) {
135            filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
136        }
137    }
138
139    private final IBluetoothCallback mBluetoothCallback =  new IBluetoothCallback.Stub() {
140        @Override
141        public void onBluetoothStateChange(int prevState, int newState) throws RemoteException  {
142            Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_STATE_CHANGE,prevState,newState);
143            mHandler.sendMessage(msg);
144        }
145    };
146
147    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
148        @Override
149        public void onReceive(Context context, Intent intent) {
150            String action = intent.getAction();
151            if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action)) {
152                String newName = intent.getStringExtra(BluetoothAdapter.EXTRA_LOCAL_NAME);
153                if (DBG) Log.d(TAG, "Bluetooth Adapter name changed to " + newName);
154                if (newName != null) {
155                    storeNameAndAddress(newName, null);
156                }
157            } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
158                synchronized(mReceiver) {
159                    if (isBluetoothPersistedStateOn()) {
160                        if (isAirplaneModeOn()) {
161                            persistBluetoothSetting(BLUETOOTH_ON_AIRPLANE);
162                        } else {
163                            persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);
164                        }
165                    }
166                    if (isAirplaneModeOn()) {
167                        // disable without persisting the setting
168                        sendDisableMsg();
169                    } else if (mEnableExternal) {
170                        // enable without persisting the setting
171                        sendEnableMsg(mQuietEnableExternal);
172                    }
173                }
174            } else if (Intent.ACTION_USER_SWITCHED.equals(action)) {
175                mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_USER_SWITCHED,
176                       intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0), 0));
177            } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
178                synchronized(mReceiver) {
179                    if (mEnableExternal && isBluetoothPersistedStateOnBluetooth()) {
180                        //Enable
181                        if (DBG) Log.d(TAG, "Auto-enabling Bluetooth.");
182                        sendEnableMsg(mQuietEnableExternal);
183                    }
184                }
185
186                if (!isNameAndAddressSet()) {
187                    //Sync the Bluetooth name and address from the Bluetooth Adapter
188                    if (DBG) Log.d(TAG,"Retrieving Bluetooth Adapter name and address...");
189                    getNameAndAddress();
190                }
191            }
192        }
193    };
194
195    BluetoothManagerService(Context context) {
196        mHandler = new BluetoothHandler(IoThread.get().getLooper());
197
198        mContext = context;
199        mBluetooth = null;
200        mBinding = false;
201        mUnbinding = false;
202        mEnable = false;
203        mState = BluetoothAdapter.STATE_OFF;
204        mQuietEnableExternal = false;
205        mEnableExternal = false;
206        mAddress = null;
207        mName = null;
208        mErrorRecoveryRetryCounter = 0;
209        mContentResolver = context.getContentResolver();
210        mCallbacks = new RemoteCallbackList<IBluetoothManagerCallback>();
211        mStateChangeCallbacks = new RemoteCallbackList<IBluetoothStateChangeCallback>();
212        IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
213        filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
214        filter.addAction(Intent.ACTION_USER_SWITCHED);
215        registerForAirplaneMode(filter);
216        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
217        mContext.registerReceiver(mReceiver, filter);
218        loadStoredNameAndAddress();
219        if (isBluetoothPersistedStateOn()) {
220            mEnableExternal = true;
221        }
222
223        int sysUiUid = -1;
224        try {
225            sysUiUid = mContext.getPackageManager().getPackageUid("com.android.systemui",
226                    UserHandle.USER_OWNER);
227        } catch (PackageManager.NameNotFoundException e) {
228            Log.wtf(TAG, "Unable to resolve SystemUI's UID.", e);
229        }
230        mSystemUiUid = sysUiUid;
231    }
232
233    /**
234     *  Returns true if airplane mode is currently on
235     */
236    private final boolean isAirplaneModeOn() {
237        return Settings.Global.getInt(mContext.getContentResolver(),
238                Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
239    }
240
241    /**
242     *  Returns true if the Bluetooth saved state is "on"
243     */
244    private final boolean isBluetoothPersistedStateOn() {
245        return Settings.Global.getInt(mContentResolver,
246                Settings.Global.BLUETOOTH_ON, 0) != BLUETOOTH_OFF;
247    }
248
249    /**
250     *  Returns true if the Bluetooth saved state is BLUETOOTH_ON_BLUETOOTH
251     */
252    private final boolean isBluetoothPersistedStateOnBluetooth() {
253        return Settings.Global.getInt(mContentResolver,
254                Settings.Global.BLUETOOTH_ON, 0) == BLUETOOTH_ON_BLUETOOTH;
255    }
256
257    /**
258     *  Save the Bluetooth on/off state
259     *
260     */
261    private void persistBluetoothSetting(int value) {
262        Settings.Global.putInt(mContext.getContentResolver(),
263                               Settings.Global.BLUETOOTH_ON,
264                               value);
265    }
266
267    /**
268     * Returns true if the Bluetooth Adapter's name and address is
269     * locally cached
270     * @return
271     */
272    private boolean isNameAndAddressSet() {
273        return mName !=null && mAddress!= null && mName.length()>0 && mAddress.length()>0;
274    }
275
276    /**
277     * Retrieve the Bluetooth Adapter's name and address and save it in
278     * in the local cache
279     */
280    private void loadStoredNameAndAddress() {
281        if (DBG) Log.d(TAG, "Loading stored name and address");
282        if (mContext.getResources().getBoolean
283            (com.android.internal.R.bool.config_bluetooth_address_validation) &&
284             Settings.Secure.getInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 0) == 0) {
285            // if the valid flag is not set, don't load the address and name
286            if (DBG) Log.d(TAG, "invalid bluetooth name and address stored");
287            return;
288        }
289        mName = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME);
290        mAddress = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS);
291        if (DBG) Log.d(TAG, "Stored bluetooth Name=" + mName + ",Address=" + mAddress);
292    }
293
294    /**
295     * Save the Bluetooth name and address in the persistent store.
296     * Only non-null values will be saved.
297     * @param name
298     * @param address
299     */
300    private void storeNameAndAddress(String name, String address) {
301        if (name != null) {
302            Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME, name);
303            mName = name;
304            if (DBG) Log.d(TAG,"Stored Bluetooth name: " +
305                Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_NAME));
306        }
307
308        if (address != null) {
309            Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS, address);
310            mAddress=address;
311            if (DBG)  Log.d(TAG,"Stored Bluetoothaddress: " +
312                Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_ADDRESS));
313        }
314
315        if ((name != null) && (address != null)) {
316            Settings.Secure.putInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 1);
317        }
318    }
319
320    public IBluetooth registerAdapter(IBluetoothManagerCallback callback){
321        if (callback == null) {
322            Log.w(TAG, "Callback is null in registerAdapter");
323            return null;
324        }
325        Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_ADAPTER);
326        msg.obj = callback;
327        mHandler.sendMessage(msg);
328        synchronized(mConnection) {
329            return mBluetooth;
330        }
331    }
332
333    public void unregisterAdapter(IBluetoothManagerCallback callback) {
334        if (callback == null) {
335            Log.w(TAG, "Callback is null in unregisterAdapter");
336            return;
337        }
338        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
339                                                "Need BLUETOOTH permission");
340        Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_ADAPTER);
341        msg.obj = callback;
342        mHandler.sendMessage(msg);
343    }
344
345    public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) {
346        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
347                                                "Need BLUETOOTH permission");
348        Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_STATE_CHANGE_CALLBACK);
349        msg.obj = callback;
350        mHandler.sendMessage(msg);
351    }
352
353    public void unregisterStateChangeCallback(IBluetoothStateChangeCallback callback) {
354        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
355                                                "Need BLUETOOTH permission");
356        Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK);
357        msg.obj = callback;
358        mHandler.sendMessage(msg);
359    }
360
361    public boolean isEnabled() {
362        if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
363            (!checkIfCallerIsForegroundUser())) {
364            Log.w(TAG,"isEnabled(): not allowed for non-active and non system user");
365            return false;
366        }
367
368        synchronized(mConnection) {
369            try {
370                return (mBluetooth != null && mBluetooth.isEnabled());
371            } catch (RemoteException e) {
372                Log.e(TAG, "isEnabled()", e);
373            }
374        }
375        return false;
376    }
377
378    public void getNameAndAddress() {
379        if (DBG) {
380            Log.d(TAG,"getNameAndAddress(): mBluetooth = " + mBluetooth +
381                  " mBinding = " + mBinding);
382        }
383        Message msg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
384        mHandler.sendMessage(msg);
385    }
386    public boolean enableNoAutoConnect()
387    {
388        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
389                                                "Need BLUETOOTH ADMIN permission");
390
391        if (DBG) {
392            Log.d(TAG,"enableNoAutoConnect():  mBluetooth =" + mBluetooth +
393                    " mBinding = " + mBinding);
394        }
395        int callingAppId = UserHandle.getAppId(Binder.getCallingUid());
396
397        if (callingAppId != Process.NFC_UID) {
398            throw new SecurityException("no permission to enable Bluetooth quietly");
399        }
400
401        synchronized(mReceiver) {
402            mQuietEnableExternal = true;
403            mEnableExternal = true;
404            sendEnableMsg(true);
405        }
406        return true;
407
408    }
409    public boolean enable() {
410        if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
411            (!checkIfCallerIsForegroundUser())) {
412            Log.w(TAG,"enable(): not allowed for non-active and non system user");
413            return false;
414        }
415
416        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
417                                                "Need BLUETOOTH ADMIN permission");
418        if (DBG) {
419            Log.d(TAG,"enable():  mBluetooth =" + mBluetooth +
420                    " mBinding = " + mBinding);
421        }
422
423        synchronized(mReceiver) {
424            mQuietEnableExternal = false;
425            mEnableExternal = true;
426            // waive WRITE_SECURE_SETTINGS permission check
427            long callingIdentity = Binder.clearCallingIdentity();
428            persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);
429            Binder.restoreCallingIdentity(callingIdentity);
430            sendEnableMsg(false);
431        }
432        return true;
433    }
434
435    public boolean disable(boolean persist) {
436        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
437                                                "Need BLUETOOTH ADMIN permissicacheNameAndAddresson");
438
439        if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
440            (!checkIfCallerIsForegroundUser())) {
441            Log.w(TAG,"disable(): not allowed for non-active and non system user");
442            return false;
443        }
444
445        if (DBG) {
446            Log.d(TAG,"disable(): mBluetooth = " + mBluetooth +
447                " mBinding = " + mBinding);
448        }
449
450        synchronized(mReceiver) {
451            if (persist) {
452                // waive WRITE_SECURE_SETTINGS permission check
453                long callingIdentity = Binder.clearCallingIdentity();
454                persistBluetoothSetting(BLUETOOTH_OFF);
455                Binder.restoreCallingIdentity(callingIdentity);
456            }
457            mEnableExternal = false;
458            sendDisableMsg();
459        }
460        return true;
461    }
462
463    public void unbindAndFinish() {
464        if (DBG) {
465            Log.d(TAG,"unbindAndFinish(): " + mBluetooth +
466                " mBinding = " + mBinding);
467        }
468
469        synchronized (mConnection) {
470            if (mUnbinding) return;
471            mUnbinding = true;
472            if (mBluetooth != null) {
473                if (!mConnection.isGetNameAddressOnly()) {
474                    //Unregister callback object
475                    try {
476                        mBluetooth.unregisterCallback(mBluetoothCallback);
477                    } catch (RemoteException re) {
478                        Log.e(TAG, "Unable to unregister BluetoothCallback",re);
479                    }
480                }
481                if (DBG) Log.d(TAG, "Sending unbind request.");
482                mBluetooth = null;
483                //Unbind
484                mContext.unbindService(mConnection);
485                mUnbinding = false;
486                mBinding = false;
487            } else {
488                mUnbinding=false;
489            }
490        }
491    }
492
493    public IBluetoothGatt getBluetoothGatt() {
494        // sync protection
495        return mBluetoothGatt;
496    }
497
498    private void sendBluetoothStateCallback(boolean isUp) {
499        int n = mStateChangeCallbacks.beginBroadcast();
500        if (DBG) Log.d(TAG,"Broadcasting onBluetoothStateChange("+isUp+") to " + n + " receivers.");
501        for (int i=0; i <n;i++) {
502            try {
503                mStateChangeCallbacks.getBroadcastItem(i).onBluetoothStateChange(isUp);
504            } catch (RemoteException e) {
505                Log.e(TAG, "Unable to call onBluetoothStateChange() on callback #" + i , e);
506            }
507        }
508        mStateChangeCallbacks.finishBroadcast();
509    }
510
511    /**
512     * Inform BluetoothAdapter instances that Adapter service is up
513     */
514    private void sendBluetoothServiceUpCallback() {
515        if (!mConnection.isGetNameAddressOnly()) {
516            if (DBG) Log.d(TAG,"Calling onBluetoothServiceUp callbacks");
517            int n = mCallbacks.beginBroadcast();
518            Log.d(TAG,"Broadcasting onBluetoothServiceUp() to " + n + " receivers.");
519            for (int i=0; i <n;i++) {
520                try {
521                    mCallbacks.getBroadcastItem(i).onBluetoothServiceUp(mBluetooth);
522                }  catch (RemoteException e) {
523                    Log.e(TAG, "Unable to call onBluetoothServiceUp() on callback #" + i, e);
524                }
525            }
526            mCallbacks.finishBroadcast();
527        }
528    }
529    /**
530     * Inform BluetoothAdapter instances that Adapter service is down
531     */
532    private void sendBluetoothServiceDownCallback() {
533        if (!mConnection.isGetNameAddressOnly()) {
534            if (DBG) Log.d(TAG,"Calling onBluetoothServiceDown callbacks");
535            int n = mCallbacks.beginBroadcast();
536            Log.d(TAG,"Broadcasting onBluetoothServiceDown() to " + n + " receivers.");
537            for (int i=0; i <n;i++) {
538                try {
539                    mCallbacks.getBroadcastItem(i).onBluetoothServiceDown();
540                }  catch (RemoteException e) {
541                    Log.e(TAG, "Unable to call onBluetoothServiceDown() on callback #" + i, e);
542                }
543            }
544            mCallbacks.finishBroadcast();
545        }
546    }
547    public String getAddress() {
548        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
549                                                "Need BLUETOOTH permission");
550
551        if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
552            (!checkIfCallerIsForegroundUser())) {
553            Log.w(TAG,"getAddress(): not allowed for non-active and non system user");
554            return null;
555        }
556
557        synchronized(mConnection) {
558            if (mBluetooth != null) {
559                try {
560                    return mBluetooth.getAddress();
561                } catch (RemoteException e) {
562                    Log.e(TAG, "getAddress(): Unable to retrieve address remotely..Returning cached address",e);
563                }
564            }
565        }
566        // mAddress is accessed from outside.
567        // It is alright without a lock. Here, bluetooth is off, no other thread is
568        // changing mAddress
569        return mAddress;
570    }
571
572    public String getName() {
573        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
574                                                "Need BLUETOOTH permission");
575
576        if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
577            (!checkIfCallerIsForegroundUser())) {
578            Log.w(TAG,"getName(): not allowed for non-active and non system user");
579            return null;
580        }
581
582        synchronized(mConnection) {
583            if (mBluetooth != null) {
584                try {
585                    return mBluetooth.getName();
586                } catch (RemoteException e) {
587                    Log.e(TAG, "getName(): Unable to retrieve name remotely..Returning cached name",e);
588                }
589            }
590        }
591        // mName is accessed from outside.
592        // It alright without a lock. Here, bluetooth is off, no other thread is
593        // changing mName
594        return mName;
595    }
596
597    private class BluetoothServiceConnection implements ServiceConnection {
598
599        private boolean mGetNameAddressOnly;
600
601        public void setGetNameAddressOnly(boolean getOnly) {
602            mGetNameAddressOnly = getOnly;
603        }
604
605        public boolean isGetNameAddressOnly() {
606            return mGetNameAddressOnly;
607        }
608
609        public void onServiceConnected(ComponentName className, IBinder service) {
610            if (DBG) Log.d(TAG, "BluetoothServiceConnection: " + className.getClassName());
611            Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED);
612            // TBD if (className.getClassName().equals(IBluetooth.class.getName())) {
613            if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) {
614                msg.arg1 = SERVICE_IBLUETOOTH;
615                // } else if (className.getClassName().equals(IBluetoothGatt.class.getName())) {
616            } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) {
617                msg.arg1 = SERVICE_IBLUETOOTHGATT;
618            } else {
619                Log.e(TAG, "Unknown service connected: " + className.getClassName());
620                return;
621            }
622            msg.obj = service;
623            mHandler.sendMessage(msg);
624        }
625
626        public void onServiceDisconnected(ComponentName className) {
627            // Called if we unexpected disconnected.
628            if (DBG) Log.d(TAG, "BluetoothServiceConnection, disconnected: " +
629                           className.getClassName());
630            Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED);
631            if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) {
632                msg.arg1 = SERVICE_IBLUETOOTH;
633            } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) {
634                msg.arg1 = SERVICE_IBLUETOOTHGATT;
635            } else {
636                Log.e(TAG, "Unknown service disconnected: " + className.getClassName());
637                return;
638            }
639            mHandler.sendMessage(msg);
640        }
641    }
642
643    private BluetoothServiceConnection mConnection = new BluetoothServiceConnection();
644
645    private class BluetoothHandler extends Handler {
646        public BluetoothHandler(Looper looper) {
647            super(looper);
648        }
649
650        @Override
651        public void handleMessage(Message msg) {
652            if (DBG) Log.d (TAG, "Message: " + msg.what);
653            switch (msg.what) {
654                case MESSAGE_GET_NAME_AND_ADDRESS: {
655                    if (DBG) Log.d(TAG,"MESSAGE_GET_NAME_AND_ADDRESS");
656                    synchronized(mConnection) {
657                        //Start bind request
658                        if ((mBluetooth == null) && (!mBinding)) {
659                            if (DBG) Log.d(TAG, "Binding to service to get name and address");
660                            mConnection.setGetNameAddressOnly(true);
661                            //Start bind timeout and bind
662                            Message timeoutMsg = mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
663                            mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS);
664                            Intent i = new Intent(IBluetooth.class.getName());
665                            if (!doBind(i, mConnection,
666                                    Context.BIND_AUTO_CREATE, UserHandle.CURRENT)) {
667                                mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
668                            } else {
669                                mBinding = true;
670                            }
671                        }
672                        else {
673                            Message saveMsg= mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS);
674                            saveMsg.arg1 = 0;
675                            if (mBluetooth != null) {
676                                mHandler.sendMessage(saveMsg);
677                            } else {
678                                // if enable is also called to bind the service
679                                // wait for MESSAGE_BLUETOOTH_SERVICE_CONNECTED
680                                mHandler.sendMessageDelayed(saveMsg, TIMEOUT_SAVE_MS);
681                            }
682                        }
683                    }
684                    break;
685                }
686                case MESSAGE_SAVE_NAME_AND_ADDRESS: {
687                    boolean unbind = false;
688                    if (DBG) Log.d(TAG,"MESSAGE_SAVE_NAME_AND_ADDRESS");
689                    synchronized(mConnection) {
690                        if (!mEnable && mBluetooth != null) {
691                            try {
692                                mBluetooth.enable();
693                            } catch (RemoteException e) {
694                                Log.e(TAG,"Unable to call enable()",e);
695                            }
696                        }
697                    }
698                    if (mBluetooth != null) waitForOnOff(true, false);
699                    synchronized(mConnection) {
700                        if (mBluetooth != null) {
701                            String name =  null;
702                            String address = null;
703                            try {
704                                name =  mBluetooth.getName();
705                                address = mBluetooth.getAddress();
706                            } catch (RemoteException re) {
707                                Log.e(TAG,"",re);
708                            }
709
710                            if (name != null && address != null) {
711                                storeNameAndAddress(name,address);
712                                if (mConnection.isGetNameAddressOnly()) {
713                                    unbind = true;
714                                }
715                            } else {
716                                if (msg.arg1 < MAX_SAVE_RETRIES) {
717                                    Message retryMsg = mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS);
718                                    retryMsg.arg1= 1+msg.arg1;
719                                    if (DBG) Log.d(TAG,"Retrying name/address remote retrieval and save.....Retry count =" + retryMsg.arg1);
720                                    mHandler.sendMessageDelayed(retryMsg, TIMEOUT_SAVE_MS);
721                                } else {
722                                    Log.w(TAG,"Maximum name/address remote retrieval retry exceeded");
723                                    if (mConnection.isGetNameAddressOnly()) {
724                                        unbind = true;
725                                    }
726                                }
727                            }
728                            if (!mEnable) {
729                                try {
730                                    mBluetooth.disable();
731                                } catch (RemoteException e) {
732                                    Log.e(TAG,"Unable to call disable()",e);
733                                }
734                            }
735                        } else {
736                            // rebind service by Request GET NAME AND ADDRESS
737                            // if service is unbinded by disable or
738                            // MESSAGE_BLUETOOTH_SERVICE_CONNECTED is not received
739                            Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
740                            mHandler.sendMessage(getMsg);
741                        }
742                    }
743                    if (!mEnable && mBluetooth != null) waitForOnOff(false, true);
744                    if (unbind) {
745                        unbindAndFinish();
746                    }
747                    break;
748                }
749                case MESSAGE_ENABLE:
750                    if (DBG) {
751                        Log.d(TAG, "MESSAGE_ENABLE: mBluetooth = " + mBluetooth);
752                    }
753                    mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
754                    mEnable = true;
755                    handleEnable(msg.arg1 == 1);
756                    break;
757
758                case MESSAGE_DISABLE:
759                    mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
760                    if (mEnable && mBluetooth != null) {
761                        waitForOnOff(true, false);
762                        mEnable = false;
763                        handleDisable();
764                        waitForOnOff(false, false);
765                    } else {
766                        mEnable = false;
767                        handleDisable();
768                    }
769                    break;
770
771                case MESSAGE_REGISTER_ADAPTER:
772                {
773                    IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
774                    boolean added = mCallbacks.register(callback);
775                    Log.d(TAG,"Added callback: " +  (callback == null? "null": callback)  +":" +added );
776                }
777                    break;
778                case MESSAGE_UNREGISTER_ADAPTER:
779                {
780                    IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
781                    boolean removed = mCallbacks.unregister(callback);
782                    Log.d(TAG,"Removed callback: " +  (callback == null? "null": callback)  +":" + removed);
783                    break;
784                }
785                case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK:
786                {
787                    IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj;
788                    if (callback != null) {
789                        mStateChangeCallbacks.register(callback);
790                    }
791                    break;
792                }
793                case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK:
794                {
795                    IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj;
796                    if (callback != null) {
797                        mStateChangeCallbacks.unregister(callback);
798                    }
799                    break;
800                }
801                case MESSAGE_BLUETOOTH_SERVICE_CONNECTED:
802                {
803                    if (DBG) Log.d(TAG,"MESSAGE_BLUETOOTH_SERVICE_CONNECTED: " + msg.arg1);
804
805                    IBinder service = (IBinder) msg.obj;
806                    synchronized(mConnection) {
807                        if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
808                            mBluetoothGatt = IBluetoothGatt.Stub.asInterface(service);
809                            break;
810                        } // else must be SERVICE_IBLUETOOTH
811
812                        //Remove timeout
813                        mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
814
815                        mBinding = false;
816                        mBluetooth = IBluetooth.Stub.asInterface(service);
817
818                        try {
819                            boolean enableHciSnoopLog = (Settings.Secure.getInt(mContentResolver,
820                                Settings.Secure.BLUETOOTH_HCI_LOG, 0) == 1);
821                            if (!mBluetooth.configHciSnoopLog(enableHciSnoopLog)) {
822                                Log.e(TAG,"IBluetooth.configHciSnoopLog return false");
823                            }
824                        } catch (RemoteException e) {
825                            Log.e(TAG,"Unable to call configHciSnoopLog", e);
826                        }
827
828                        if (mConnection.isGetNameAddressOnly()) {
829                            //Request GET NAME AND ADDRESS
830                            Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
831                            mHandler.sendMessage(getMsg);
832                            if (!mEnable) return;
833                        }
834
835                        mConnection.setGetNameAddressOnly(false);
836                        //Register callback object
837                        try {
838                            mBluetooth.registerCallback(mBluetoothCallback);
839                        } catch (RemoteException re) {
840                            Log.e(TAG, "Unable to register BluetoothCallback",re);
841                        }
842                        //Inform BluetoothAdapter instances that service is up
843                        sendBluetoothServiceUpCallback();
844
845                        //Do enable request
846                        try {
847                            if (mQuietEnable == false) {
848                                if(!mBluetooth.enable()) {
849                                    Log.e(TAG,"IBluetooth.enable() returned false");
850                                }
851                            }
852                            else
853                            {
854                                if(!mBluetooth.enableNoAutoConnect()) {
855                                    Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false");
856                                }
857                            }
858                        } catch (RemoteException e) {
859                            Log.e(TAG,"Unable to call enable()",e);
860                        }
861                    }
862
863                    if (!mEnable) {
864                        waitForOnOff(true, false);
865                        handleDisable();
866                        waitForOnOff(false, false);
867                    }
868                    break;
869                }
870                case MESSAGE_TIMEOUT_BIND: {
871                    Log.e(TAG, "MESSAGE_TIMEOUT_BIND");
872                    synchronized(mConnection) {
873                        mBinding = false;
874                    }
875                    break;
876                }
877                case MESSAGE_BLUETOOTH_STATE_CHANGE:
878                {
879                    int prevState = msg.arg1;
880                    int newState = msg.arg2;
881                    if (DBG) Log.d(TAG, "MESSAGE_BLUETOOTH_STATE_CHANGE: prevState = " + prevState + ", newState=" + newState);
882                    mState = newState;
883                    bluetoothStateChangeHandler(prevState, newState);
884                    // handle error state transition case from TURNING_ON to OFF
885                    // unbind and rebind bluetooth service and enable bluetooth
886                    if ((prevState == BluetoothAdapter.STATE_TURNING_ON) &&
887                        (newState == BluetoothAdapter.STATE_OFF) &&
888                        (mBluetooth != null) && mEnable) {
889                        recoverBluetoothServiceFromError();
890                    }
891                    if (newState == BluetoothAdapter.STATE_ON) {
892                        // bluetooth is working, reset the counter
893                        if (mErrorRecoveryRetryCounter != 0) {
894                            Log.w(TAG, "bluetooth is recovered from error");
895                            mErrorRecoveryRetryCounter = 0;
896                        }
897                    }
898                    break;
899                }
900                case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED:
901                {
902                    Log.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: " + msg.arg1);
903                    synchronized(mConnection) {
904                        if (msg.arg1 == SERVICE_IBLUETOOTH) {
905                            // if service is unbinded already, do nothing and return
906                            if (mBluetooth == null) break;
907                            mBluetooth = null;
908                        } else if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
909                            mBluetoothGatt = null;
910                            break;
911                        } else {
912                            Log.e(TAG, "Bad msg.arg1: " + msg.arg1);
913                            break;
914                        }
915                    }
916
917                    if (mEnable) {
918                        mEnable = false;
919                        // Send a Bluetooth Restart message
920                        Message restartMsg = mHandler.obtainMessage(
921                            MESSAGE_RESTART_BLUETOOTH_SERVICE);
922                        mHandler.sendMessageDelayed(restartMsg,
923                            SERVICE_RESTART_TIME_MS);
924                    }
925
926                    if (!mConnection.isGetNameAddressOnly()) {
927                        sendBluetoothServiceDownCallback();
928
929                        // Send BT state broadcast to update
930                        // the BT icon correctly
931                        if ((mState == BluetoothAdapter.STATE_TURNING_ON) ||
932                            (mState == BluetoothAdapter.STATE_ON)) {
933                            bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,
934                                                        BluetoothAdapter.STATE_TURNING_OFF);
935                            mState = BluetoothAdapter.STATE_TURNING_OFF;
936                        }
937                        if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
938                            bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
939                                                        BluetoothAdapter.STATE_OFF);
940                        }
941
942                        mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
943                        mState = BluetoothAdapter.STATE_OFF;
944                    }
945                    break;
946                }
947                case MESSAGE_RESTART_BLUETOOTH_SERVICE:
948                {
949                    Log.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE:"
950                        +" Restart IBluetooth service");
951                    /* Enable without persisting the setting as
952                     it doesnt change when IBluetooth
953                     service restarts */
954                    mEnable = true;
955                    handleEnable(mQuietEnable);
956                    break;
957                }
958
959                case MESSAGE_TIMEOUT_UNBIND:
960                {
961                    Log.e(TAG, "MESSAGE_TIMEOUT_UNBIND");
962                    synchronized(mConnection) {
963                        mUnbinding = false;
964                    }
965                    break;
966                }
967
968                case MESSAGE_USER_SWITCHED:
969                {
970                    if (DBG) {
971                        Log.d(TAG, "MESSAGE_USER_SWITCHED");
972                    }
973                    mHandler.removeMessages(MESSAGE_USER_SWITCHED);
974                    /* disable and enable BT when detect a user switch */
975                    if (mEnable && mBluetooth != null) {
976                        synchronized (mConnection) {
977                            if (mBluetooth != null) {
978                                //Unregister callback object
979                                try {
980                                    mBluetooth.unregisterCallback(mBluetoothCallback);
981                                } catch (RemoteException re) {
982                                    Log.e(TAG, "Unable to unregister",re);
983                                }
984                            }
985                        }
986
987                        if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
988                            // MESSAGE_USER_SWITCHED happened right after MESSAGE_ENABLE
989                            bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_OFF);
990                            mState = BluetoothAdapter.STATE_OFF;
991                        }
992                        if (mState == BluetoothAdapter.STATE_OFF) {
993                            bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_TURNING_ON);
994                            mState = BluetoothAdapter.STATE_TURNING_ON;
995                        }
996
997                        waitForOnOff(true, false);
998
999                        if (mState == BluetoothAdapter.STATE_TURNING_ON) {
1000                            bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_ON);
1001                        }
1002
1003                        // disable
1004                        handleDisable();
1005                        // Pbap service need receive STATE_TURNING_OFF intent to close
1006                        bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,
1007                                                    BluetoothAdapter.STATE_TURNING_OFF);
1008
1009                        waitForOnOff(false, true);
1010
1011                        bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
1012                                                    BluetoothAdapter.STATE_OFF);
1013                        sendBluetoothServiceDownCallback();
1014                        synchronized (mConnection) {
1015                            if (mBluetooth != null) {
1016                                mBluetooth = null;
1017                                //Unbind
1018                                mContext.unbindService(mConnection);
1019                            }
1020                        }
1021                        SystemClock.sleep(100);
1022
1023                        mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
1024                        mState = BluetoothAdapter.STATE_OFF;
1025                        // enable
1026                        handleEnable(mQuietEnable);
1027                    } else if (mBinding || mBluetooth != null) {
1028                        Message userMsg = mHandler.obtainMessage(MESSAGE_USER_SWITCHED);
1029                        userMsg.arg2 = 1 + msg.arg2;
1030                        // if user is switched when service is being binding
1031                        // delay sending MESSAGE_USER_SWITCHED
1032                        mHandler.sendMessageDelayed(userMsg, USER_SWITCHED_TIME_MS);
1033                        if (DBG) {
1034                            Log.d(TAG, "delay MESSAGE_USER_SWITCHED " + userMsg.arg2);
1035                        }
1036                    }
1037                    break;
1038                }
1039            }
1040        }
1041    }
1042
1043    private void handleEnable(boolean quietMode) {
1044        mQuietEnable = quietMode;
1045
1046        synchronized(mConnection) {
1047            if ((mBluetooth == null) && (!mBinding)) {
1048                //Start bind timeout and bind
1049                Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
1050                mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS);
1051                mConnection.setGetNameAddressOnly(false);
1052                Intent i = new Intent(IBluetooth.class.getName());
1053                if (!doBind(i, mConnection,Context.BIND_AUTO_CREATE, UserHandle.CURRENT)) {
1054                    mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
1055                } else {
1056                    mBinding = true;
1057                }
1058            } else if (mBluetooth != null) {
1059                if (mConnection.isGetNameAddressOnly()) {
1060                    // if GetNameAddressOnly is set, we can clear this flag,
1061                    // so the service won't be unbind
1062                    // after name and address are saved
1063                    mConnection.setGetNameAddressOnly(false);
1064                    //Register callback object
1065                    try {
1066                        mBluetooth.registerCallback(mBluetoothCallback);
1067                    } catch (RemoteException re) {
1068                        Log.e(TAG, "Unable to register BluetoothCallback",re);
1069                    }
1070                    //Inform BluetoothAdapter instances that service is up
1071                    sendBluetoothServiceUpCallback();
1072                }
1073
1074                //Enable bluetooth
1075                try {
1076                    if (!mQuietEnable) {
1077                        if(!mBluetooth.enable()) {
1078                            Log.e(TAG,"IBluetooth.enable() returned false");
1079                        }
1080                    }
1081                    else {
1082                        if(!mBluetooth.enableNoAutoConnect()) {
1083                            Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false");
1084                        }
1085                    }
1086                } catch (RemoteException e) {
1087                    Log.e(TAG,"Unable to call enable()",e);
1088                }
1089            }
1090        }
1091    }
1092
1093    boolean doBind(Intent intent, ServiceConnection conn, int flags, UserHandle user) {
1094        ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
1095        intent.setComponent(comp);
1096        if (comp == null || !mContext.bindServiceAsUser(intent, conn, flags, user)) {
1097            Log.e(TAG, "Fail to bind to: " + intent);
1098            return false;
1099        }
1100        return true;
1101    }
1102
1103    private void handleDisable() {
1104        synchronized(mConnection) {
1105            // don't need to disable if GetNameAddressOnly is set,
1106            // service will be unbinded after Name and Address are saved
1107            if ((mBluetooth != null) && (!mConnection.isGetNameAddressOnly())) {
1108                if (DBG) Log.d(TAG,"Sending off request.");
1109
1110                try {
1111                    if(!mBluetooth.disable()) {
1112                        Log.e(TAG,"IBluetooth.disable() returned false");
1113                    }
1114                } catch (RemoteException e) {
1115                    Log.e(TAG,"Unable to call disable()",e);
1116                }
1117            }
1118        }
1119    }
1120
1121    private boolean checkIfCallerIsForegroundUser() {
1122        int foregroundUser;
1123        int callingUser = UserHandle.getCallingUserId();
1124        int callingUid = Binder.getCallingUid();
1125        long callingIdentity = Binder.clearCallingIdentity();
1126        int callingAppId = UserHandle.getAppId(callingUid);
1127        boolean valid = false;
1128        try {
1129            foregroundUser = ActivityManager.getCurrentUser();
1130            valid = (callingUser == foregroundUser) ||
1131                    callingAppId == Process.NFC_UID ||
1132                    callingAppId == mSystemUiUid;
1133            if (DBG) {
1134                Log.d(TAG, "checkIfCallerIsForegroundUser: valid=" + valid
1135                    + " callingUser=" + callingUser
1136                    + " foregroundUser=" + foregroundUser);
1137            }
1138        } finally {
1139            Binder.restoreCallingIdentity(callingIdentity);
1140        }
1141        return valid;
1142    }
1143
1144    private void bluetoothStateChangeHandler(int prevState, int newState) {
1145        if (prevState != newState) {
1146            //Notify all proxy objects first of adapter state change
1147            if (newState == BluetoothAdapter.STATE_ON || newState == BluetoothAdapter.STATE_OFF) {
1148                boolean isUp = (newState==BluetoothAdapter.STATE_ON);
1149                sendBluetoothStateCallback(isUp);
1150
1151                if (isUp) {
1152                    // connect to GattService
1153                    if (mContext.getPackageManager().hasSystemFeature(
1154                                                     PackageManager.FEATURE_BLUETOOTH_LE)) {
1155                        Intent i = new Intent(IBluetoothGatt.class.getName());
1156                        doBind(i, mConnection, Context.BIND_AUTO_CREATE, UserHandle.CURRENT);
1157                    }
1158                } else {
1159                    //If Bluetooth is off, send service down event to proxy objects, and unbind
1160                    if (!isUp && canUnbindBluetoothService()) {
1161                        sendBluetoothServiceDownCallback();
1162                        unbindAndFinish();
1163                    }
1164                }
1165            }
1166
1167            //Send broadcast message to everyone else
1168            Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
1169            intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState);
1170            intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState);
1171            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1172            if (DBG) Log.d(TAG,"Bluetooth State Change Intent: " + prevState + " -> " + newState);
1173            mContext.sendBroadcastAsUser(intent, UserHandle.ALL,
1174                    BLUETOOTH_PERM);
1175        }
1176    }
1177
1178    /**
1179     *  if on is true, wait for state become ON
1180     *  if off is true, wait for state become OFF
1181     *  if both on and off are false, wait for state not ON
1182     */
1183    private boolean waitForOnOff(boolean on, boolean off) {
1184        int i = 0;
1185        while (i < 10) {
1186            synchronized(mConnection) {
1187                try {
1188                    if (mBluetooth == null) break;
1189                    if (on) {
1190                        if (mBluetooth.getState() == BluetoothAdapter.STATE_ON) return true;
1191                    } else if (off) {
1192                        if (mBluetooth.getState() == BluetoothAdapter.STATE_OFF) return true;
1193                    } else {
1194                        if (mBluetooth.getState() != BluetoothAdapter.STATE_ON) return true;
1195                    }
1196                } catch (RemoteException e) {
1197                    Log.e(TAG, "getState()", e);
1198                    break;
1199                }
1200            }
1201            if (on || off) {
1202                SystemClock.sleep(300);
1203            } else {
1204                SystemClock.sleep(50);
1205            }
1206            i++;
1207        }
1208        Log.e(TAG,"waitForOnOff time out");
1209        return false;
1210    }
1211
1212    private void sendDisableMsg() {
1213        mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISABLE));
1214    }
1215
1216    private void sendEnableMsg(boolean quietMode) {
1217        mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_ENABLE,
1218                             quietMode ? 1 : 0, 0));
1219    }
1220
1221    private boolean canUnbindBluetoothService() {
1222        synchronized(mConnection) {
1223            //Only unbind with mEnable flag not set
1224            //For race condition: disable and enable back-to-back
1225            //Avoid unbind right after enable due to callback from disable
1226            //Only unbind with Bluetooth at OFF state
1227            //Only unbind without any MESSAGE_BLUETOOTH_STATE_CHANGE message
1228            try {
1229                if (mEnable || (mBluetooth == null)) return false;
1230                if (mHandler.hasMessages(MESSAGE_BLUETOOTH_STATE_CHANGE)) return false;
1231                return (mBluetooth.getState() == BluetoothAdapter.STATE_OFF);
1232            } catch (RemoteException e) {
1233                Log.e(TAG, "getState()", e);
1234            }
1235        }
1236        return false;
1237    }
1238
1239    private void recoverBluetoothServiceFromError() {
1240        Log.e(TAG,"recoverBluetoothServiceFromError");
1241        synchronized (mConnection) {
1242            if (mBluetooth != null) {
1243                //Unregister callback object
1244                try {
1245                    mBluetooth.unregisterCallback(mBluetoothCallback);
1246                } catch (RemoteException re) {
1247                    Log.e(TAG, "Unable to unregister",re);
1248                }
1249            }
1250        }
1251
1252        SystemClock.sleep(500);
1253
1254        // disable
1255        handleDisable();
1256
1257        waitForOnOff(false, true);
1258
1259        sendBluetoothServiceDownCallback();
1260        synchronized (mConnection) {
1261            if (mBluetooth != null) {
1262                mBluetooth = null;
1263                //Unbind
1264                mContext.unbindService(mConnection);
1265            }
1266        }
1267
1268        mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
1269        mState = BluetoothAdapter.STATE_OFF;
1270
1271        mEnable = false;
1272
1273        if (mErrorRecoveryRetryCounter++ < MAX_ERROR_RESTART_RETRIES) {
1274            // Send a Bluetooth Restart message to reenable bluetooth
1275            Message restartMsg = mHandler.obtainMessage(
1276                             MESSAGE_RESTART_BLUETOOTH_SERVICE);
1277            mHandler.sendMessageDelayed(restartMsg, ERROR_RESTART_TIME_MS);
1278        } else {
1279            // todo: notify user to power down and power up phone to make bluetooth work.
1280        }
1281    }
1282}
1283