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