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