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