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; |
| 21 | import android.bluetooth.IBluetooth; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 22 | import android.bluetooth.IBluetoothGatt; |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 23 | import android.bluetooth.IBluetoothCallback; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 24 | import android.bluetooth.IBluetoothManager; |
| 25 | import android.bluetooth.IBluetoothManagerCallback; |
| 26 | import android.bluetooth.IBluetoothStateChangeCallback; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 27 | import android.content.BroadcastReceiver; |
| 28 | import android.content.ComponentName; |
| 29 | import android.content.ContentResolver; |
| 30 | import android.content.Context; |
| 31 | import android.content.Intent; |
| 32 | import android.content.IntentFilter; |
| 33 | import android.content.ServiceConnection; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 34 | import android.os.Binder; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 35 | import android.os.Handler; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 36 | import android.os.HandlerThread; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 37 | import android.os.IBinder; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 38 | import android.os.Looper; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 39 | import android.os.Message; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 40 | import android.os.Process; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 41 | import android.os.RemoteCallbackList; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 42 | import android.os.RemoteException; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 43 | import android.os.SystemClock; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 44 | import android.os.UserHandle; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 45 | import android.provider.Settings; |
| 46 | import android.util.Log; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 47 | class BluetoothManagerService extends IBluetoothManager.Stub { |
| 48 | private static final String TAG = "BluetoothManagerService"; |
| 49 | private static final boolean DBG = true; |
| 50 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 51 | private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN; |
| 52 | private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 53 | private static final String ACTION_SERVICE_STATE_CHANGED="com.android.bluetooth.btservice.action.STATE_CHANGED"; |
| 54 | private static final String EXTRA_ACTION="action"; |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 55 | private static final String SECURE_SETTINGS_BLUETOOTH_ADDR_VALID="bluetooth_addr_valid"; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 56 | private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address"; |
| 57 | private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name"; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 58 | private static final int TIMEOUT_BIND_MS = 3000; //Maximum msec to wait for a bind |
| 59 | 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] | 60 | //Maximum msec to wait for service restart |
| 61 | private static final int SERVICE_RESTART_TIME_MS = 200; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 62 | //Maximum msec to delay MESSAGE_USER_SWITCHED |
| 63 | private static final int USER_SWITCHED_TIME_MS = 200; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 64 | |
| 65 | private static final int MESSAGE_ENABLE = 1; |
| 66 | private static final int MESSAGE_DISABLE = 2; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 67 | private static final int MESSAGE_REGISTER_ADAPTER = 20; |
| 68 | private static final int MESSAGE_UNREGISTER_ADAPTER = 21; |
| 69 | private static final int MESSAGE_REGISTER_STATE_CHANGE_CALLBACK = 30; |
| 70 | private static final int MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK = 31; |
| 71 | private static final int MESSAGE_BLUETOOTH_SERVICE_CONNECTED = 40; |
| 72 | private static final int MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED = 41; |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 73 | private static final int MESSAGE_RESTART_BLUETOOTH_SERVICE = 42; |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 74 | private static final int MESSAGE_BLUETOOTH_STATE_CHANGE=60; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 75 | private static final int MESSAGE_TIMEOUT_BIND =100; |
| 76 | private static final int MESSAGE_TIMEOUT_UNBIND =101; |
| 77 | private static final int MESSAGE_GET_NAME_AND_ADDRESS=200; |
| 78 | private static final int MESSAGE_SAVE_NAME_AND_ADDRESS=201; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 79 | private static final int MESSAGE_USER_SWITCHED = 300; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 80 | private static final int MAX_SAVE_RETRIES=3; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 81 | // Bluetooth persisted setting is off |
| 82 | private static final int BLUETOOTH_OFF=0; |
| 83 | // Bluetooth persisted setting is on |
| 84 | // and Airplane mode won't affect Bluetooth state at start up |
| 85 | private static final int BLUETOOTH_ON_BLUETOOTH=1; |
| 86 | // Bluetooth persisted setting is on |
| 87 | // but Airplane mode will affect Bluetooth state at start up |
| 88 | // and Airplane mode will have higher priority. |
| 89 | private static final int BLUETOOTH_ON_AIRPLANE=2; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 90 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 91 | private static final int SERVICE_IBLUETOOTH = 1; |
| 92 | private static final int SERVICE_IBLUETOOTHGATT = 2; |
| 93 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 94 | private final Context mContext; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 95 | |
| 96 | // Locks are not provided for mName and mAddress. |
| 97 | // They are accessed in handler or broadcast receiver, same thread context. |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 98 | private String mAddress; |
| 99 | private String mName; |
Matthew Xie | 6fde309 | 2012-07-11 17:10:07 -0700 | [diff] [blame] | 100 | private final ContentResolver mContentResolver; |
| 101 | private final RemoteCallbackList<IBluetoothManagerCallback> mCallbacks; |
| 102 | private final RemoteCallbackList<IBluetoothStateChangeCallback> mStateChangeCallbacks; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 103 | private IBluetooth mBluetooth; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 104 | private IBluetoothGatt mBluetoothGatt; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 105 | private boolean mBinding; |
| 106 | private boolean mUnbinding; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 107 | // used inside handler thread |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 108 | private boolean mQuietEnable = false; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 109 | // configuarion from external IBinder call which is used to |
| 110 | // synchronize with broadcast receiver. |
| 111 | private boolean mQuietEnableExternal; |
| 112 | // configuarion from external IBinder call which is used to |
| 113 | // synchronize with broadcast receiver. |
| 114 | private boolean mEnableExternal; |
| 115 | // used inside handler thread |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 116 | private boolean mEnable; |
| 117 | private int mState; |
| 118 | private HandlerThread mThread; |
| 119 | private final BluetoothHandler mHandler; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 120 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 121 | private void registerForAirplaneMode(IntentFilter filter) { |
| 122 | final ContentResolver resolver = mContext.getContentResolver(); |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 123 | final String airplaneModeRadios = Settings.Global.getString(resolver, |
| 124 | Settings.Global.AIRPLANE_MODE_RADIOS); |
| 125 | final String toggleableRadios = Settings.Global.getString(resolver, |
| 126 | Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 127 | boolean mIsAirplaneSensitive = airplaneModeRadios == null ? true : |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 128 | airplaneModeRadios.contains(Settings.Global.RADIO_BLUETOOTH); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 129 | if (mIsAirplaneSensitive) { |
| 130 | filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 131 | } |
| 132 | } |
| 133 | |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 134 | private final IBluetoothCallback mBluetoothCallback = new IBluetoothCallback.Stub() { |
| 135 | @Override |
| 136 | public void onBluetoothStateChange(int prevState, int newState) throws RemoteException { |
| 137 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_STATE_CHANGE,prevState,newState); |
| 138 | mHandler.sendMessage(msg); |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 143 | @Override |
| 144 | public void onReceive(Context context, Intent intent) { |
| 145 | String action = intent.getAction(); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 146 | if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action)) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 147 | String newName = intent.getStringExtra(BluetoothAdapter.EXTRA_LOCAL_NAME); |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 148 | if (DBG) Log.d(TAG, "Bluetooth Adapter name changed to " + newName); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 149 | if (newName != null) { |
| 150 | storeNameAndAddress(newName, null); |
| 151 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 152 | } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) { |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 153 | synchronized(mReceiver) { |
| 154 | if (isBluetoothPersistedStateOn()) { |
| 155 | if (isAirplaneModeOn()) { |
| 156 | persistBluetoothSetting(BLUETOOTH_ON_AIRPLANE); |
| 157 | } else { |
| 158 | persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH); |
| 159 | } |
| 160 | } |
| 161 | if (isAirplaneModeOn()) { |
| 162 | // disable without persisting the setting |
| 163 | sendDisableMsg(); |
| 164 | } else if (mEnableExternal) { |
| 165 | // enable without persisting the setting |
| 166 | sendEnableMsg(mQuietEnableExternal); |
| 167 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 168 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 169 | } else if (Intent.ACTION_USER_SWITCHED.equals(action)) { |
| 170 | mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_USER_SWITCHED, |
| 171 | intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0), 0)); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 172 | } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { |
| 173 | synchronized(mReceiver) { |
| 174 | if (mEnableExternal && isBluetoothPersistedStateOnBluetooth()) { |
| 175 | //Enable |
| 176 | if (DBG) Log.d(TAG, "Auto-enabling Bluetooth."); |
| 177 | sendEnableMsg(mQuietEnableExternal); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (!isNameAndAddressSet()) { |
| 182 | //Sync the Bluetooth name and address from the Bluetooth Adapter |
| 183 | if (DBG) Log.d(TAG,"Retrieving Bluetooth Adapter name and address..."); |
| 184 | getNameAndAddress(); |
| 185 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | }; |
| 189 | |
| 190 | BluetoothManagerService(Context context) { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 191 | mThread = new HandlerThread("BluetoothManager"); |
| 192 | mThread.start(); |
| 193 | mHandler = new BluetoothHandler(mThread.getLooper()); |
| 194 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 195 | mContext = context; |
| 196 | mBluetooth = null; |
| 197 | mBinding = false; |
| 198 | mUnbinding = false; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 199 | mEnable = false; |
| 200 | mState = BluetoothAdapter.STATE_OFF; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 201 | mQuietEnableExternal = false; |
| 202 | mEnableExternal = false; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 203 | mAddress = null; |
| 204 | mName = null; |
| 205 | mContentResolver = context.getContentResolver(); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 206 | mCallbacks = new RemoteCallbackList<IBluetoothManagerCallback>(); |
| 207 | mStateChangeCallbacks = new RemoteCallbackList<IBluetoothStateChangeCallback>(); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 208 | IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED); |
Matthew Xie | 6fde309 | 2012-07-11 17:10:07 -0700 | [diff] [blame] | 209 | filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 210 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Matthew Xie | 6fde309 | 2012-07-11 17:10:07 -0700 | [diff] [blame] | 211 | registerForAirplaneMode(filter); |
| 212 | mContext.registerReceiver(mReceiver, filter); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 213 | loadStoredNameAndAddress(); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 214 | if (isBluetoothPersistedStateOn()) { |
| 215 | mEnableExternal = true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 219 | /** |
| 220 | * Returns true if airplane mode is currently on |
| 221 | */ |
| 222 | private final boolean isAirplaneModeOn() { |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 223 | return Settings.Global.getInt(mContext.getContentResolver(), |
| 224 | Settings.Global.AIRPLANE_MODE_ON, 0) == 1; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Returns true if the Bluetooth saved state is "on" |
| 229 | */ |
| 230 | private final boolean isBluetoothPersistedStateOn() { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 231 | return Settings.Global.getInt(mContentResolver, |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 232 | Settings.Global.BLUETOOTH_ON, 0) != BLUETOOTH_OFF; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Returns true if the Bluetooth saved state is BLUETOOTH_ON_BLUETOOTH |
| 237 | */ |
| 238 | private final boolean isBluetoothPersistedStateOnBluetooth() { |
| 239 | return Settings.Global.getInt(mContentResolver, |
| 240 | Settings.Global.BLUETOOTH_ON, 0) == BLUETOOTH_ON_BLUETOOTH; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Save the Bluetooth on/off state |
| 245 | * |
| 246 | */ |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 247 | private void persistBluetoothSetting(int value) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 248 | Settings.Global.putInt(mContext.getContentResolver(), |
| 249 | Settings.Global.BLUETOOTH_ON, |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 250 | value); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Returns true if the Bluetooth Adapter's name and address is |
| 255 | * locally cached |
| 256 | * @return |
| 257 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 258 | private boolean isNameAndAddressSet() { |
| 259 | return mName !=null && mAddress!= null && mName.length()>0 && mAddress.length()>0; |
| 260 | } |
| 261 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 262 | /** |
| 263 | * Retrieve the Bluetooth Adapter's name and address and save it in |
| 264 | * in the local cache |
| 265 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 266 | private void loadStoredNameAndAddress() { |
| 267 | if (DBG) Log.d(TAG, "Loading stored name and address"); |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 268 | if (mContext.getResources().getBoolean |
| 269 | (com.android.internal.R.bool.config_bluetooth_address_validation) && |
| 270 | Settings.Secure.getInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 0) == 0) { |
| 271 | // if the valid flag is not set, don't load the address and name |
| 272 | if (DBG) Log.d(TAG, "invalid bluetooth name and address stored"); |
| 273 | return; |
| 274 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 275 | mName = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME); |
| 276 | mAddress = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS); |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 277 | if (DBG) Log.d(TAG, "Stored bluetooth Name=" + mName + ",Address=" + mAddress); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 278 | } |
| 279 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 280 | /** |
| 281 | * Save the Bluetooth name and address in the persistent store. |
| 282 | * Only non-null values will be saved. |
| 283 | * @param name |
| 284 | * @param address |
| 285 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 286 | private void storeNameAndAddress(String name, String address) { |
| 287 | if (name != null) { |
| 288 | Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME, name); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 289 | mName = name; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 290 | if (DBG) Log.d(TAG,"Stored Bluetooth name: " + |
| 291 | Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_NAME)); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | if (address != null) { |
| 295 | Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS, address); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 296 | mAddress=address; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 297 | if (DBG) Log.d(TAG,"Stored Bluetoothaddress: " + |
| 298 | Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_ADDRESS)); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 299 | } |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 300 | |
| 301 | if ((name != null) && (address != null)) { |
| 302 | Settings.Secure.putInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 1); |
| 303 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | public IBluetooth registerAdapter(IBluetoothManagerCallback callback){ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 307 | Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_ADAPTER); |
| 308 | msg.obj = callback; |
| 309 | mHandler.sendMessage(msg); |
| 310 | synchronized(mConnection) { |
| 311 | return mBluetooth; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | public void unregisterAdapter(IBluetoothManagerCallback callback) { |
| 316 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 317 | "Need BLUETOOTH permission"); |
| 318 | Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_ADAPTER); |
| 319 | msg.obj = callback; |
| 320 | mHandler.sendMessage(msg); |
| 321 | } |
| 322 | |
| 323 | public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) { |
| 324 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 325 | "Need BLUETOOTH permission"); |
| 326 | Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_STATE_CHANGE_CALLBACK); |
| 327 | msg.obj = callback; |
| 328 | mHandler.sendMessage(msg); |
| 329 | } |
| 330 | |
| 331 | public void unregisterStateChangeCallback(IBluetoothStateChangeCallback callback) { |
| 332 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 333 | "Need BLUETOOTH permission"); |
| 334 | Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK); |
| 335 | msg.obj = callback; |
| 336 | mHandler.sendMessage(msg); |
| 337 | } |
| 338 | |
| 339 | public boolean isEnabled() { |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 340 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 341 | (!checkIfCallerIsForegroundUser())) { |
| 342 | 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] | 343 | return false; |
| 344 | } |
| 345 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 346 | synchronized(mConnection) { |
| 347 | try { |
| 348 | return (mBluetooth != null && mBluetooth.isEnabled()); |
| 349 | } catch (RemoteException e) { |
| 350 | Log.e(TAG, "isEnabled()", e); |
| 351 | } |
| 352 | } |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | public void getNameAndAddress() { |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 357 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 358 | Log.d(TAG,"getNameAndAddress(): mBluetooth = " + mBluetooth + |
| 359 | " mBinding = " + mBinding); |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 360 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 361 | Message msg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS); |
| 362 | mHandler.sendMessage(msg); |
| 363 | } |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 364 | public boolean enableNoAutoConnect() |
| 365 | { |
| 366 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 367 | "Need BLUETOOTH ADMIN permission"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 368 | |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 369 | if (DBG) { |
| 370 | Log.d(TAG,"enableNoAutoConnect(): mBluetooth =" + mBluetooth + |
| 371 | " mBinding = " + mBinding); |
| 372 | } |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 373 | int callingAppId = UserHandle.getAppId(Binder.getCallingUid()); |
| 374 | |
| 375 | if (callingAppId != Process.NFC_UID) { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 376 | throw new SecurityException("no permission to enable Bluetooth quietly"); |
| 377 | } |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 378 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 379 | synchronized(mReceiver) { |
| 380 | mQuietEnableExternal = true; |
| 381 | mEnableExternal = true; |
| 382 | sendEnableMsg(true); |
| 383 | } |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 384 | return true; |
| 385 | |
| 386 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 387 | public boolean enable() { |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 388 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 389 | (!checkIfCallerIsForegroundUser())) { |
| 390 | 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] | 391 | return false; |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 394 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 395 | "Need BLUETOOTH ADMIN permission"); |
| 396 | if (DBG) { |
| 397 | Log.d(TAG,"enable(): mBluetooth =" + mBluetooth + |
| 398 | " mBinding = " + mBinding); |
| 399 | } |
| 400 | |
| 401 | synchronized(mReceiver) { |
| 402 | mQuietEnableExternal = false; |
| 403 | mEnableExternal = true; |
| 404 | // waive WRITE_SECURE_SETTINGS permission check |
| 405 | long callingIdentity = Binder.clearCallingIdentity(); |
| 406 | persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH); |
| 407 | Binder.restoreCallingIdentity(callingIdentity); |
| 408 | sendEnableMsg(false); |
| 409 | } |
| 410 | return true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | public boolean disable(boolean persist) { |
| 414 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 415 | "Need BLUETOOTH ADMIN permissicacheNameAndAddresson"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 416 | |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 417 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 418 | (!checkIfCallerIsForegroundUser())) { |
| 419 | 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] | 420 | return false; |
| 421 | } |
| 422 | |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 423 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 424 | Log.d(TAG,"disable(): mBluetooth = " + mBluetooth + |
| 425 | " mBinding = " + mBinding); |
| 426 | } |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 427 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 428 | synchronized(mReceiver) { |
| 429 | if (persist) { |
| 430 | // waive WRITE_SECURE_SETTINGS permission check |
| 431 | long callingIdentity = Binder.clearCallingIdentity(); |
| 432 | persistBluetoothSetting(BLUETOOTH_OFF); |
| 433 | Binder.restoreCallingIdentity(callingIdentity); |
| 434 | } |
| 435 | mEnableExternal = false; |
| 436 | sendDisableMsg(); |
| 437 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 438 | return true; |
| 439 | } |
| 440 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 441 | public void unbindAndFinish() { |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 442 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 443 | Log.d(TAG,"unbindAndFinish(): " + mBluetooth + |
| 444 | " mBinding = " + mBinding); |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 445 | } |
| 446 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 447 | synchronized (mConnection) { |
| 448 | if (mUnbinding) return; |
| 449 | mUnbinding = true; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 450 | if (mBluetooth != null) { |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 451 | if (!mConnection.isGetNameAddressOnly()) { |
| 452 | //Unregister callback object |
| 453 | try { |
| 454 | mBluetooth.unregisterCallback(mBluetoothCallback); |
| 455 | } catch (RemoteException re) { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 456 | Log.e(TAG, "Unable to unregister BluetoothCallback",re); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 457 | } |
| 458 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 459 | if (DBG) Log.d(TAG, "Sending unbind request."); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 460 | mBluetooth = null; |
| 461 | //Unbind |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 462 | mContext.unbindService(mConnection); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 463 | mUnbinding = false; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 464 | mBinding = false; |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 465 | } else { |
| 466 | mUnbinding=false; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 471 | public IBluetoothGatt getBluetoothGatt() { |
| 472 | // sync protection |
| 473 | return mBluetoothGatt; |
| 474 | } |
| 475 | |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 476 | private void sendBluetoothStateCallback(boolean isUp) { |
| 477 | int n = mStateChangeCallbacks.beginBroadcast(); |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 478 | if (DBG) Log.d(TAG,"Broadcasting onBluetoothStateChange("+isUp+") to " + n + " receivers."); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 479 | for (int i=0; i <n;i++) { |
| 480 | try { |
| 481 | mStateChangeCallbacks.getBroadcastItem(i).onBluetoothStateChange(isUp); |
| 482 | } catch (RemoteException e) { |
| 483 | Log.e(TAG, "Unable to call onBluetoothStateChange() on callback #" + i , e); |
| 484 | } |
| 485 | } |
| 486 | mStateChangeCallbacks.finishBroadcast(); |
| 487 | } |
| 488 | |
| 489 | /** |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 490 | * Inform BluetoothAdapter instances that Adapter service is up |
| 491 | */ |
| 492 | private void sendBluetoothServiceUpCallback() { |
| 493 | if (!mConnection.isGetNameAddressOnly()) { |
| 494 | if (DBG) Log.d(TAG,"Calling onBluetoothServiceUp callbacks"); |
| 495 | int n = mCallbacks.beginBroadcast(); |
| 496 | Log.d(TAG,"Broadcasting onBluetoothServiceUp() to " + n + " receivers."); |
| 497 | for (int i=0; i <n;i++) { |
| 498 | try { |
| 499 | mCallbacks.getBroadcastItem(i).onBluetoothServiceUp(mBluetooth); |
| 500 | } catch (RemoteException e) { |
| 501 | Log.e(TAG, "Unable to call onBluetoothServiceUp() on callback #" + i, e); |
| 502 | } |
| 503 | } |
| 504 | mCallbacks.finishBroadcast(); |
| 505 | } |
| 506 | } |
| 507 | /** |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 508 | * Inform BluetoothAdapter instances that Adapter service is down |
| 509 | */ |
| 510 | private void sendBluetoothServiceDownCallback() { |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 511 | if (!mConnection.isGetNameAddressOnly()) { |
| 512 | if (DBG) Log.d(TAG,"Calling onBluetoothServiceDown callbacks"); |
| 513 | int n = mCallbacks.beginBroadcast(); |
| 514 | Log.d(TAG,"Broadcasting onBluetoothServiceDown() to " + n + " receivers."); |
| 515 | for (int i=0; i <n;i++) { |
| 516 | try { |
| 517 | mCallbacks.getBroadcastItem(i).onBluetoothServiceDown(); |
| 518 | } catch (RemoteException e) { |
| 519 | Log.e(TAG, "Unable to call onBluetoothServiceDown() on callback #" + i, e); |
| 520 | } |
| 521 | } |
| 522 | mCallbacks.finishBroadcast(); |
| 523 | } |
| 524 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 525 | public String getAddress() { |
Matthew Xie | af5ddbf | 2012-12-04 10:47:43 -0800 | [diff] [blame] | 526 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 527 | "Need BLUETOOTH permission"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 528 | |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 529 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 530 | (!checkIfCallerIsForegroundUser())) { |
| 531 | Log.w(TAG,"getAddress(): not allowed for non-active and non system user"); |
| 532 | return null; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 533 | } |
| 534 | |
fredc | 116d1d46 | 2012-04-20 14:47:08 -0700 | [diff] [blame] | 535 | synchronized(mConnection) { |
| 536 | if (mBluetooth != null) { |
| 537 | try { |
| 538 | return mBluetooth.getAddress(); |
| 539 | } catch (RemoteException e) { |
| 540 | Log.e(TAG, "getAddress(): Unable to retrieve address remotely..Returning cached address",e); |
| 541 | } |
| 542 | } |
| 543 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 544 | // mAddress is accessed from outside. |
| 545 | // It is alright without a lock. Here, bluetooth is off, no other thread is |
| 546 | // changing mAddress |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 547 | return mAddress; |
| 548 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 549 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 550 | public String getName() { |
Matthew Xie | af5ddbf | 2012-12-04 10:47:43 -0800 | [diff] [blame] | 551 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 552 | "Need BLUETOOTH permission"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 553 | |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 554 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 555 | (!checkIfCallerIsForegroundUser())) { |
| 556 | Log.w(TAG,"getName(): not allowed for non-active and non system user"); |
| 557 | return null; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 558 | } |
| 559 | |
fredc | 116d1d46 | 2012-04-20 14:47:08 -0700 | [diff] [blame] | 560 | synchronized(mConnection) { |
| 561 | if (mBluetooth != null) { |
| 562 | try { |
| 563 | return mBluetooth.getName(); |
| 564 | } catch (RemoteException e) { |
| 565 | Log.e(TAG, "getName(): Unable to retrieve name remotely..Returning cached name",e); |
| 566 | } |
| 567 | } |
| 568 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 569 | // mName is accessed from outside. |
| 570 | // It alright without a lock. Here, bluetooth is off, no other thread is |
| 571 | // changing mName |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 572 | return mName; |
| 573 | } |
| 574 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 575 | private class BluetoothServiceConnection implements ServiceConnection { |
| 576 | |
| 577 | private boolean mGetNameAddressOnly; |
| 578 | |
| 579 | public void setGetNameAddressOnly(boolean getOnly) { |
| 580 | mGetNameAddressOnly = getOnly; |
| 581 | } |
| 582 | |
| 583 | public boolean isGetNameAddressOnly() { |
| 584 | return mGetNameAddressOnly; |
| 585 | } |
| 586 | |
| 587 | public void onServiceConnected(ComponentName className, IBinder service) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 588 | if (DBG) Log.d(TAG, "BluetoothServiceConnection: " + className.getClassName()); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 589 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 590 | // TBD if (className.getClassName().equals(IBluetooth.class.getName())) { |
| 591 | if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) { |
| 592 | msg.arg1 = SERVICE_IBLUETOOTH; |
| 593 | // } else if (className.getClassName().equals(IBluetoothGatt.class.getName())) { |
| 594 | } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) { |
| 595 | msg.arg1 = SERVICE_IBLUETOOTHGATT; |
| 596 | } else { |
| 597 | Log.e(TAG, "Unknown service connected: " + className.getClassName()); |
| 598 | return; |
| 599 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 600 | msg.obj = service; |
| 601 | mHandler.sendMessage(msg); |
| 602 | } |
| 603 | |
| 604 | public void onServiceDisconnected(ComponentName className) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 605 | // Called if we unexpected disconnected. |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 606 | if (DBG) Log.d(TAG, "BluetoothServiceConnection, disconnected: " + |
| 607 | className.getClassName()); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 608 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 609 | if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) { |
| 610 | msg.arg1 = SERVICE_IBLUETOOTH; |
| 611 | } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) { |
| 612 | msg.arg1 = SERVICE_IBLUETOOTHGATT; |
| 613 | } else { |
| 614 | Log.e(TAG, "Unknown service disconnected: " + className.getClassName()); |
| 615 | return; |
| 616 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 617 | mHandler.sendMessage(msg); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | private BluetoothServiceConnection mConnection = new BluetoothServiceConnection(); |
| 622 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 623 | private class BluetoothHandler extends Handler { |
| 624 | public BluetoothHandler(Looper looper) { |
| 625 | super(looper); |
| 626 | } |
| 627 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 628 | @Override |
| 629 | public void handleMessage(Message msg) { |
| 630 | if (DBG) Log.d (TAG, "Message: " + msg.what); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 631 | switch (msg.what) { |
| 632 | case MESSAGE_GET_NAME_AND_ADDRESS: { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 633 | if (DBG) Log.d(TAG,"MESSAGE_GET_NAME_AND_ADDRESS"); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 634 | synchronized(mConnection) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 635 | //Start bind request |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 636 | if ((mBluetooth == null) && (!mBinding)) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 637 | if (DBG) Log.d(TAG, "Binding to service to get name and address"); |
| 638 | mConnection.setGetNameAddressOnly(true); |
| 639 | //Start bind timeout and bind |
| 640 | Message timeoutMsg = mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND); |
| 641 | mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS); |
| 642 | Intent i = new Intent(IBluetooth.class.getName()); |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 643 | if (!mContext.bindServiceAsUser(i, mConnection, |
| 644 | Context.BIND_AUTO_CREATE, UserHandle.CURRENT)) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 645 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
| 646 | Log.e(TAG, "fail to bind to: " + IBluetooth.class.getName()); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 647 | } else { |
| 648 | mBinding = true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 649 | } |
| 650 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 651 | else { |
| 652 | Message saveMsg= mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 653 | saveMsg.arg1 = 0; |
| 654 | if (mBluetooth != null) { |
| 655 | mHandler.sendMessage(saveMsg); |
| 656 | } else { |
| 657 | // if enable is also called to bind the service |
| 658 | // wait for MESSAGE_BLUETOOTH_SERVICE_CONNECTED |
| 659 | mHandler.sendMessageDelayed(saveMsg, TIMEOUT_SAVE_MS); |
| 660 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 661 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 662 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 663 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 664 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 665 | case MESSAGE_SAVE_NAME_AND_ADDRESS: { |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 666 | boolean unbind = false; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 667 | if (DBG) Log.d(TAG,"MESSAGE_SAVE_NAME_AND_ADDRESS"); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 668 | synchronized(mConnection) { |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 669 | if (!mEnable && mBluetooth != null) { |
| 670 | try { |
| 671 | mBluetooth.enable(); |
| 672 | } catch (RemoteException e) { |
| 673 | Log.e(TAG,"Unable to call enable()",e); |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | if (mBluetooth != null) waitForOnOff(true, false); |
| 678 | synchronized(mConnection) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 679 | if (mBluetooth != null) { |
| 680 | String name = null; |
| 681 | String address = null; |
| 682 | try { |
| 683 | name = mBluetooth.getName(); |
| 684 | address = mBluetooth.getAddress(); |
| 685 | } catch (RemoteException re) { |
| 686 | Log.e(TAG,"",re); |
| 687 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 688 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 689 | if (name != null && address != null) { |
| 690 | storeNameAndAddress(name,address); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 691 | if (mConnection.isGetNameAddressOnly()) { |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 692 | unbind = true; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 693 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 694 | } else { |
| 695 | if (msg.arg1 < MAX_SAVE_RETRIES) { |
| 696 | Message retryMsg = mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS); |
| 697 | retryMsg.arg1= 1+msg.arg1; |
| 698 | if (DBG) Log.d(TAG,"Retrying name/address remote retrieval and save.....Retry count =" + retryMsg.arg1); |
| 699 | mHandler.sendMessageDelayed(retryMsg, TIMEOUT_SAVE_MS); |
| 700 | } else { |
| 701 | Log.w(TAG,"Maximum name/address remote retrieval retry exceeded"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 702 | if (mConnection.isGetNameAddressOnly()) { |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 703 | unbind = true; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 704 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 705 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 706 | } |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 707 | if (!mEnable) { |
| 708 | try { |
| 709 | mBluetooth.disable(); |
| 710 | } catch (RemoteException e) { |
| 711 | Log.e(TAG,"Unable to call disable()",e); |
| 712 | } |
| 713 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 714 | } else { |
| 715 | // rebind service by Request GET NAME AND ADDRESS |
| 716 | // if service is unbinded by disable or |
| 717 | // MESSAGE_BLUETOOTH_SERVICE_CONNECTED is not received |
| 718 | Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS); |
| 719 | mHandler.sendMessage(getMsg); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 720 | } |
| 721 | } |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 722 | if (!mEnable && mBluetooth != null) waitForOnOff(false, true); |
| 723 | if (unbind) { |
| 724 | unbindAndFinish(); |
| 725 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 726 | break; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 727 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 728 | case MESSAGE_ENABLE: |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 729 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 730 | Log.d(TAG, "MESSAGE_ENABLE: mBluetooth = " + mBluetooth); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 731 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 732 | mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 733 | mEnable = true; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 734 | handleEnable(msg.arg1 == 1); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 735 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 736 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 737 | case MESSAGE_DISABLE: |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 738 | mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 739 | if (mEnable && mBluetooth != null) { |
| 740 | waitForOnOff(true, false); |
| 741 | mEnable = false; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 742 | handleDisable(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 743 | waitForOnOff(false, false); |
| 744 | } else { |
| 745 | mEnable = false; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 746 | handleDisable(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 747 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 748 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 749 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 750 | case MESSAGE_REGISTER_ADAPTER: |
| 751 | { |
| 752 | IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 753 | boolean added = mCallbacks.register(callback); |
| 754 | Log.d(TAG,"Added callback: " + (callback == null? "null": callback) +":" +added ); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 755 | } |
| 756 | break; |
| 757 | case MESSAGE_UNREGISTER_ADAPTER: |
| 758 | { |
| 759 | IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 760 | boolean removed = mCallbacks.unregister(callback); |
| 761 | Log.d(TAG,"Removed callback: " + (callback == null? "null": callback) +":" + removed); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 762 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 763 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 764 | case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK: |
| 765 | { |
| 766 | IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 767 | mStateChangeCallbacks.register(callback); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 768 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 769 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 770 | case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK: |
| 771 | { |
| 772 | IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 773 | mStateChangeCallbacks.unregister(callback); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 774 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 775 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 776 | case MESSAGE_BLUETOOTH_SERVICE_CONNECTED: |
| 777 | { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 778 | if (DBG) Log.d(TAG,"MESSAGE_BLUETOOTH_SERVICE_CONNECTED: " + msg.arg1); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 779 | |
| 780 | IBinder service = (IBinder) msg.obj; |
| 781 | synchronized(mConnection) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 782 | if (msg.arg1 == SERVICE_IBLUETOOTHGATT) { |
| 783 | mBluetoothGatt = IBluetoothGatt.Stub.asInterface(service); |
| 784 | break; |
| 785 | } // else must be SERVICE_IBLUETOOTH |
| 786 | |
| 787 | //Remove timeout |
| 788 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
| 789 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 790 | mBinding = false; |
| 791 | mBluetooth = IBluetooth.Stub.asInterface(service); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 792 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 793 | if (mConnection.isGetNameAddressOnly()) { |
| 794 | //Request GET NAME AND ADDRESS |
| 795 | Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS); |
| 796 | mHandler.sendMessage(getMsg); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 797 | if (!mEnable) return; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 798 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 799 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 800 | mConnection.setGetNameAddressOnly(false); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 801 | //Register callback object |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 802 | try { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 803 | mBluetooth.registerCallback(mBluetoothCallback); |
| 804 | } catch (RemoteException re) { |
| 805 | Log.e(TAG, "Unable to register BluetoothCallback",re); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 806 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 807 | //Inform BluetoothAdapter instances that service is up |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 808 | sendBluetoothServiceUpCallback(); |
| 809 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 810 | //Do enable request |
| 811 | try { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 812 | if (mQuietEnable == false) { |
| 813 | if(!mBluetooth.enable()) { |
| 814 | Log.e(TAG,"IBluetooth.enable() returned false"); |
| 815 | } |
| 816 | } |
| 817 | else |
| 818 | { |
| 819 | if(!mBluetooth.enableNoAutoConnect()) { |
| 820 | Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false"); |
| 821 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 822 | } |
| 823 | } catch (RemoteException e) { |
| 824 | Log.e(TAG,"Unable to call enable()",e); |
| 825 | } |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 826 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 827 | |
| 828 | if (!mEnable) { |
| 829 | waitForOnOff(true, false); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 830 | handleDisable(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 831 | waitForOnOff(false, false); |
| 832 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 833 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 834 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 835 | case MESSAGE_TIMEOUT_BIND: { |
| 836 | Log.e(TAG, "MESSAGE_TIMEOUT_BIND"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 837 | synchronized(mConnection) { |
| 838 | mBinding = false; |
| 839 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 840 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 841 | } |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 842 | case MESSAGE_BLUETOOTH_STATE_CHANGE: |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 843 | { |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 844 | int prevState = msg.arg1; |
| 845 | int newState = msg.arg2; |
| 846 | 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] | 847 | mState = newState; |
| 848 | bluetoothStateChangeHandler(prevState, newState); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 849 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 850 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 851 | case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: |
| 852 | { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 853 | Log.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: " + msg.arg1); |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 854 | synchronized(mConnection) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 855 | if (msg.arg1 == SERVICE_IBLUETOOTH) { |
| 856 | // if service is unbinded already, do nothing and return |
| 857 | if (mBluetooth == null) break; |
| 858 | mBluetooth = null; |
| 859 | } else if (msg.arg1 == SERVICE_IBLUETOOTHGATT) { |
| 860 | mBluetoothGatt = null; |
| 861 | break; |
| 862 | } else { |
| 863 | Log.e(TAG, "Bad msg.arg1: " + msg.arg1); |
| 864 | break; |
| 865 | } |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 866 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 867 | |
| 868 | if (mEnable) { |
| 869 | mEnable = false; |
| 870 | // Send a Bluetooth Restart message |
| 871 | Message restartMsg = mHandler.obtainMessage( |
| 872 | MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 873 | mHandler.sendMessageDelayed(restartMsg, |
| 874 | SERVICE_RESTART_TIME_MS); |
| 875 | } |
| 876 | |
| 877 | if (!mConnection.isGetNameAddressOnly()) { |
| 878 | sendBluetoothServiceDownCallback(); |
| 879 | |
| 880 | // Send BT state broadcast to update |
| 881 | // the BT icon correctly |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 882 | if ((mState == BluetoothAdapter.STATE_TURNING_ON) || |
| 883 | (mState == BluetoothAdapter.STATE_ON)) { |
| 884 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON, |
| 885 | BluetoothAdapter.STATE_TURNING_OFF); |
| 886 | mState = BluetoothAdapter.STATE_TURNING_OFF; |
| 887 | } |
| 888 | if (mState == BluetoothAdapter.STATE_TURNING_OFF) { |
| 889 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF, |
| 890 | BluetoothAdapter.STATE_OFF); |
| 891 | } |
| 892 | |
| 893 | mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 894 | mState = BluetoothAdapter.STATE_OFF; |
| 895 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 896 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 897 | } |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 898 | case MESSAGE_RESTART_BLUETOOTH_SERVICE: |
| 899 | { |
| 900 | Log.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE:" |
| 901 | +" Restart IBluetooth service"); |
| 902 | /* Enable without persisting the setting as |
| 903 | it doesnt change when IBluetooth |
| 904 | service restarts */ |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 905 | mEnable = true; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 906 | handleEnable(mQuietEnable); |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 907 | break; |
| 908 | } |
| 909 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 910 | case MESSAGE_TIMEOUT_UNBIND: |
| 911 | { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 912 | Log.e(TAG, "MESSAGE_TIMEOUT_UNBIND"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 913 | synchronized(mConnection) { |
| 914 | mUnbinding = false; |
| 915 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 916 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 917 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 918 | |
| 919 | case MESSAGE_USER_SWITCHED: |
| 920 | { |
| 921 | if (DBG) { |
| 922 | Log.d(TAG, "MESSAGE_USER_SWITCHED"); |
| 923 | } |
| 924 | mHandler.removeMessages(MESSAGE_USER_SWITCHED); |
| 925 | /* disable and enable BT when detect a user switch */ |
| 926 | if (mEnable && mBluetooth != null) { |
| 927 | synchronized (mConnection) { |
| 928 | if (mBluetooth != null) { |
| 929 | //Unregister callback object |
| 930 | try { |
| 931 | mBluetooth.unregisterCallback(mBluetoothCallback); |
| 932 | } catch (RemoteException re) { |
| 933 | Log.e(TAG, "Unable to unregister",re); |
| 934 | } |
| 935 | } |
| 936 | } |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 937 | |
| 938 | if (mState == BluetoothAdapter.STATE_TURNING_OFF) { |
| 939 | // MESSAGE_USER_SWITCHED happened right after MESSAGE_ENABLE |
| 940 | bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_OFF); |
| 941 | mState = BluetoothAdapter.STATE_OFF; |
| 942 | } |
| 943 | if (mState == BluetoothAdapter.STATE_OFF) { |
| 944 | bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_TURNING_ON); |
| 945 | mState = BluetoothAdapter.STATE_TURNING_ON; |
| 946 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 947 | |
| 948 | waitForOnOff(true, false); |
| 949 | |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 950 | if (mState == BluetoothAdapter.STATE_TURNING_ON) { |
| 951 | bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_ON); |
| 952 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 953 | |
| 954 | // disable |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 955 | handleDisable(); |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 956 | // Pbap service need receive STATE_TURNING_OFF intent to close |
| 957 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON, |
| 958 | BluetoothAdapter.STATE_TURNING_OFF); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 959 | |
| 960 | waitForOnOff(false, true); |
| 961 | |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 962 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF, |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 963 | BluetoothAdapter.STATE_OFF); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 964 | sendBluetoothServiceDownCallback(); |
| 965 | synchronized (mConnection) { |
| 966 | if (mBluetooth != null) { |
| 967 | mBluetooth = null; |
| 968 | //Unbind |
| 969 | mContext.unbindService(mConnection); |
| 970 | } |
| 971 | } |
| 972 | SystemClock.sleep(100); |
| 973 | |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 974 | mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE); |
| 975 | mState = BluetoothAdapter.STATE_OFF; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 976 | // enable |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 977 | handleEnable(mQuietEnable); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 978 | } else if (mBinding || mBluetooth != null) { |
| 979 | Message userMsg = mHandler.obtainMessage(MESSAGE_USER_SWITCHED); |
| 980 | userMsg.arg2 = 1 + msg.arg2; |
| 981 | // if user is switched when service is being binding |
| 982 | // delay sending MESSAGE_USER_SWITCHED |
| 983 | mHandler.sendMessageDelayed(userMsg, USER_SWITCHED_TIME_MS); |
| 984 | if (DBG) { |
| 985 | Log.d(TAG, "delay MESSAGE_USER_SWITCHED " + userMsg.arg2); |
| 986 | } |
| 987 | } |
| 988 | break; |
| 989 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 990 | } |
| 991 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 992 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 993 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 994 | private void handleEnable(boolean quietMode) { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 995 | mQuietEnable = quietMode; |
| 996 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 997 | synchronized(mConnection) { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 998 | if ((mBluetooth == null) && (!mBinding)) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 999 | //Start bind timeout and bind |
| 1000 | Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND); |
| 1001 | mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS); |
| 1002 | mConnection.setGetNameAddressOnly(false); |
| 1003 | Intent i = new Intent(IBluetooth.class.getName()); |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 1004 | if (!mContext.bindServiceAsUser(i, mConnection,Context.BIND_AUTO_CREATE, |
| 1005 | UserHandle.CURRENT)) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1006 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
| 1007 | Log.e(TAG, "Fail to bind to: " + IBluetooth.class.getName()); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1008 | } else { |
| 1009 | mBinding = true; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1010 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1011 | } else if (mBluetooth != null) { |
| 1012 | if (mConnection.isGetNameAddressOnly()) { |
| 1013 | // if GetNameAddressOnly is set, we can clear this flag, |
| 1014 | // so the service won't be unbind |
| 1015 | // after name and address are saved |
| 1016 | mConnection.setGetNameAddressOnly(false); |
| 1017 | //Register callback object |
| 1018 | try { |
| 1019 | mBluetooth.registerCallback(mBluetoothCallback); |
| 1020 | } catch (RemoteException re) { |
| 1021 | Log.e(TAG, "Unable to register BluetoothCallback",re); |
| 1022 | } |
| 1023 | //Inform BluetoothAdapter instances that service is up |
| 1024 | sendBluetoothServiceUpCallback(); |
| 1025 | } |
| 1026 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1027 | //Enable bluetooth |
| 1028 | try { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1029 | if (!mQuietEnable) { |
| 1030 | if(!mBluetooth.enable()) { |
| 1031 | Log.e(TAG,"IBluetooth.enable() returned false"); |
| 1032 | } |
| 1033 | } |
| 1034 | else { |
| 1035 | if(!mBluetooth.enableNoAutoConnect()) { |
| 1036 | Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false"); |
| 1037 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1038 | } |
| 1039 | } catch (RemoteException e) { |
| 1040 | Log.e(TAG,"Unable to call enable()",e); |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1046 | private void handleDisable() { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1047 | synchronized(mConnection) { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1048 | // don't need to disable if GetNameAddressOnly is set, |
| 1049 | // service will be unbinded after Name and Address are saved |
| 1050 | if ((mBluetooth != null) && (!mConnection.isGetNameAddressOnly())) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1051 | if (DBG) Log.d(TAG,"Sending off request."); |
| 1052 | |
| 1053 | try { |
| 1054 | if(!mBluetooth.disable()) { |
| 1055 | Log.e(TAG,"IBluetooth.disable() returned false"); |
| 1056 | } |
| 1057 | } catch (RemoteException e) { |
| 1058 | Log.e(TAG,"Unable to call disable()",e); |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1063 | |
| 1064 | private boolean checkIfCallerIsForegroundUser() { |
| 1065 | int foregroundUser; |
| 1066 | int callingUser = UserHandle.getCallingUserId(); |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 1067 | int callingUid = Binder.getCallingUid(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1068 | long callingIdentity = Binder.clearCallingIdentity(); |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 1069 | int callingAppId = UserHandle.getAppId(callingUid); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1070 | boolean valid = false; |
| 1071 | try { |
| 1072 | foregroundUser = ActivityManager.getCurrentUser(); |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 1073 | valid = (callingUser == foregroundUser) || |
| 1074 | callingAppId == Process.NFC_UID; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1075 | if (DBG) { |
| 1076 | Log.d(TAG, "checkIfCallerIsForegroundUser: valid=" + valid |
| 1077 | + " callingUser=" + callingUser |
| 1078 | + " foregroundUser=" + foregroundUser); |
| 1079 | } |
| 1080 | } finally { |
| 1081 | Binder.restoreCallingIdentity(callingIdentity); |
| 1082 | } |
| 1083 | return valid; |
| 1084 | } |
| 1085 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1086 | private void bluetoothStateChangeHandler(int prevState, int newState) { |
| 1087 | if (prevState != newState) { |
| 1088 | //Notify all proxy objects first of adapter state change |
| 1089 | if (newState == BluetoothAdapter.STATE_ON || newState == BluetoothAdapter.STATE_OFF) { |
| 1090 | boolean isUp = (newState==BluetoothAdapter.STATE_ON); |
| 1091 | sendBluetoothStateCallback(isUp); |
| 1092 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1093 | if (isUp) { |
| 1094 | // connect to GattService |
| 1095 | Intent i = new Intent(IBluetoothGatt.class.getName()); |
| 1096 | if (!mContext.bindServiceAsUser(i, mConnection, Context.BIND_AUTO_CREATE, |
| 1097 | UserHandle.CURRENT)) { |
| 1098 | Log.e(TAG, "Fail to bind to: " + IBluetoothGatt.class.getName()); |
| 1099 | } |
| 1100 | } else { |
| 1101 | //If Bluetooth is off, send service down event to proxy objects, and unbind |
| 1102 | if (!isUp && canUnbindBluetoothService()) { |
| 1103 | sendBluetoothServiceDownCallback(); |
| 1104 | unbindAndFinish(); |
| 1105 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | //Send broadcast message to everyone else |
| 1110 | Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED); |
| 1111 | intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState); |
| 1112 | intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState); |
| 1113 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 1114 | if (DBG) Log.d(TAG,"Bluetooth State Change Intent: " + prevState + " -> " + newState); |
| 1115 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL, |
| 1116 | BLUETOOTH_PERM); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | /** |
| 1121 | * if on is true, wait for state become ON |
| 1122 | * if off is true, wait for state become OFF |
| 1123 | * if both on and off are false, wait for state not ON |
| 1124 | */ |
| 1125 | private boolean waitForOnOff(boolean on, boolean off) { |
| 1126 | int i = 0; |
| 1127 | while (i < 10) { |
| 1128 | synchronized(mConnection) { |
| 1129 | try { |
| 1130 | if (mBluetooth == null) break; |
| 1131 | if (on) { |
| 1132 | if (mBluetooth.getState() == BluetoothAdapter.STATE_ON) return true; |
| 1133 | } else if (off) { |
| 1134 | if (mBluetooth.getState() == BluetoothAdapter.STATE_OFF) return true; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 1135 | } else { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1136 | if (mBluetooth.getState() != BluetoothAdapter.STATE_ON) return true; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 1137 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1138 | } catch (RemoteException e) { |
| 1139 | Log.e(TAG, "getState()", e); |
| 1140 | break; |
| 1141 | } |
| 1142 | } |
| 1143 | if (on || off) { |
| 1144 | SystemClock.sleep(300); |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 1145 | } else { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1146 | SystemClock.sleep(50); |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 1147 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1148 | i++; |
| 1149 | } |
| 1150 | Log.e(TAG,"waitForOnOff time out"); |
| 1151 | return false; |
| 1152 | } |
Zhihai Xu | 681ae7f | 2012-11-12 15:14:18 -0800 | [diff] [blame] | 1153 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1154 | private void sendDisableMsg() { |
| 1155 | mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISABLE)); |
| 1156 | } |
| 1157 | |
| 1158 | private void sendEnableMsg(boolean quietMode) { |
| 1159 | mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_ENABLE, |
| 1160 | quietMode ? 1 : 0, 0)); |
| 1161 | } |
| 1162 | |
Zhihai Xu | 681ae7f | 2012-11-12 15:14:18 -0800 | [diff] [blame] | 1163 | private boolean canUnbindBluetoothService() { |
| 1164 | synchronized(mConnection) { |
| 1165 | //Only unbind with mEnable flag not set |
| 1166 | //For race condition: disable and enable back-to-back |
| 1167 | //Avoid unbind right after enable due to callback from disable |
| 1168 | //Only unbind with Bluetooth at OFF state |
| 1169 | //Only unbind without any MESSAGE_BLUETOOTH_STATE_CHANGE message |
| 1170 | try { |
| 1171 | if (mEnable || (mBluetooth == null)) return false; |
| 1172 | if (mHandler.hasMessages(MESSAGE_BLUETOOTH_STATE_CHANGE)) return false; |
| 1173 | return (mBluetooth.getState() == BluetoothAdapter.STATE_OFF); |
| 1174 | } catch (RemoteException e) { |
| 1175 | Log.e(TAG, "getState()", e); |
| 1176 | } |
| 1177 | } |
| 1178 | return false; |
| 1179 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1180 | } |