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