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