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