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