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