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; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 21 | import android.bluetooth.BluetoothProfile; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 22 | import android.bluetooth.IBluetooth; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 23 | import android.bluetooth.IBluetoothGatt; |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 24 | import android.bluetooth.IBluetoothCallback; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 25 | import android.bluetooth.IBluetoothHeadset; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 26 | import android.bluetooth.IBluetoothManager; |
| 27 | import android.bluetooth.IBluetoothManagerCallback; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 28 | import android.bluetooth.IBluetoothProfileServiceConnection; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 29 | import android.bluetooth.IBluetoothStateChangeCallback; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 30 | import android.content.BroadcastReceiver; |
| 31 | import android.content.ComponentName; |
| 32 | import android.content.ContentResolver; |
| 33 | import android.content.Context; |
| 34 | import android.content.Intent; |
| 35 | import android.content.IntentFilter; |
| 36 | import android.content.ServiceConnection; |
Matthew Xie | 32ab77b | 2013-05-08 19:26:57 -0700 | [diff] [blame] | 37 | import android.content.pm.PackageManager; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 38 | import android.content.pm.UserInfo; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 39 | import android.os.Binder; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 40 | import android.os.Handler; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 41 | import android.os.IBinder; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 42 | import android.os.Looper; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 43 | import android.os.Message; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 44 | import android.os.Process; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 45 | import android.os.RemoteCallbackList; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 46 | import android.os.RemoteException; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 47 | import android.os.SystemClock; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 48 | import android.os.UserHandle; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 49 | import android.os.UserManager; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 50 | import android.provider.Settings; |
| 51 | import android.util.Log; |
Mike Lockwood | 726d4de | 2014-10-28 14:06:28 -0700 | [diff] [blame] | 52 | |
| 53 | import java.io.FileDescriptor; |
| 54 | import java.io.PrintWriter; |
| 55 | |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 56 | import java.util.HashMap; |
| 57 | import java.util.Map; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 58 | |
Nitin Arora | 6ddbb5e | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 59 | import java.util.*; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 60 | class BluetoothManagerService extends IBluetoothManager.Stub { |
| 61 | private static final String TAG = "BluetoothManagerService"; |
| 62 | private static final boolean DBG = true; |
| 63 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 64 | private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN; |
| 65 | private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 66 | private static final String ACTION_SERVICE_STATE_CHANGED="com.android.bluetooth.btservice.action.STATE_CHANGED"; |
| 67 | private static final String EXTRA_ACTION="action"; |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 68 | private static final String SECURE_SETTINGS_BLUETOOTH_ADDR_VALID="bluetooth_addr_valid"; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 69 | private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address"; |
| 70 | private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name"; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 71 | private static final int TIMEOUT_BIND_MS = 3000; //Maximum msec to wait for a bind |
| 72 | 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] | 73 | //Maximum msec to wait for service restart |
| 74 | private static final int SERVICE_RESTART_TIME_MS = 200; |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 75 | //Maximum msec to wait for restart due to error |
| 76 | private static final int ERROR_RESTART_TIME_MS = 3000; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 77 | //Maximum msec to delay MESSAGE_USER_SWITCHED |
| 78 | private static final int USER_SWITCHED_TIME_MS = 200; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 79 | // Delay for the addProxy function in msec |
| 80 | private static final int ADD_PROXY_DELAY_MS = 100; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 81 | |
| 82 | private static final int MESSAGE_ENABLE = 1; |
| 83 | private static final int MESSAGE_DISABLE = 2; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 84 | private static final int MESSAGE_REGISTER_ADAPTER = 20; |
| 85 | private static final int MESSAGE_UNREGISTER_ADAPTER = 21; |
| 86 | private static final int MESSAGE_REGISTER_STATE_CHANGE_CALLBACK = 30; |
| 87 | private static final int MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK = 31; |
| 88 | private static final int MESSAGE_BLUETOOTH_SERVICE_CONNECTED = 40; |
| 89 | private static final int MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED = 41; |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 90 | private static final int MESSAGE_RESTART_BLUETOOTH_SERVICE = 42; |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 91 | private static final int MESSAGE_BLUETOOTH_STATE_CHANGE=60; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 92 | private static final int MESSAGE_TIMEOUT_BIND =100; |
| 93 | private static final int MESSAGE_TIMEOUT_UNBIND =101; |
| 94 | private static final int MESSAGE_GET_NAME_AND_ADDRESS=200; |
| 95 | private static final int MESSAGE_SAVE_NAME_AND_ADDRESS=201; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 96 | private static final int MESSAGE_USER_SWITCHED = 300; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 97 | private static final int MESSAGE_ADD_PROXY_DELAYED = 400; |
| 98 | private static final int MESSAGE_BIND_PROFILE_SERVICE = 401; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 99 | private static final int MAX_SAVE_RETRIES=3; |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 100 | private static final int MAX_ERROR_RESTART_RETRIES=6; |
| 101 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 102 | // Bluetooth persisted setting is off |
| 103 | private static final int BLUETOOTH_OFF=0; |
| 104 | // Bluetooth persisted setting is on |
| 105 | // and Airplane mode won't affect Bluetooth state at start up |
| 106 | private static final int BLUETOOTH_ON_BLUETOOTH=1; |
| 107 | // Bluetooth persisted setting is on |
| 108 | // but Airplane mode will affect Bluetooth state at start up |
| 109 | // and Airplane mode will have higher priority. |
| 110 | private static final int BLUETOOTH_ON_AIRPLANE=2; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 111 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 112 | private static final int SERVICE_IBLUETOOTH = 1; |
| 113 | private static final int SERVICE_IBLUETOOTHGATT = 2; |
| 114 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 115 | private final Context mContext; |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 116 | private static int mBleAppCount = 0; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 117 | |
| 118 | // Locks are not provided for mName and mAddress. |
| 119 | // They are accessed in handler or broadcast receiver, same thread context. |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 120 | private String mAddress; |
| 121 | private String mName; |
Matthew Xie | 6fde309 | 2012-07-11 17:10:07 -0700 | [diff] [blame] | 122 | private final ContentResolver mContentResolver; |
| 123 | private final RemoteCallbackList<IBluetoothManagerCallback> mCallbacks; |
| 124 | private final RemoteCallbackList<IBluetoothStateChangeCallback> mStateChangeCallbacks; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 125 | private IBluetooth mBluetooth; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 126 | private IBluetoothGatt mBluetoothGatt; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 127 | private boolean mBinding; |
| 128 | private boolean mUnbinding; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 129 | // used inside handler thread |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 130 | private boolean mQuietEnable = false; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 131 | // configuarion from external IBinder call which is used to |
| 132 | // synchronize with broadcast receiver. |
| 133 | private boolean mQuietEnableExternal; |
| 134 | // configuarion from external IBinder call which is used to |
| 135 | // synchronize with broadcast receiver. |
| 136 | private boolean mEnableExternal; |
| 137 | // used inside handler thread |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 138 | private boolean mEnable; |
| 139 | private int mState; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 140 | private final BluetoothHandler mHandler; |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 141 | private int mErrorRecoveryRetryCounter; |
Adrian Roos | bd9a9a5 | 2014-08-18 15:31:57 +0200 | [diff] [blame] | 142 | private final int mSystemUiUid; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 143 | |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 144 | // Save a ProfileServiceConnections object for each of the bound |
| 145 | // bluetooth profile services |
| 146 | private final Map <Integer, ProfileServiceConnections> mProfileServices = |
| 147 | new HashMap <Integer, ProfileServiceConnections>(); |
| 148 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 149 | private void registerForAirplaneMode(IntentFilter filter) { |
| 150 | final ContentResolver resolver = mContext.getContentResolver(); |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 151 | final String airplaneModeRadios = Settings.Global.getString(resolver, |
| 152 | Settings.Global.AIRPLANE_MODE_RADIOS); |
| 153 | final String toggleableRadios = Settings.Global.getString(resolver, |
| 154 | Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 155 | boolean mIsAirplaneSensitive = airplaneModeRadios == null ? true : |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 156 | airplaneModeRadios.contains(Settings.Global.RADIO_BLUETOOTH); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 157 | if (mIsAirplaneSensitive) { |
| 158 | filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 159 | } |
| 160 | } |
| 161 | |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 162 | private final IBluetoothCallback mBluetoothCallback = new IBluetoothCallback.Stub() { |
| 163 | @Override |
| 164 | public void onBluetoothStateChange(int prevState, int newState) throws RemoteException { |
| 165 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_STATE_CHANGE,prevState,newState); |
| 166 | mHandler.sendMessage(msg); |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 171 | @Override |
| 172 | public void onReceive(Context context, Intent intent) { |
| 173 | String action = intent.getAction(); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 174 | if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action)) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 175 | String newName = intent.getStringExtra(BluetoothAdapter.EXTRA_LOCAL_NAME); |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 176 | if (DBG) Log.d(TAG, "Bluetooth Adapter name changed to " + newName); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 177 | if (newName != null) { |
| 178 | storeNameAndAddress(newName, null); |
| 179 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 180 | } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) { |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 181 | synchronized(mReceiver) { |
| 182 | if (isBluetoothPersistedStateOn()) { |
| 183 | if (isAirplaneModeOn()) { |
| 184 | persistBluetoothSetting(BLUETOOTH_ON_AIRPLANE); |
| 185 | } else { |
| 186 | persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH); |
| 187 | } |
| 188 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 189 | |
| 190 | int st = BluetoothAdapter.STATE_OFF; |
| 191 | if (mBluetooth != null) { |
| 192 | try { |
| 193 | st = mBluetooth.getState(); |
| 194 | } catch (RemoteException e) { |
| 195 | Log.e(TAG,"Unable to call getState", e); |
| 196 | } |
| 197 | } |
| 198 | Log.d(TAG, "state" + st); |
| 199 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 200 | if (isAirplaneModeOn()) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 201 | // Clear registered LE apps to force shut-off |
| 202 | synchronized (this) { |
| 203 | mBleAppCount = 0; |
| 204 | } |
| 205 | if (st == BluetoothAdapter.STATE_BLE_ON) { |
| 206 | //if state is BLE_ON make sure you trigger disableBLE part |
| 207 | try { |
| 208 | if (mBluetooth != null) { |
| 209 | mBluetooth.onBrEdrDown(); |
| 210 | mEnableExternal = false; |
| 211 | } |
| 212 | } catch(RemoteException e) { |
| 213 | Log.e(TAG,"Unable to call onBrEdrDown", e); |
| 214 | } |
| 215 | } else if (st == BluetoothAdapter.STATE_ON){ |
| 216 | // disable without persisting the setting |
| 217 | Log.d(TAG, "Calling disable"); |
| 218 | sendDisableMsg(); |
| 219 | } |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 220 | } else if (mEnableExternal) { |
| 221 | // enable without persisting the setting |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 222 | Log.d(TAG, "Calling enable"); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 223 | sendEnableMsg(mQuietEnableExternal); |
| 224 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 225 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 226 | } else if (Intent.ACTION_USER_SWITCHED.equals(action)) { |
| 227 | mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_USER_SWITCHED, |
| 228 | intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0), 0)); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 229 | } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { |
| 230 | synchronized(mReceiver) { |
| 231 | if (mEnableExternal && isBluetoothPersistedStateOnBluetooth()) { |
| 232 | //Enable |
| 233 | if (DBG) Log.d(TAG, "Auto-enabling Bluetooth."); |
| 234 | sendEnableMsg(mQuietEnableExternal); |
| 235 | } |
| 236 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | }; |
| 240 | |
| 241 | BluetoothManagerService(Context context) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 242 | mHandler = new BluetoothHandler(IoThread.get().getLooper()); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 243 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 244 | mContext = context; |
| 245 | mBluetooth = null; |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 246 | mBluetoothGatt = null; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 247 | mBinding = false; |
| 248 | mUnbinding = false; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 249 | mEnable = false; |
| 250 | mState = BluetoothAdapter.STATE_OFF; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 251 | mQuietEnableExternal = false; |
| 252 | mEnableExternal = false; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 253 | mAddress = null; |
| 254 | mName = null; |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 255 | mErrorRecoveryRetryCounter = 0; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 256 | mContentResolver = context.getContentResolver(); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 257 | mCallbacks = new RemoteCallbackList<IBluetoothManagerCallback>(); |
| 258 | mStateChangeCallbacks = new RemoteCallbackList<IBluetoothStateChangeCallback>(); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 259 | IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED); |
Matthew Xie | 6fde309 | 2012-07-11 17:10:07 -0700 | [diff] [blame] | 260 | filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 261 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Matthew Xie | 6fde309 | 2012-07-11 17:10:07 -0700 | [diff] [blame] | 262 | registerForAirplaneMode(filter); |
Dianne Hackborn | d83a096 | 2014-05-02 16:28:33 -0700 | [diff] [blame] | 263 | filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
Matthew Xie | 6fde309 | 2012-07-11 17:10:07 -0700 | [diff] [blame] | 264 | mContext.registerReceiver(mReceiver, filter); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 265 | loadStoredNameAndAddress(); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 266 | if (isBluetoothPersistedStateOn()) { |
| 267 | mEnableExternal = true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 268 | } |
Adrian Roos | bd9a9a5 | 2014-08-18 15:31:57 +0200 | [diff] [blame] | 269 | |
| 270 | int sysUiUid = -1; |
| 271 | try { |
| 272 | sysUiUid = mContext.getPackageManager().getPackageUid("com.android.systemui", |
| 273 | UserHandle.USER_OWNER); |
| 274 | } catch (PackageManager.NameNotFoundException e) { |
| 275 | Log.wtf(TAG, "Unable to resolve SystemUI's UID.", e); |
| 276 | } |
| 277 | mSystemUiUid = sysUiUid; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 278 | } |
| 279 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 280 | /** |
| 281 | * Returns true if airplane mode is currently on |
| 282 | */ |
| 283 | private final boolean isAirplaneModeOn() { |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 284 | return Settings.Global.getInt(mContext.getContentResolver(), |
| 285 | Settings.Global.AIRPLANE_MODE_ON, 0) == 1; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Returns true if the Bluetooth saved state is "on" |
| 290 | */ |
| 291 | private final boolean isBluetoothPersistedStateOn() { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 292 | return Settings.Global.getInt(mContentResolver, |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 293 | Settings.Global.BLUETOOTH_ON, 0) != BLUETOOTH_OFF; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Returns true if the Bluetooth saved state is BLUETOOTH_ON_BLUETOOTH |
| 298 | */ |
| 299 | private final boolean isBluetoothPersistedStateOnBluetooth() { |
| 300 | return Settings.Global.getInt(mContentResolver, |
| 301 | Settings.Global.BLUETOOTH_ON, 0) == BLUETOOTH_ON_BLUETOOTH; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Save the Bluetooth on/off state |
| 306 | * |
| 307 | */ |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 308 | private void persistBluetoothSetting(int value) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 309 | Settings.Global.putInt(mContext.getContentResolver(), |
| 310 | Settings.Global.BLUETOOTH_ON, |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 311 | value); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Returns true if the Bluetooth Adapter's name and address is |
| 316 | * locally cached |
| 317 | * @return |
| 318 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 319 | private boolean isNameAndAddressSet() { |
| 320 | return mName !=null && mAddress!= null && mName.length()>0 && mAddress.length()>0; |
| 321 | } |
| 322 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 323 | /** |
| 324 | * Retrieve the Bluetooth Adapter's name and address and save it in |
| 325 | * in the local cache |
| 326 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 327 | private void loadStoredNameAndAddress() { |
| 328 | if (DBG) Log.d(TAG, "Loading stored name and address"); |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 329 | if (mContext.getResources().getBoolean |
| 330 | (com.android.internal.R.bool.config_bluetooth_address_validation) && |
| 331 | Settings.Secure.getInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 0) == 0) { |
| 332 | // if the valid flag is not set, don't load the address and name |
| 333 | if (DBG) Log.d(TAG, "invalid bluetooth name and address stored"); |
| 334 | return; |
| 335 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 336 | mName = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME); |
| 337 | mAddress = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS); |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 338 | if (DBG) Log.d(TAG, "Stored bluetooth Name=" + mName + ",Address=" + mAddress); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 339 | } |
| 340 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 341 | /** |
| 342 | * Save the Bluetooth name and address in the persistent store. |
| 343 | * Only non-null values will be saved. |
| 344 | * @param name |
| 345 | * @param address |
| 346 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 347 | private void storeNameAndAddress(String name, String address) { |
| 348 | if (name != null) { |
| 349 | Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME, name); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 350 | mName = name; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 351 | if (DBG) Log.d(TAG,"Stored Bluetooth name: " + |
| 352 | Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_NAME)); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | if (address != null) { |
| 356 | Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS, address); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 357 | mAddress=address; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 358 | if (DBG) Log.d(TAG,"Stored Bluetoothaddress: " + |
| 359 | Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_ADDRESS)); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 360 | } |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 361 | |
| 362 | if ((name != null) && (address != null)) { |
| 363 | Settings.Secure.putInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 1); |
| 364 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | public IBluetooth registerAdapter(IBluetoothManagerCallback callback){ |
Natalie Silvanovich | 55db646 | 2014-05-01 16:12:23 -0700 | [diff] [blame] | 368 | if (callback == null) { |
| 369 | Log.w(TAG, "Callback is null in registerAdapter"); |
| 370 | return null; |
| 371 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 372 | Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_ADAPTER); |
| 373 | msg.obj = callback; |
| 374 | mHandler.sendMessage(msg); |
| 375 | synchronized(mConnection) { |
| 376 | return mBluetooth; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | public void unregisterAdapter(IBluetoothManagerCallback callback) { |
Natalie Silvanovich | 55db646 | 2014-05-01 16:12:23 -0700 | [diff] [blame] | 381 | if (callback == null) { |
| 382 | Log.w(TAG, "Callback is null in unregisterAdapter"); |
| 383 | return; |
| 384 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 385 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 386 | "Need BLUETOOTH permission"); |
| 387 | Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_ADAPTER); |
| 388 | msg.obj = callback; |
| 389 | mHandler.sendMessage(msg); |
| 390 | } |
| 391 | |
| 392 | public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) { |
| 393 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 394 | "Need BLUETOOTH permission"); |
| 395 | Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_STATE_CHANGE_CALLBACK); |
| 396 | msg.obj = callback; |
| 397 | mHandler.sendMessage(msg); |
| 398 | } |
| 399 | |
| 400 | public void unregisterStateChangeCallback(IBluetoothStateChangeCallback callback) { |
| 401 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 402 | "Need BLUETOOTH permission"); |
| 403 | Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK); |
| 404 | msg.obj = callback; |
| 405 | mHandler.sendMessage(msg); |
| 406 | } |
| 407 | |
| 408 | public boolean isEnabled() { |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 409 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 410 | (!checkIfCallerIsForegroundUser())) { |
| 411 | 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] | 412 | return false; |
| 413 | } |
| 414 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 415 | synchronized(mConnection) { |
| 416 | try { |
| 417 | return (mBluetooth != null && mBluetooth.isEnabled()); |
| 418 | } catch (RemoteException e) { |
| 419 | Log.e(TAG, "isEnabled()", e); |
| 420 | } |
| 421 | } |
| 422 | return false; |
| 423 | } |
| 424 | |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 425 | class ClientDeathRecipient implements IBinder.DeathRecipient { |
| 426 | public void binderDied() { |
| 427 | if (DBG) Log.d(TAG, "Binder is dead - unregister Ble App"); |
| 428 | if (mBleAppCount > 0) --mBleAppCount; |
| 429 | |
| 430 | if (mBleAppCount == 0) { |
| 431 | if (DBG) Log.d(TAG, "Disabling LE only mode after application crash"); |
| 432 | try { |
| 433 | if (mBluetooth != null) { |
| 434 | mBluetooth.onBrEdrDown(); |
| 435 | } |
| 436 | } catch(RemoteException e) { |
| 437 | Log.e(TAG,"Unable to call onBrEdrDown", e); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | /** Internal death rec list */ |
| 444 | Map<IBinder, ClientDeathRecipient> mBleApps = new HashMap<IBinder, ClientDeathRecipient>(); |
| 445 | |
| 446 | public int updateBleAppCount(IBinder token, boolean enable) { |
| 447 | if (enable) { |
| 448 | ClientDeathRecipient r = mBleApps.get(token); |
| 449 | if (r == null) { |
| 450 | ClientDeathRecipient deathRec = new ClientDeathRecipient(); |
| 451 | try { |
| 452 | token.linkToDeath(deathRec, 0); |
| 453 | } catch (RemoteException ex) { |
| 454 | throw new IllegalArgumentException("Wake lock is already dead."); |
| 455 | } |
| 456 | mBleApps.put(token, deathRec); |
| 457 | synchronized (this) { |
| 458 | ++mBleAppCount; |
| 459 | } |
| 460 | if (DBG) Log.d(TAG, "Registered for death Notification"); |
| 461 | } |
| 462 | |
| 463 | } else { |
| 464 | ClientDeathRecipient r = mBleApps.get(token); |
| 465 | if (r != null) { |
| 466 | try { |
| 467 | token.linkToDeath(r, 0); |
| 468 | } catch (RemoteException ex) { |
| 469 | throw new IllegalArgumentException("Wake lock is already dead."); |
| 470 | } |
| 471 | mBleApps.remove(token); |
| 472 | synchronized (this) { |
| 473 | if (mBleAppCount > 0) --mBleAppCount; |
| 474 | } |
| 475 | if (DBG) Log.d(TAG, "Unregistered for death Notification"); |
| 476 | } |
| 477 | } |
| 478 | if (DBG) Log.d(TAG, "Updated BleAppCount" + mBleAppCount); |
| 479 | if (mBleAppCount == 0 && mEnable) { |
| 480 | try { |
| 481 | if (mBluetooth != null && (mBluetooth.getState() != BluetoothAdapter.STATE_ON)) { |
| 482 | if (DBG) Log.d(TAG, "Reseting the mEnable flag for clean disable"); |
| 483 | mEnable = false; |
| 484 | } |
| 485 | } catch (RemoteException e) { |
| 486 | Log.e(TAG, "getState()", e); |
| 487 | } |
| 488 | } |
| 489 | return mBleAppCount; |
| 490 | } |
| 491 | |
| 492 | /** @hide*/ |
| 493 | public boolean isBleAppPresent() { |
| 494 | if (DBG) Log.d(TAG, "isBleAppPresent() count: " + mBleAppCount); |
| 495 | return (mBleAppCount > 0); |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * Action taken when GattService is turned off |
| 500 | */ |
| 501 | private void onBluetoothGattServiceUp() { |
| 502 | if (DBG) Log.d(TAG,"BluetoothGatt Service is Up"); |
| 503 | try{ |
| 504 | if (isBleAppPresent() == false && mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON) { |
| 505 | mBluetooth.onLeServiceUp(); |
| 506 | |
| 507 | // waive WRITE_SECURE_SETTINGS permission check |
| 508 | long callingIdentity = Binder.clearCallingIdentity(); |
| 509 | persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH); |
| 510 | Binder.restoreCallingIdentity(callingIdentity); |
| 511 | } |
| 512 | } catch(RemoteException e) { |
| 513 | Log.e(TAG,"Unable to call onServiceUp", e); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Inform BluetoothAdapter instances that BREDR part is down |
| 519 | * and turn off all service and stack if no LE app needs it |
| 520 | */ |
| 521 | private void sendBrEdrDownCallback() { |
| 522 | if (DBG) Log.d(TAG,"Calling sendBrEdrDownCallback callbacks"); |
| 523 | int n = mCallbacks.beginBroadcast(); |
| 524 | |
| 525 | if (isBleAppPresent() == false) { |
| 526 | try { |
| 527 | mBluetooth.onBrEdrDown(); |
| 528 | } catch(RemoteException e) { |
| 529 | Log.e(TAG,"Unable to call onBrEdrDown", e); |
| 530 | } |
| 531 | } |
| 532 | else{//need to stay at BLE ON. disconnect all Gatt connections |
| 533 | try{ |
| 534 | mBluetoothGatt.unregAll();//disconnectAll(); |
| 535 | } catch(RemoteException e) { |
| 536 | Log.e(TAG,"Unable to disconn all", e); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | Log.d(TAG,"Broadcasting onBrEdrDown() to " + n + " receivers."); |
| 541 | for (int i=0; i <n; i++) { |
| 542 | try { |
| 543 | mCallbacks.getBroadcastItem(i).onBrEdrDown(); |
| 544 | } catch (RemoteException e) { |
| 545 | Log.e(TAG, "Unable to call sendBrEdrDownCallback() on callback #" + i, e); |
| 546 | } |
| 547 | } |
| 548 | mCallbacks.finishBroadcast(); |
| 549 | } |
| 550 | |
| 551 | /** @hide*/ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 552 | public void getNameAndAddress() { |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 553 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 554 | Log.d(TAG,"getNameAndAddress(): mBluetooth = " + mBluetooth + |
| 555 | " mBinding = " + mBinding); |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 556 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 557 | Message msg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS); |
| 558 | mHandler.sendMessage(msg); |
| 559 | } |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 560 | public boolean enableNoAutoConnect() |
| 561 | { |
| 562 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 563 | "Need BLUETOOTH ADMIN permission"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 564 | |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 565 | if (DBG) { |
| 566 | Log.d(TAG,"enableNoAutoConnect(): mBluetooth =" + mBluetooth + |
| 567 | " mBinding = " + mBinding); |
| 568 | } |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 569 | int callingAppId = UserHandle.getAppId(Binder.getCallingUid()); |
| 570 | |
| 571 | if (callingAppId != Process.NFC_UID) { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 572 | throw new SecurityException("no permission to enable Bluetooth quietly"); |
| 573 | } |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 574 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 575 | synchronized(mReceiver) { |
| 576 | mQuietEnableExternal = true; |
| 577 | mEnableExternal = true; |
| 578 | sendEnableMsg(true); |
| 579 | } |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 580 | return true; |
| 581 | |
| 582 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 583 | public boolean enable() { |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 584 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 585 | (!checkIfCallerIsForegroundUser())) { |
| 586 | 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] | 587 | return false; |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 590 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 591 | "Need BLUETOOTH ADMIN permission"); |
| 592 | if (DBG) { |
| 593 | Log.d(TAG,"enable(): mBluetooth =" + mBluetooth + |
| 594 | " mBinding = " + mBinding); |
| 595 | } |
| 596 | |
| 597 | synchronized(mReceiver) { |
| 598 | mQuietEnableExternal = false; |
| 599 | mEnableExternal = true; |
| 600 | // waive WRITE_SECURE_SETTINGS permission check |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 601 | sendEnableMsg(false); |
| 602 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 603 | if (DBG) Log.d(TAG, "enable returning"); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 604 | return true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | public boolean disable(boolean persist) { |
| 608 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 609 | "Need BLUETOOTH ADMIN permissicacheNameAndAddresson"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 610 | |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 611 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 612 | (!checkIfCallerIsForegroundUser())) { |
| 613 | 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] | 614 | return false; |
| 615 | } |
| 616 | |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 617 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 618 | Log.d(TAG,"disable(): mBluetooth = " + mBluetooth + |
| 619 | " mBinding = " + mBinding); |
| 620 | } |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 621 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 622 | synchronized(mReceiver) { |
| 623 | if (persist) { |
| 624 | // waive WRITE_SECURE_SETTINGS permission check |
| 625 | long callingIdentity = Binder.clearCallingIdentity(); |
| 626 | persistBluetoothSetting(BLUETOOTH_OFF); |
| 627 | Binder.restoreCallingIdentity(callingIdentity); |
| 628 | } |
| 629 | mEnableExternal = false; |
| 630 | sendDisableMsg(); |
| 631 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 632 | return true; |
| 633 | } |
| 634 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 635 | public void unbindAndFinish() { |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 636 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 637 | Log.d(TAG,"unbindAndFinish(): " + mBluetooth + |
| 638 | " mBinding = " + mBinding); |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 639 | } |
| 640 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 641 | synchronized (mConnection) { |
| 642 | if (mUnbinding) return; |
| 643 | mUnbinding = true; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 644 | if (mBluetooth != null) { |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 645 | if (!mConnection.isGetNameAddressOnly()) { |
| 646 | //Unregister callback object |
| 647 | try { |
| 648 | mBluetooth.unregisterCallback(mBluetoothCallback); |
| 649 | } catch (RemoteException re) { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 650 | Log.e(TAG, "Unable to unregister BluetoothCallback",re); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 651 | } |
| 652 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 653 | if (DBG) Log.d(TAG, "Sending unbind request."); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 654 | mBluetooth = null; |
| 655 | //Unbind |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 656 | mContext.unbindService(mConnection); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 657 | mUnbinding = false; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 658 | mBinding = false; |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 659 | } else { |
| 660 | mUnbinding=false; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 661 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 662 | mBluetoothGatt = null; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 666 | public IBluetoothGatt getBluetoothGatt() { |
| 667 | // sync protection |
| 668 | return mBluetoothGatt; |
| 669 | } |
| 670 | |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 671 | @Override |
| 672 | public boolean bindBluetoothProfileService(int bluetoothProfile, |
| 673 | IBluetoothProfileServiceConnection proxy) { |
| 674 | if (!mEnable) { |
| 675 | if (DBG) { |
| 676 | Log.d(TAG, "Trying to bind to profile: " + bluetoothProfile + |
| 677 | ", while Bluetooth was disabled"); |
| 678 | } |
| 679 | return false; |
| 680 | } |
| 681 | synchronized (mProfileServices) { |
| 682 | ProfileServiceConnections psc = mProfileServices.get(new Integer(bluetoothProfile)); |
| 683 | if (psc == null) { |
| 684 | if (DBG) { |
| 685 | Log.d(TAG, "Creating new ProfileServiceConnections object for" |
| 686 | + " profile: " + bluetoothProfile); |
| 687 | } |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 688 | |
| 689 | if (bluetoothProfile != BluetoothProfile.HEADSET) return false; |
| 690 | |
| 691 | Intent intent = new Intent(IBluetoothHeadset.class.getName()); |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 692 | psc = new ProfileServiceConnections(intent); |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 693 | if (!psc.bindService()) return false; |
| 694 | |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 695 | mProfileServices.put(new Integer(bluetoothProfile), psc); |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
| 699 | // Introducing a delay to give the client app time to prepare |
| 700 | Message addProxyMsg = mHandler.obtainMessage(MESSAGE_ADD_PROXY_DELAYED); |
| 701 | addProxyMsg.arg1 = bluetoothProfile; |
| 702 | addProxyMsg.obj = proxy; |
| 703 | mHandler.sendMessageDelayed(addProxyMsg, ADD_PROXY_DELAY_MS); |
| 704 | return true; |
| 705 | } |
| 706 | |
| 707 | @Override |
| 708 | public void unbindBluetoothProfileService(int bluetoothProfile, |
| 709 | IBluetoothProfileServiceConnection proxy) { |
| 710 | synchronized (mProfileServices) { |
| 711 | ProfileServiceConnections psc = mProfileServices.get(new Integer(bluetoothProfile)); |
| 712 | if (psc == null) { |
| 713 | return; |
| 714 | } |
| 715 | psc.removeProxy(proxy); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | private void unbindAllBluetoothProfileServices() { |
| 720 | synchronized (mProfileServices) { |
| 721 | for (Integer i : mProfileServices.keySet()) { |
| 722 | ProfileServiceConnections psc = mProfileServices.get(i); |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 723 | try { |
| 724 | mContext.unbindService(psc); |
| 725 | } catch (IllegalArgumentException e) { |
| 726 | Log.e(TAG, "Unable to unbind service with intent: " + psc.mIntent, e); |
| 727 | } |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 728 | psc.removeAllProxies(); |
| 729 | } |
| 730 | mProfileServices.clear(); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * This class manages the clients connected to a given ProfileService |
| 736 | * and maintains the connection with that service. |
| 737 | */ |
| 738 | final private class ProfileServiceConnections implements ServiceConnection, |
| 739 | IBinder.DeathRecipient { |
| 740 | final RemoteCallbackList<IBluetoothProfileServiceConnection> mProxies = |
| 741 | new RemoteCallbackList <IBluetoothProfileServiceConnection>(); |
| 742 | IBinder mService; |
| 743 | ComponentName mClassName; |
| 744 | Intent mIntent; |
| 745 | |
| 746 | ProfileServiceConnections(Intent intent) { |
| 747 | mService = null; |
| 748 | mClassName = null; |
| 749 | mIntent = intent; |
| 750 | } |
| 751 | |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 752 | private boolean bindService() { |
| 753 | if (mIntent != null && mService == null && |
| 754 | doBind(mIntent, this, 0, UserHandle.CURRENT_OR_SELF)) { |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 755 | Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE); |
| 756 | msg.obj = this; |
| 757 | mHandler.sendMessageDelayed(msg, TIMEOUT_BIND_MS); |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 758 | return true; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 759 | } |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 760 | Log.w(TAG, "Unable to bind with intent: " + mIntent); |
| 761 | return false; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | private void addProxy(IBluetoothProfileServiceConnection proxy) { |
| 765 | mProxies.register(proxy); |
| 766 | if (mService != null) { |
| 767 | try{ |
| 768 | proxy.onServiceConnected(mClassName, mService); |
| 769 | } catch (RemoteException e) { |
| 770 | Log.e(TAG, "Unable to connect to proxy", e); |
| 771 | } |
| 772 | } else { |
| 773 | if (!mHandler.hasMessages(MESSAGE_BIND_PROFILE_SERVICE, this)) { |
| 774 | Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE); |
| 775 | msg.obj = this; |
| 776 | mHandler.sendMessage(msg); |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | private void removeProxy(IBluetoothProfileServiceConnection proxy) { |
| 782 | if (proxy != null) { |
| 783 | if (mProxies.unregister(proxy)) { |
| 784 | try { |
| 785 | proxy.onServiceDisconnected(mClassName); |
| 786 | } catch (RemoteException e) { |
| 787 | Log.e(TAG, "Unable to disconnect proxy", e); |
| 788 | } |
| 789 | } |
| 790 | } else { |
| 791 | Log.w(TAG, "Trying to remove a null proxy"); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | private void removeAllProxies() { |
| 796 | onServiceDisconnected(mClassName); |
| 797 | mProxies.kill(); |
| 798 | } |
| 799 | |
| 800 | @Override |
| 801 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 802 | // remove timeout message |
| 803 | mHandler.removeMessages(MESSAGE_BIND_PROFILE_SERVICE, this); |
| 804 | mService = service; |
| 805 | mClassName = className; |
| 806 | try { |
| 807 | mService.linkToDeath(this, 0); |
| 808 | } catch (RemoteException e) { |
| 809 | Log.e(TAG, "Unable to linkToDeath", e); |
| 810 | } |
| 811 | int n = mProxies.beginBroadcast(); |
| 812 | for (int i = 0; i < n; i++) { |
| 813 | try { |
| 814 | mProxies.getBroadcastItem(i).onServiceConnected(className, service); |
| 815 | } catch (RemoteException e) { |
| 816 | Log.e(TAG, "Unable to connect to proxy", e); |
| 817 | } |
| 818 | } |
| 819 | mProxies.finishBroadcast(); |
| 820 | } |
| 821 | |
| 822 | @Override |
| 823 | public void onServiceDisconnected(ComponentName className) { |
| 824 | if (mService == null) { |
| 825 | return; |
| 826 | } |
| 827 | mService.unlinkToDeath(this, 0); |
| 828 | mService = null; |
| 829 | mClassName = null; |
| 830 | int n = mProxies.beginBroadcast(); |
| 831 | for (int i = 0; i < n; i++) { |
| 832 | try { |
| 833 | mProxies.getBroadcastItem(i).onServiceDisconnected(className); |
| 834 | } catch (RemoteException e) { |
| 835 | Log.e(TAG, "Unable to disconnect from proxy", e); |
| 836 | } |
| 837 | } |
| 838 | mProxies.finishBroadcast(); |
| 839 | } |
| 840 | |
| 841 | @Override |
| 842 | public void binderDied() { |
| 843 | if (DBG) { |
| 844 | Log.w(TAG, "Profile service for profile: " + mClassName |
| 845 | + " died."); |
| 846 | } |
| 847 | onServiceDisconnected(mClassName); |
| 848 | // Trigger rebind |
| 849 | Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE); |
| 850 | msg.obj = this; |
| 851 | mHandler.sendMessageDelayed(msg, TIMEOUT_BIND_MS); |
| 852 | } |
| 853 | } |
| 854 | |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 855 | private void sendBluetoothStateCallback(boolean isUp) { |
| 856 | int n = mStateChangeCallbacks.beginBroadcast(); |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 857 | if (DBG) Log.d(TAG,"Broadcasting onBluetoothStateChange("+isUp+") to " + n + " receivers."); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 858 | for (int i=0; i <n;i++) { |
| 859 | try { |
| 860 | mStateChangeCallbacks.getBroadcastItem(i).onBluetoothStateChange(isUp); |
| 861 | } catch (RemoteException e) { |
| 862 | Log.e(TAG, "Unable to call onBluetoothStateChange() on callback #" + i , e); |
| 863 | } |
| 864 | } |
| 865 | mStateChangeCallbacks.finishBroadcast(); |
| 866 | } |
| 867 | |
| 868 | /** |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 869 | * Inform BluetoothAdapter instances that Adapter service is up |
| 870 | */ |
| 871 | private void sendBluetoothServiceUpCallback() { |
| 872 | if (!mConnection.isGetNameAddressOnly()) { |
| 873 | if (DBG) Log.d(TAG,"Calling onBluetoothServiceUp callbacks"); |
| 874 | int n = mCallbacks.beginBroadcast(); |
| 875 | Log.d(TAG,"Broadcasting onBluetoothServiceUp() to " + n + " receivers."); |
| 876 | for (int i=0; i <n;i++) { |
| 877 | try { |
| 878 | mCallbacks.getBroadcastItem(i).onBluetoothServiceUp(mBluetooth); |
| 879 | } catch (RemoteException e) { |
| 880 | Log.e(TAG, "Unable to call onBluetoothServiceUp() on callback #" + i, e); |
| 881 | } |
| 882 | } |
| 883 | mCallbacks.finishBroadcast(); |
| 884 | } |
| 885 | } |
| 886 | /** |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 887 | * Inform BluetoothAdapter instances that Adapter service is down |
| 888 | */ |
| 889 | private void sendBluetoothServiceDownCallback() { |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 890 | if (!mConnection.isGetNameAddressOnly()) { |
| 891 | if (DBG) Log.d(TAG,"Calling onBluetoothServiceDown callbacks"); |
| 892 | int n = mCallbacks.beginBroadcast(); |
| 893 | Log.d(TAG,"Broadcasting onBluetoothServiceDown() to " + n + " receivers."); |
| 894 | for (int i=0; i <n;i++) { |
| 895 | try { |
| 896 | mCallbacks.getBroadcastItem(i).onBluetoothServiceDown(); |
| 897 | } catch (RemoteException e) { |
| 898 | Log.e(TAG, "Unable to call onBluetoothServiceDown() on callback #" + i, e); |
| 899 | } |
| 900 | } |
| 901 | mCallbacks.finishBroadcast(); |
| 902 | } |
| 903 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 904 | public String getAddress() { |
Matthew Xie | af5ddbf | 2012-12-04 10:47:43 -0800 | [diff] [blame] | 905 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 906 | "Need BLUETOOTH permission"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 907 | |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 908 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 909 | (!checkIfCallerIsForegroundUser())) { |
| 910 | Log.w(TAG,"getAddress(): not allowed for non-active and non system user"); |
| 911 | return null; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 912 | } |
| 913 | |
fredc | 116d1d46 | 2012-04-20 14:47:08 -0700 | [diff] [blame] | 914 | synchronized(mConnection) { |
| 915 | if (mBluetooth != null) { |
| 916 | try { |
| 917 | return mBluetooth.getAddress(); |
| 918 | } catch (RemoteException e) { |
| 919 | Log.e(TAG, "getAddress(): Unable to retrieve address remotely..Returning cached address",e); |
| 920 | } |
| 921 | } |
| 922 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 923 | // mAddress is accessed from outside. |
| 924 | // It is alright without a lock. Here, bluetooth is off, no other thread is |
| 925 | // changing mAddress |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 926 | return mAddress; |
| 927 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 928 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 929 | public String getName() { |
Matthew Xie | af5ddbf | 2012-12-04 10:47:43 -0800 | [diff] [blame] | 930 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 931 | "Need BLUETOOTH permission"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 932 | |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 933 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 934 | (!checkIfCallerIsForegroundUser())) { |
| 935 | Log.w(TAG,"getName(): not allowed for non-active and non system user"); |
| 936 | return null; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 937 | } |
| 938 | |
fredc | 116d1d46 | 2012-04-20 14:47:08 -0700 | [diff] [blame] | 939 | synchronized(mConnection) { |
| 940 | if (mBluetooth != null) { |
| 941 | try { |
| 942 | return mBluetooth.getName(); |
| 943 | } catch (RemoteException e) { |
| 944 | Log.e(TAG, "getName(): Unable to retrieve name remotely..Returning cached name",e); |
| 945 | } |
| 946 | } |
| 947 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 948 | // mName is accessed from outside. |
| 949 | // It alright without a lock. Here, bluetooth is off, no other thread is |
| 950 | // changing mName |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 951 | return mName; |
| 952 | } |
| 953 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 954 | private class BluetoothServiceConnection implements ServiceConnection { |
| 955 | |
| 956 | private boolean mGetNameAddressOnly; |
| 957 | |
| 958 | public void setGetNameAddressOnly(boolean getOnly) { |
| 959 | mGetNameAddressOnly = getOnly; |
| 960 | } |
| 961 | |
| 962 | public boolean isGetNameAddressOnly() { |
| 963 | return mGetNameAddressOnly; |
| 964 | } |
| 965 | |
| 966 | public void onServiceConnected(ComponentName className, IBinder service) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 967 | if (DBG) Log.d(TAG, "BluetoothServiceConnection: " + className.getClassName()); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 968 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 969 | // TBD if (className.getClassName().equals(IBluetooth.class.getName())) { |
| 970 | if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) { |
| 971 | msg.arg1 = SERVICE_IBLUETOOTH; |
| 972 | // } else if (className.getClassName().equals(IBluetoothGatt.class.getName())) { |
| 973 | } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) { |
| 974 | msg.arg1 = SERVICE_IBLUETOOTHGATT; |
| 975 | } else { |
| 976 | Log.e(TAG, "Unknown service connected: " + className.getClassName()); |
| 977 | return; |
| 978 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 979 | msg.obj = service; |
| 980 | mHandler.sendMessage(msg); |
| 981 | } |
| 982 | |
| 983 | public void onServiceDisconnected(ComponentName className) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 984 | // Called if we unexpected disconnected. |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 985 | if (DBG) Log.d(TAG, "BluetoothServiceConnection, disconnected: " + |
| 986 | className.getClassName()); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 987 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 988 | if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) { |
| 989 | msg.arg1 = SERVICE_IBLUETOOTH; |
| 990 | } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) { |
| 991 | msg.arg1 = SERVICE_IBLUETOOTHGATT; |
| 992 | } else { |
| 993 | Log.e(TAG, "Unknown service disconnected: " + className.getClassName()); |
| 994 | return; |
| 995 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 996 | mHandler.sendMessage(msg); |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | private BluetoothServiceConnection mConnection = new BluetoothServiceConnection(); |
| 1001 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1002 | private class BluetoothHandler extends Handler { |
| 1003 | public BluetoothHandler(Looper looper) { |
| 1004 | super(looper); |
| 1005 | } |
| 1006 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1007 | @Override |
| 1008 | public void handleMessage(Message msg) { |
| 1009 | if (DBG) Log.d (TAG, "Message: " + msg.what); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1010 | switch (msg.what) { |
| 1011 | case MESSAGE_GET_NAME_AND_ADDRESS: { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1012 | if (DBG) Log.d(TAG,"MESSAGE_GET_NAME_AND_ADDRESS"); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1013 | synchronized(mConnection) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1014 | //Start bind request |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1015 | if ((mBluetooth == null) && (!mBinding)) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1016 | if (DBG) Log.d(TAG, "Binding to service to get name and address"); |
| 1017 | mConnection.setGetNameAddressOnly(true); |
| 1018 | //Start bind timeout and bind |
| 1019 | Message timeoutMsg = mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND); |
| 1020 | mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS); |
| 1021 | Intent i = new Intent(IBluetooth.class.getName()); |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 1022 | if (!doBind(i, mConnection, |
Dianne Hackborn | ce09f5a | 2014-10-10 15:03:13 -0700 | [diff] [blame] | 1023 | Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, |
| 1024 | UserHandle.CURRENT)) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1025 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1026 | } else { |
| 1027 | mBinding = true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1028 | } |
| 1029 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1030 | else { |
| 1031 | Message saveMsg= mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1032 | saveMsg.arg1 = 0; |
| 1033 | if (mBluetooth != null) { |
| 1034 | mHandler.sendMessage(saveMsg); |
| 1035 | } else { |
| 1036 | // if enable is also called to bind the service |
| 1037 | // wait for MESSAGE_BLUETOOTH_SERVICE_CONNECTED |
| 1038 | mHandler.sendMessageDelayed(saveMsg, TIMEOUT_SAVE_MS); |
| 1039 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1040 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1041 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1042 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1043 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1044 | case MESSAGE_SAVE_NAME_AND_ADDRESS: { |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 1045 | boolean unbind = false; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1046 | if (DBG) Log.d(TAG,"MESSAGE_SAVE_NAME_AND_ADDRESS"); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1047 | synchronized(mConnection) { |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 1048 | if (!mEnable && mBluetooth != null) { |
| 1049 | try { |
| 1050 | mBluetooth.enable(); |
| 1051 | } catch (RemoteException e) { |
| 1052 | Log.e(TAG,"Unable to call enable()",e); |
| 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | if (mBluetooth != null) waitForOnOff(true, false); |
| 1057 | synchronized(mConnection) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1058 | if (mBluetooth != null) { |
| 1059 | String name = null; |
| 1060 | String address = null; |
| 1061 | try { |
| 1062 | name = mBluetooth.getName(); |
| 1063 | address = mBluetooth.getAddress(); |
| 1064 | } catch (RemoteException re) { |
| 1065 | Log.e(TAG,"",re); |
| 1066 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1067 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1068 | if (name != null && address != null) { |
| 1069 | storeNameAndAddress(name,address); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1070 | if (mConnection.isGetNameAddressOnly()) { |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 1071 | unbind = true; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1072 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1073 | } else { |
| 1074 | if (msg.arg1 < MAX_SAVE_RETRIES) { |
| 1075 | Message retryMsg = mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS); |
| 1076 | retryMsg.arg1= 1+msg.arg1; |
| 1077 | if (DBG) Log.d(TAG,"Retrying name/address remote retrieval and save.....Retry count =" + retryMsg.arg1); |
| 1078 | mHandler.sendMessageDelayed(retryMsg, TIMEOUT_SAVE_MS); |
| 1079 | } else { |
| 1080 | Log.w(TAG,"Maximum name/address remote retrieval retry exceeded"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1081 | if (mConnection.isGetNameAddressOnly()) { |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 1082 | unbind = true; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1083 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1084 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1085 | } |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 1086 | if (!mEnable) { |
| 1087 | try { |
| 1088 | mBluetooth.disable(); |
| 1089 | } catch (RemoteException e) { |
| 1090 | Log.e(TAG,"Unable to call disable()",e); |
| 1091 | } |
| 1092 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1093 | } else { |
| 1094 | // rebind service by Request GET NAME AND ADDRESS |
| 1095 | // if service is unbinded by disable or |
| 1096 | // MESSAGE_BLUETOOTH_SERVICE_CONNECTED is not received |
| 1097 | Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS); |
| 1098 | mHandler.sendMessage(getMsg); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1099 | } |
| 1100 | } |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 1101 | if (!mEnable && mBluetooth != null) waitForOnOff(false, true); |
| 1102 | if (unbind) { |
| 1103 | unbindAndFinish(); |
| 1104 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1105 | break; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1106 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1107 | case MESSAGE_ENABLE: |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 1108 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1109 | Log.d(TAG, "MESSAGE_ENABLE: mBluetooth = " + mBluetooth); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1110 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1111 | mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 1112 | mEnable = true; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1113 | handleEnable(msg.arg1 == 1); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1114 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1115 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1116 | case MESSAGE_DISABLE: |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1117 | mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 1118 | if (mEnable && mBluetooth != null) { |
| 1119 | waitForOnOff(true, false); |
| 1120 | mEnable = false; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1121 | handleDisable(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1122 | waitForOnOff(false, false); |
| 1123 | } else { |
| 1124 | mEnable = false; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1125 | handleDisable(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1126 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1127 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1128 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1129 | case MESSAGE_REGISTER_ADAPTER: |
| 1130 | { |
| 1131 | IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 1132 | boolean added = mCallbacks.register(callback); |
| 1133 | Log.d(TAG,"Added callback: " + (callback == null? "null": callback) +":" +added ); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1134 | } |
| 1135 | break; |
| 1136 | case MESSAGE_UNREGISTER_ADAPTER: |
| 1137 | { |
| 1138 | IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 1139 | boolean removed = mCallbacks.unregister(callback); |
| 1140 | Log.d(TAG,"Removed callback: " + (callback == null? "null": callback) +":" + removed); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1141 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1142 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1143 | case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK: |
| 1144 | { |
| 1145 | IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj; |
Matthew Xie | 9b69399 | 2013-10-10 11:21:40 -0700 | [diff] [blame] | 1146 | if (callback != null) { |
| 1147 | mStateChangeCallbacks.register(callback); |
| 1148 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1149 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1150 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1151 | case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK: |
| 1152 | { |
| 1153 | IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj; |
Matthew Xie | 9b69399 | 2013-10-10 11:21:40 -0700 | [diff] [blame] | 1154 | if (callback != null) { |
| 1155 | mStateChangeCallbacks.unregister(callback); |
| 1156 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1157 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1158 | } |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1159 | case MESSAGE_ADD_PROXY_DELAYED: |
| 1160 | { |
| 1161 | ProfileServiceConnections psc = mProfileServices.get( |
| 1162 | new Integer(msg.arg1)); |
| 1163 | if (psc == null) { |
| 1164 | break; |
| 1165 | } |
| 1166 | IBluetoothProfileServiceConnection proxy = |
| 1167 | (IBluetoothProfileServiceConnection) msg.obj; |
| 1168 | psc.addProxy(proxy); |
| 1169 | break; |
| 1170 | } |
| 1171 | case MESSAGE_BIND_PROFILE_SERVICE: |
| 1172 | { |
| 1173 | ProfileServiceConnections psc = (ProfileServiceConnections) msg.obj; |
| 1174 | removeMessages(MESSAGE_BIND_PROFILE_SERVICE, msg.obj); |
| 1175 | if (psc == null) { |
| 1176 | break; |
| 1177 | } |
| 1178 | psc.bindService(); |
| 1179 | break; |
| 1180 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1181 | case MESSAGE_BLUETOOTH_SERVICE_CONNECTED: |
| 1182 | { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1183 | if (DBG) Log.d(TAG,"MESSAGE_BLUETOOTH_SERVICE_CONNECTED: " + msg.arg1); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1184 | |
| 1185 | IBinder service = (IBinder) msg.obj; |
| 1186 | synchronized(mConnection) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1187 | if (msg.arg1 == SERVICE_IBLUETOOTHGATT) { |
| 1188 | mBluetoothGatt = IBluetoothGatt.Stub.asInterface(service); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1189 | onBluetoothGattServiceUp(); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1190 | break; |
| 1191 | } // else must be SERVICE_IBLUETOOTH |
| 1192 | |
| 1193 | //Remove timeout |
Zhihai Xu | af5971e | 2013-06-10 20:28:31 -0700 | [diff] [blame] | 1194 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1195 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1196 | mBinding = false; |
| 1197 | mBluetooth = IBluetooth.Stub.asInterface(service); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1198 | |
Zhihai Xu | af5971e | 2013-06-10 20:28:31 -0700 | [diff] [blame] | 1199 | try { |
| 1200 | boolean enableHciSnoopLog = (Settings.Secure.getInt(mContentResolver, |
| 1201 | Settings.Secure.BLUETOOTH_HCI_LOG, 0) == 1); |
| 1202 | if (!mBluetooth.configHciSnoopLog(enableHciSnoopLog)) { |
| 1203 | Log.e(TAG,"IBluetooth.configHciSnoopLog return false"); |
| 1204 | } |
| 1205 | } catch (RemoteException e) { |
| 1206 | Log.e(TAG,"Unable to call configHciSnoopLog", e); |
| 1207 | } |
| 1208 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1209 | if (mConnection.isGetNameAddressOnly()) { |
| 1210 | //Request GET NAME AND ADDRESS |
| 1211 | Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS); |
| 1212 | mHandler.sendMessage(getMsg); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1213 | if (!mEnable) return; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1214 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1215 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1216 | mConnection.setGetNameAddressOnly(false); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1217 | //Register callback object |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1218 | try { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1219 | mBluetooth.registerCallback(mBluetoothCallback); |
| 1220 | } catch (RemoteException re) { |
| 1221 | Log.e(TAG, "Unable to register BluetoothCallback",re); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1222 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1223 | //Inform BluetoothAdapter instances that service is up |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1224 | sendBluetoothServiceUpCallback(); |
| 1225 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1226 | //Do enable request |
| 1227 | try { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1228 | if (mQuietEnable == false) { |
| 1229 | if(!mBluetooth.enable()) { |
| 1230 | Log.e(TAG,"IBluetooth.enable() returned false"); |
| 1231 | } |
| 1232 | } |
| 1233 | else |
| 1234 | { |
| 1235 | if(!mBluetooth.enableNoAutoConnect()) { |
| 1236 | Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false"); |
| 1237 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1238 | } |
| 1239 | } catch (RemoteException e) { |
| 1240 | Log.e(TAG,"Unable to call enable()",e); |
| 1241 | } |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 1242 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1243 | |
| 1244 | if (!mEnable) { |
| 1245 | waitForOnOff(true, false); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1246 | handleDisable(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1247 | waitForOnOff(false, false); |
| 1248 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1249 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1250 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1251 | case MESSAGE_TIMEOUT_BIND: { |
| 1252 | Log.e(TAG, "MESSAGE_TIMEOUT_BIND"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1253 | synchronized(mConnection) { |
| 1254 | mBinding = false; |
| 1255 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1256 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1257 | } |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1258 | case MESSAGE_BLUETOOTH_STATE_CHANGE: |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1259 | { |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1260 | int prevState = msg.arg1; |
| 1261 | int newState = msg.arg2; |
| 1262 | 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] | 1263 | mState = newState; |
| 1264 | bluetoothStateChangeHandler(prevState, newState); |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 1265 | // handle error state transition case from TURNING_ON to OFF |
| 1266 | // unbind and rebind bluetooth service and enable bluetooth |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1267 | if ((prevState == BluetoothAdapter.STATE_BLE_TURNING_ON) && |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 1268 | (newState == BluetoothAdapter.STATE_OFF) && |
| 1269 | (mBluetooth != null) && mEnable) { |
| 1270 | recoverBluetoothServiceFromError(); |
| 1271 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1272 | if ((prevState == BluetoothAdapter.STATE_TURNING_ON) && |
| 1273 | (newState == BluetoothAdapter.STATE_BLE_ON) && |
| 1274 | (mBluetooth != null) && mEnable) { |
| 1275 | recoverBluetoothServiceFromError(); |
| 1276 | } |
| 1277 | if (newState == BluetoothAdapter.STATE_ON || |
| 1278 | newState == BluetoothAdapter.STATE_BLE_ON) { |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 1279 | // bluetooth is working, reset the counter |
| 1280 | if (mErrorRecoveryRetryCounter != 0) { |
| 1281 | Log.w(TAG, "bluetooth is recovered from error"); |
| 1282 | mErrorRecoveryRetryCounter = 0; |
| 1283 | } |
| 1284 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1285 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1286 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1287 | case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: |
| 1288 | { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1289 | Log.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: " + msg.arg1); |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 1290 | synchronized(mConnection) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1291 | if (msg.arg1 == SERVICE_IBLUETOOTH) { |
| 1292 | // if service is unbinded already, do nothing and return |
| 1293 | if (mBluetooth == null) break; |
| 1294 | mBluetooth = null; |
| 1295 | } else if (msg.arg1 == SERVICE_IBLUETOOTHGATT) { |
| 1296 | mBluetoothGatt = null; |
| 1297 | break; |
| 1298 | } else { |
| 1299 | Log.e(TAG, "Bad msg.arg1: " + msg.arg1); |
| 1300 | break; |
| 1301 | } |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 1302 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1303 | |
| 1304 | if (mEnable) { |
| 1305 | mEnable = false; |
| 1306 | // Send a Bluetooth Restart message |
| 1307 | Message restartMsg = mHandler.obtainMessage( |
| 1308 | MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 1309 | mHandler.sendMessageDelayed(restartMsg, |
| 1310 | SERVICE_RESTART_TIME_MS); |
| 1311 | } |
| 1312 | |
| 1313 | if (!mConnection.isGetNameAddressOnly()) { |
| 1314 | sendBluetoothServiceDownCallback(); |
| 1315 | |
| 1316 | // Send BT state broadcast to update |
| 1317 | // the BT icon correctly |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 1318 | if ((mState == BluetoothAdapter.STATE_TURNING_ON) || |
| 1319 | (mState == BluetoothAdapter.STATE_ON)) { |
| 1320 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON, |
| 1321 | BluetoothAdapter.STATE_TURNING_OFF); |
| 1322 | mState = BluetoothAdapter.STATE_TURNING_OFF; |
| 1323 | } |
| 1324 | if (mState == BluetoothAdapter.STATE_TURNING_OFF) { |
| 1325 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF, |
| 1326 | BluetoothAdapter.STATE_OFF); |
| 1327 | } |
| 1328 | |
| 1329 | mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1330 | mState = BluetoothAdapter.STATE_OFF; |
| 1331 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1332 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1333 | } |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 1334 | case MESSAGE_RESTART_BLUETOOTH_SERVICE: |
| 1335 | { |
| 1336 | Log.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE:" |
| 1337 | +" Restart IBluetooth service"); |
| 1338 | /* Enable without persisting the setting as |
| 1339 | it doesnt change when IBluetooth |
| 1340 | service restarts */ |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1341 | mEnable = true; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1342 | handleEnable(mQuietEnable); |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 1343 | break; |
| 1344 | } |
| 1345 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1346 | case MESSAGE_TIMEOUT_UNBIND: |
| 1347 | { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1348 | Log.e(TAG, "MESSAGE_TIMEOUT_UNBIND"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1349 | synchronized(mConnection) { |
| 1350 | mUnbinding = false; |
| 1351 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1352 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1353 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1354 | |
| 1355 | case MESSAGE_USER_SWITCHED: |
| 1356 | { |
| 1357 | if (DBG) { |
| 1358 | Log.d(TAG, "MESSAGE_USER_SWITCHED"); |
| 1359 | } |
| 1360 | mHandler.removeMessages(MESSAGE_USER_SWITCHED); |
| 1361 | /* disable and enable BT when detect a user switch */ |
| 1362 | if (mEnable && mBluetooth != null) { |
| 1363 | synchronized (mConnection) { |
| 1364 | if (mBluetooth != null) { |
| 1365 | //Unregister callback object |
| 1366 | try { |
| 1367 | mBluetooth.unregisterCallback(mBluetoothCallback); |
| 1368 | } catch (RemoteException re) { |
| 1369 | Log.e(TAG, "Unable to unregister",re); |
| 1370 | } |
| 1371 | } |
| 1372 | } |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 1373 | |
| 1374 | if (mState == BluetoothAdapter.STATE_TURNING_OFF) { |
| 1375 | // MESSAGE_USER_SWITCHED happened right after MESSAGE_ENABLE |
| 1376 | bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_OFF); |
| 1377 | mState = BluetoothAdapter.STATE_OFF; |
| 1378 | } |
| 1379 | if (mState == BluetoothAdapter.STATE_OFF) { |
| 1380 | bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_TURNING_ON); |
| 1381 | mState = BluetoothAdapter.STATE_TURNING_ON; |
| 1382 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1383 | |
| 1384 | waitForOnOff(true, false); |
| 1385 | |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 1386 | if (mState == BluetoothAdapter.STATE_TURNING_ON) { |
| 1387 | bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_ON); |
| 1388 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1389 | |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1390 | unbindAllBluetoothProfileServices(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1391 | // disable |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1392 | handleDisable(); |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 1393 | // Pbap service need receive STATE_TURNING_OFF intent to close |
| 1394 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON, |
| 1395 | BluetoothAdapter.STATE_TURNING_OFF); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1396 | |
| 1397 | waitForOnOff(false, true); |
| 1398 | |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 1399 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF, |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1400 | BluetoothAdapter.STATE_OFF); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1401 | sendBluetoothServiceDownCallback(); |
| 1402 | synchronized (mConnection) { |
| 1403 | if (mBluetooth != null) { |
| 1404 | mBluetooth = null; |
| 1405 | //Unbind |
| 1406 | mContext.unbindService(mConnection); |
| 1407 | } |
| 1408 | } |
| 1409 | SystemClock.sleep(100); |
| 1410 | |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 1411 | mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE); |
| 1412 | mState = BluetoothAdapter.STATE_OFF; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1413 | // enable |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1414 | handleEnable(mQuietEnable); |
John Spurlock | 8a985d2 | 2014-02-25 09:40:05 -0500 | [diff] [blame] | 1415 | } else if (mBinding || mBluetooth != null) { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1416 | Message userMsg = mHandler.obtainMessage(MESSAGE_USER_SWITCHED); |
| 1417 | userMsg.arg2 = 1 + msg.arg2; |
| 1418 | // if user is switched when service is being binding |
| 1419 | // delay sending MESSAGE_USER_SWITCHED |
| 1420 | mHandler.sendMessageDelayed(userMsg, USER_SWITCHED_TIME_MS); |
| 1421 | if (DBG) { |
| 1422 | Log.d(TAG, "delay MESSAGE_USER_SWITCHED " + userMsg.arg2); |
| 1423 | } |
John Spurlock | 8a985d2 | 2014-02-25 09:40:05 -0500 | [diff] [blame] | 1424 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1425 | break; |
| 1426 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1427 | } |
| 1428 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1429 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1430 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1431 | private void handleEnable(boolean quietMode) { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1432 | mQuietEnable = quietMode; |
| 1433 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1434 | synchronized(mConnection) { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1435 | if ((mBluetooth == null) && (!mBinding)) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1436 | //Start bind timeout and bind |
| 1437 | Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND); |
| 1438 | mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS); |
| 1439 | mConnection.setGetNameAddressOnly(false); |
| 1440 | Intent i = new Intent(IBluetooth.class.getName()); |
Dianne Hackborn | ce09f5a | 2014-10-10 15:03:13 -0700 | [diff] [blame] | 1441 | if (!doBind(i, mConnection,Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, |
| 1442 | UserHandle.CURRENT)) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1443 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1444 | } else { |
| 1445 | mBinding = true; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1446 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1447 | } else if (mBluetooth != null) { |
| 1448 | if (mConnection.isGetNameAddressOnly()) { |
| 1449 | // if GetNameAddressOnly is set, we can clear this flag, |
| 1450 | // so the service won't be unbind |
| 1451 | // after name and address are saved |
| 1452 | mConnection.setGetNameAddressOnly(false); |
| 1453 | //Register callback object |
| 1454 | try { |
| 1455 | mBluetooth.registerCallback(mBluetoothCallback); |
| 1456 | } catch (RemoteException re) { |
| 1457 | Log.e(TAG, "Unable to register BluetoothCallback",re); |
| 1458 | } |
| 1459 | //Inform BluetoothAdapter instances that service is up |
| 1460 | sendBluetoothServiceUpCallback(); |
| 1461 | } |
| 1462 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1463 | //Enable bluetooth |
| 1464 | try { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1465 | if (!mQuietEnable) { |
| 1466 | if(!mBluetooth.enable()) { |
| 1467 | Log.e(TAG,"IBluetooth.enable() returned false"); |
| 1468 | } |
| 1469 | } |
| 1470 | else { |
| 1471 | if(!mBluetooth.enableNoAutoConnect()) { |
| 1472 | Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false"); |
| 1473 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1474 | } |
| 1475 | } catch (RemoteException e) { |
| 1476 | Log.e(TAG,"Unable to call enable()",e); |
| 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | } |
| 1481 | |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 1482 | boolean doBind(Intent intent, ServiceConnection conn, int flags, UserHandle user) { |
| 1483 | ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); |
| 1484 | intent.setComponent(comp); |
| 1485 | if (comp == null || !mContext.bindServiceAsUser(intent, conn, flags, user)) { |
| 1486 | Log.e(TAG, "Fail to bind to: " + intent); |
| 1487 | return false; |
| 1488 | } |
| 1489 | return true; |
| 1490 | } |
| 1491 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1492 | private void handleDisable() { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1493 | synchronized(mConnection) { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1494 | // don't need to disable if GetNameAddressOnly is set, |
| 1495 | // service will be unbinded after Name and Address are saved |
| 1496 | if ((mBluetooth != null) && (!mConnection.isGetNameAddressOnly())) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1497 | if (DBG) Log.d(TAG,"Sending off request."); |
| 1498 | |
| 1499 | try { |
| 1500 | if(!mBluetooth.disable()) { |
| 1501 | Log.e(TAG,"IBluetooth.disable() returned false"); |
| 1502 | } |
| 1503 | } catch (RemoteException e) { |
| 1504 | Log.e(TAG,"Unable to call disable()",e); |
| 1505 | } |
| 1506 | } |
| 1507 | } |
| 1508 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1509 | |
| 1510 | private boolean checkIfCallerIsForegroundUser() { |
| 1511 | int foregroundUser; |
| 1512 | int callingUser = UserHandle.getCallingUserId(); |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 1513 | int callingUid = Binder.getCallingUid(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1514 | long callingIdentity = Binder.clearCallingIdentity(); |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1515 | UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 1516 | UserInfo ui = um.getProfileParent(callingUser); |
| 1517 | int parentUser = (ui != null) ? ui.id : UserHandle.USER_NULL; |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 1518 | int callingAppId = UserHandle.getAppId(callingUid); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1519 | boolean valid = false; |
| 1520 | try { |
| 1521 | foregroundUser = ActivityManager.getCurrentUser(); |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 1522 | valid = (callingUser == foregroundUser) || |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1523 | parentUser == foregroundUser || |
Adrian Roos | bd9a9a5 | 2014-08-18 15:31:57 +0200 | [diff] [blame] | 1524 | callingAppId == Process.NFC_UID || |
| 1525 | callingAppId == mSystemUiUid; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1526 | if (DBG) { |
| 1527 | Log.d(TAG, "checkIfCallerIsForegroundUser: valid=" + valid |
| 1528 | + " callingUser=" + callingUser |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1529 | + " parentUser=" + parentUser |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1530 | + " foregroundUser=" + foregroundUser); |
| 1531 | } |
| 1532 | } finally { |
| 1533 | Binder.restoreCallingIdentity(callingIdentity); |
| 1534 | } |
| 1535 | return valid; |
| 1536 | } |
| 1537 | |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1538 | private void sendBleStateChanged(int prevState, int newState) { |
| 1539 | if (DBG) Log.d(TAG,"BLE State Change Intent: " + prevState + " -> " + newState); |
| 1540 | // Send broadcast message to everyone else |
| 1541 | Intent intent = new Intent(BluetoothAdapter.ACTION_BLE_STATE_CHANGED); |
| 1542 | intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState); |
| 1543 | intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState); |
| 1544 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 1545 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM); |
| 1546 | } |
| 1547 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1548 | private void bluetoothStateChangeHandler(int prevState, int newState) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1549 | boolean isStandardBroadcast = true; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1550 | if (prevState != newState) { |
| 1551 | //Notify all proxy objects first of adapter state change |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1552 | if (newState == BluetoothAdapter.STATE_BLE_ON |
| 1553 | || newState == BluetoothAdapter.STATE_OFF) { |
| 1554 | boolean intermediate_off = (prevState == BluetoothAdapter.STATE_TURNING_OFF |
| 1555 | && newState == BluetoothAdapter.STATE_BLE_ON); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1556 | |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1557 | if (newState == BluetoothAdapter.STATE_OFF) { |
| 1558 | // If Bluetooth is off, send service down event to proxy objects, and unbind |
| 1559 | if (DBG) Log.d(TAG, "Bluetooth is complete turn off"); |
| 1560 | if (canUnbindBluetoothService()) { |
| 1561 | if (DBG) Log.d(TAG, "Good to unbind!"); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1562 | sendBluetoothServiceDownCallback(); |
| 1563 | unbindAndFinish(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1564 | sendBleStateChanged(prevState, newState); |
| 1565 | // Don't broadcast as it has already been broadcast before |
| 1566 | isStandardBroadcast = false; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1567 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1568 | |
| 1569 | } else if (!intermediate_off) { |
| 1570 | // connect to GattService |
| 1571 | if (DBG) Log.d(TAG, "Bluetooth is in LE only mode"); |
| 1572 | if (mBluetoothGatt != null) { |
| 1573 | if (DBG) Log.d(TAG, "Calling BluetoothGattServiceUp"); |
| 1574 | onBluetoothGattServiceUp(); |
| 1575 | } else { |
| 1576 | if (DBG) Log.d(TAG, "Binding Bluetooth GATT service"); |
| 1577 | if (mContext.getPackageManager().hasSystemFeature( |
| 1578 | PackageManager.FEATURE_BLUETOOTH_LE)) { |
| 1579 | Intent i = new Intent(IBluetoothGatt.class.getName()); |
| 1580 | doBind(i, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, UserHandle.CURRENT); |
| 1581 | } |
| 1582 | } |
| 1583 | sendBleStateChanged(prevState, newState); |
| 1584 | //Don't broadcase this as std intent |
| 1585 | isStandardBroadcast = false; |
| 1586 | |
| 1587 | } else if (intermediate_off){ |
| 1588 | if (DBG) Log.d(TAG, "Intermediate off, back to LE only mode"); |
| 1589 | // For LE only mode, broadcast as is |
| 1590 | sendBleStateChanged(prevState, newState); |
| 1591 | sendBluetoothStateCallback(false); // BT is OFF for general users |
| 1592 | // Broadcast as STATE_OFF |
| 1593 | newState = BluetoothAdapter.STATE_OFF; |
| 1594 | sendBrEdrDownCallback(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1595 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1596 | } else if (newState == BluetoothAdapter.STATE_ON) { |
| 1597 | boolean isUp = (newState==BluetoothAdapter.STATE_ON); |
| 1598 | sendBluetoothStateCallback(isUp); |
| 1599 | sendBleStateChanged(prevState, newState); |
| 1600 | |
| 1601 | } else if (newState == BluetoothAdapter.STATE_BLE_TURNING_ON |
| 1602 | || newState == BluetoothAdapter.STATE_BLE_TURNING_OFF ) { |
| 1603 | sendBleStateChanged(prevState, newState); |
| 1604 | isStandardBroadcast = false; |
| 1605 | |
| 1606 | } else if (newState == BluetoothAdapter.STATE_TURNING_ON |
| 1607 | || newState == BluetoothAdapter.STATE_TURNING_OFF) { |
| 1608 | sendBleStateChanged(prevState, newState); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1611 | if (isStandardBroadcast) { |
| 1612 | if (prevState == BluetoothAdapter.STATE_BLE_ON) { |
| 1613 | // Show prevState of BLE_ON as OFF to standard users |
| 1614 | prevState = BluetoothAdapter.STATE_OFF; |
| 1615 | } |
| 1616 | Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED); |
| 1617 | intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState); |
| 1618 | intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState); |
| 1619 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 1620 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM); |
| 1621 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | /** |
| 1626 | * if on is true, wait for state become ON |
| 1627 | * if off is true, wait for state become OFF |
| 1628 | * if both on and off are false, wait for state not ON |
| 1629 | */ |
| 1630 | private boolean waitForOnOff(boolean on, boolean off) { |
| 1631 | int i = 0; |
| 1632 | while (i < 10) { |
| 1633 | synchronized(mConnection) { |
| 1634 | try { |
| 1635 | if (mBluetooth == null) break; |
| 1636 | if (on) { |
| 1637 | if (mBluetooth.getState() == BluetoothAdapter.STATE_ON) return true; |
| 1638 | } else if (off) { |
| 1639 | if (mBluetooth.getState() == BluetoothAdapter.STATE_OFF) return true; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 1640 | } else { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1641 | if (mBluetooth.getState() != BluetoothAdapter.STATE_ON) return true; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 1642 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1643 | } catch (RemoteException e) { |
| 1644 | Log.e(TAG, "getState()", e); |
| 1645 | break; |
| 1646 | } |
| 1647 | } |
| 1648 | if (on || off) { |
| 1649 | SystemClock.sleep(300); |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 1650 | } else { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1651 | SystemClock.sleep(50); |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 1652 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1653 | i++; |
| 1654 | } |
| 1655 | Log.e(TAG,"waitForOnOff time out"); |
| 1656 | return false; |
| 1657 | } |
Zhihai Xu | 681ae7f | 2012-11-12 15:14:18 -0800 | [diff] [blame] | 1658 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1659 | private void sendDisableMsg() { |
| 1660 | mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISABLE)); |
| 1661 | } |
| 1662 | |
| 1663 | private void sendEnableMsg(boolean quietMode) { |
| 1664 | mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_ENABLE, |
| 1665 | quietMode ? 1 : 0, 0)); |
| 1666 | } |
| 1667 | |
Zhihai Xu | 681ae7f | 2012-11-12 15:14:18 -0800 | [diff] [blame] | 1668 | private boolean canUnbindBluetoothService() { |
| 1669 | synchronized(mConnection) { |
| 1670 | //Only unbind with mEnable flag not set |
| 1671 | //For race condition: disable and enable back-to-back |
| 1672 | //Avoid unbind right after enable due to callback from disable |
| 1673 | //Only unbind with Bluetooth at OFF state |
| 1674 | //Only unbind without any MESSAGE_BLUETOOTH_STATE_CHANGE message |
| 1675 | try { |
| 1676 | if (mEnable || (mBluetooth == null)) return false; |
| 1677 | if (mHandler.hasMessages(MESSAGE_BLUETOOTH_STATE_CHANGE)) return false; |
| 1678 | return (mBluetooth.getState() == BluetoothAdapter.STATE_OFF); |
| 1679 | } catch (RemoteException e) { |
| 1680 | Log.e(TAG, "getState()", e); |
| 1681 | } |
| 1682 | } |
| 1683 | return false; |
| 1684 | } |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 1685 | |
| 1686 | private void recoverBluetoothServiceFromError() { |
| 1687 | Log.e(TAG,"recoverBluetoothServiceFromError"); |
| 1688 | synchronized (mConnection) { |
| 1689 | if (mBluetooth != null) { |
| 1690 | //Unregister callback object |
| 1691 | try { |
| 1692 | mBluetooth.unregisterCallback(mBluetoothCallback); |
| 1693 | } catch (RemoteException re) { |
| 1694 | Log.e(TAG, "Unable to unregister",re); |
| 1695 | } |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | SystemClock.sleep(500); |
| 1700 | |
| 1701 | // disable |
| 1702 | handleDisable(); |
| 1703 | |
| 1704 | waitForOnOff(false, true); |
| 1705 | |
| 1706 | sendBluetoothServiceDownCallback(); |
| 1707 | synchronized (mConnection) { |
| 1708 | if (mBluetooth != null) { |
| 1709 | mBluetooth = null; |
| 1710 | //Unbind |
| 1711 | mContext.unbindService(mConnection); |
| 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE); |
| 1716 | mState = BluetoothAdapter.STATE_OFF; |
| 1717 | |
| 1718 | mEnable = false; |
| 1719 | |
| 1720 | if (mErrorRecoveryRetryCounter++ < MAX_ERROR_RESTART_RETRIES) { |
| 1721 | // Send a Bluetooth Restart message to reenable bluetooth |
| 1722 | Message restartMsg = mHandler.obtainMessage( |
| 1723 | MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 1724 | mHandler.sendMessageDelayed(restartMsg, ERROR_RESTART_TIME_MS); |
| 1725 | } else { |
| 1726 | // todo: notify user to power down and power up phone to make bluetooth work. |
| 1727 | } |
| 1728 | } |
Mike Lockwood | 726d4de | 2014-10-28 14:06:28 -0700 | [diff] [blame] | 1729 | |
| 1730 | @Override |
| 1731 | public void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Mike Lockwood | 75b52bb | 2014-12-18 14:16:36 -0800 | [diff] [blame] | 1732 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
| 1733 | |
Mike Lockwood | 726d4de | 2014-10-28 14:06:28 -0700 | [diff] [blame] | 1734 | writer.println("enabled: " + mEnable); |
| 1735 | writer.println("state: " + mState); |
| 1736 | writer.println("address: " + mAddress); |
| 1737 | writer.println("name: " + mName); |
| 1738 | if (mBluetooth == null) { |
| 1739 | writer.println("Bluetooth Service not connected"); |
| 1740 | } else { |
| 1741 | try { |
| 1742 | writer.println(mBluetooth.dump()); |
| 1743 | } catch (RemoteException re) { |
| 1744 | writer.println("RemoteException while calling Bluetooth Service"); |
| 1745 | } |
| 1746 | } |
| 1747 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1748 | } |