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