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