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