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