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