fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Google Inc. |
| 3 | */ |
| 4 | |
| 5 | package com.android.server; |
| 6 | |
| 7 | import android.bluetooth.BluetoothAdapter; |
| 8 | import android.bluetooth.IBluetooth; |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 9 | import android.bluetooth.IBluetoothCallback; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 10 | import android.bluetooth.IBluetoothManager; |
| 11 | import android.bluetooth.IBluetoothManagerCallback; |
| 12 | import android.bluetooth.IBluetoothStateChangeCallback; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 13 | import android.content.BroadcastReceiver; |
| 14 | import android.content.ComponentName; |
| 15 | import android.content.ContentResolver; |
| 16 | import android.content.Context; |
| 17 | import android.content.Intent; |
| 18 | import android.content.IntentFilter; |
| 19 | import android.content.ServiceConnection; |
| 20 | import android.os.Handler; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 21 | import android.os.IBinder; |
| 22 | import android.os.Message; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 23 | import android.os.RemoteCallbackList; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 24 | import android.os.RemoteException; |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 25 | import android.os.Binder; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 26 | import android.os.UserHandle; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 27 | import android.provider.Settings; |
| 28 | import android.util.Log; |
| 29 | import java.util.List; |
| 30 | import java.util.ArrayList; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 31 | class BluetoothManagerService extends IBluetoothManager.Stub { |
| 32 | private static final String TAG = "BluetoothManagerService"; |
| 33 | private static final boolean DBG = true; |
| 34 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 35 | private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN; |
| 36 | private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 37 | private static final String ACTION_SERVICE_STATE_CHANGED="com.android.bluetooth.btservice.action.STATE_CHANGED"; |
| 38 | private static final String EXTRA_ACTION="action"; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 39 | private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address"; |
| 40 | private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name"; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 41 | private static final int TIMEOUT_BIND_MS = 3000; //Maximum msec to wait for a bind |
| 42 | 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] | 43 | //Maximum msec to wait for service restart |
| 44 | private static final int SERVICE_RESTART_TIME_MS = 200; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 45 | |
| 46 | private static final int MESSAGE_ENABLE = 1; |
| 47 | private static final int MESSAGE_DISABLE = 2; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 48 | private static final int MESSAGE_REGISTER_ADAPTER = 20; |
| 49 | private static final int MESSAGE_UNREGISTER_ADAPTER = 21; |
| 50 | private static final int MESSAGE_REGISTER_STATE_CHANGE_CALLBACK = 30; |
| 51 | private static final int MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK = 31; |
| 52 | private static final int MESSAGE_BLUETOOTH_SERVICE_CONNECTED = 40; |
| 53 | private static final int MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED = 41; |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 54 | private static final int MESSAGE_RESTART_BLUETOOTH_SERVICE = 42; |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 55 | private static final int MESSAGE_BLUETOOTH_STATE_CHANGE=60; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 56 | private static final int MESSAGE_TIMEOUT_BIND =100; |
| 57 | private static final int MESSAGE_TIMEOUT_UNBIND =101; |
| 58 | private static final int MESSAGE_GET_NAME_AND_ADDRESS=200; |
| 59 | private static final int MESSAGE_SAVE_NAME_AND_ADDRESS=201; |
| 60 | private static final int MAX_SAVE_RETRIES=3; |
| 61 | |
| 62 | private final Context mContext; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 63 | |
| 64 | // Locks are not provided for mName and mAddress. |
| 65 | // They are accessed in handler or broadcast receiver, same thread context. |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 66 | private String mAddress; |
| 67 | private String mName; |
Matthew Xie | 6fde309 | 2012-07-11 17:10:07 -0700 | [diff] [blame] | 68 | private final ContentResolver mContentResolver; |
| 69 | private final RemoteCallbackList<IBluetoothManagerCallback> mCallbacks; |
| 70 | private final RemoteCallbackList<IBluetoothStateChangeCallback> mStateChangeCallbacks; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 71 | private IBluetooth mBluetooth; |
| 72 | private boolean mBinding; |
| 73 | private boolean mUnbinding; |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 74 | private boolean mQuietEnable = false; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 75 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 76 | private void registerForAirplaneMode(IntentFilter filter) { |
| 77 | final ContentResolver resolver = mContext.getContentResolver(); |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 78 | final String airplaneModeRadios = Settings.Global.getString(resolver, |
| 79 | Settings.Global.AIRPLANE_MODE_RADIOS); |
| 80 | final String toggleableRadios = Settings.Global.getString(resolver, |
| 81 | Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 82 | boolean mIsAirplaneSensitive = airplaneModeRadios == null ? true : |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 83 | airplaneModeRadios.contains(Settings.Global.RADIO_BLUETOOTH); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 84 | if (mIsAirplaneSensitive) { |
| 85 | filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 86 | } |
| 87 | } |
| 88 | |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 89 | private final IBluetoothCallback mBluetoothCallback = new IBluetoothCallback.Stub() { |
| 90 | @Override |
| 91 | public void onBluetoothStateChange(int prevState, int newState) throws RemoteException { |
| 92 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_STATE_CHANGE,prevState,newState); |
| 93 | mHandler.sendMessage(msg); |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 98 | @Override |
| 99 | public void onReceive(Context context, Intent intent) { |
| 100 | String action = intent.getAction(); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 101 | if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action)) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 102 | String newName = intent.getStringExtra(BluetoothAdapter.EXTRA_LOCAL_NAME); |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 103 | if (DBG) Log.d(TAG, "Bluetooth Adapter name changed to " + newName); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 104 | if (newName != null) { |
| 105 | storeNameAndAddress(newName, null); |
| 106 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 107 | } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) { |
| 108 | if (isAirplaneModeOn()) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 109 | // disable without persisting the setting |
| 110 | handleDisable(false); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 111 | } else { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 112 | if (isBluetoothPersistedStateOn()) { |
| 113 | // enable without persisting the setting |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 114 | handleEnable(false, false); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 115 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 116 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | BluetoothManagerService(Context context) { |
| 122 | mContext = context; |
| 123 | mBluetooth = null; |
| 124 | mBinding = false; |
| 125 | mUnbinding = false; |
| 126 | mAddress = null; |
| 127 | mName = null; |
| 128 | mContentResolver = context.getContentResolver(); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 129 | mCallbacks = new RemoteCallbackList<IBluetoothManagerCallback>(); |
| 130 | mStateChangeCallbacks = new RemoteCallbackList<IBluetoothStateChangeCallback>(); |
Matthew Xie | 6fde309 | 2012-07-11 17:10:07 -0700 | [diff] [blame] | 131 | IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); |
| 132 | filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED); |
| 133 | registerForAirplaneMode(filter); |
| 134 | mContext.registerReceiver(mReceiver, filter); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 135 | boolean airplaneModeOn = isAirplaneModeOn(); |
| 136 | boolean bluetoothOn = isBluetoothPersistedStateOn(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 137 | loadStoredNameAndAddress(); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 138 | if (DBG) Log.d(TAG, "airplaneModeOn: " + airplaneModeOn + " bluetoothOn: " + bluetoothOn); |
Andre Eisenbach | a732ffd | 2012-05-02 00:39:24 -0700 | [diff] [blame] | 139 | if (bluetoothOn) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 140 | //Enable |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 141 | if (DBG) Log.d(TAG, "Auto-enabling Bluetooth."); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 142 | enable(); |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 143 | } else if (!isNameAndAddressSet()) { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 144 | //Sync the Bluetooth name and address from the Bluetooth Adapter |
| 145 | if (DBG) Log.d(TAG,"Retrieving Bluetooth Adapter name and address..."); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 146 | getNameAndAddress(); |
| 147 | } |
| 148 | } |
| 149 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 150 | /** |
| 151 | * Returns true if airplane mode is currently on |
| 152 | */ |
| 153 | private final boolean isAirplaneModeOn() { |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 154 | return Settings.Global.getInt(mContext.getContentResolver(), |
| 155 | Settings.Global.AIRPLANE_MODE_ON, 0) == 1; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns true if the Bluetooth saved state is "on" |
| 160 | */ |
| 161 | private final boolean isBluetoothPersistedStateOn() { |
| 162 | return Settings.Secure.getInt(mContentResolver, |
| 163 | Settings.Secure.BLUETOOTH_ON, 0) ==1; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Save the Bluetooth on/off state |
| 168 | * |
| 169 | */ |
| 170 | private void persistBluetoothSetting(boolean setOn) { |
| 171 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 172 | Settings.Secure.BLUETOOTH_ON, |
| 173 | setOn ? 1 : 0); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Returns true if the Bluetooth Adapter's name and address is |
| 178 | * locally cached |
| 179 | * @return |
| 180 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 181 | private boolean isNameAndAddressSet() { |
| 182 | return mName !=null && mAddress!= null && mName.length()>0 && mAddress.length()>0; |
| 183 | } |
| 184 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 185 | /** |
| 186 | * Retrieve the Bluetooth Adapter's name and address and save it in |
| 187 | * in the local cache |
| 188 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 189 | private void loadStoredNameAndAddress() { |
| 190 | if (DBG) Log.d(TAG, "Loading stored name and address"); |
| 191 | mName = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME); |
| 192 | mAddress = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS); |
| 193 | if (mName == null || mAddress == null) { |
| 194 | if (DBG) Log.d(TAG, "Name or address not cached..."); |
| 195 | } |
| 196 | } |
| 197 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 198 | /** |
| 199 | * Save the Bluetooth name and address in the persistent store. |
| 200 | * Only non-null values will be saved. |
| 201 | * @param name |
| 202 | * @param address |
| 203 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 204 | private void storeNameAndAddress(String name, String address) { |
| 205 | if (name != null) { |
| 206 | Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME, name); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 207 | mName = name; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 208 | if (DBG) Log.d(TAG,"Stored Bluetooth name: " + |
| 209 | Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_NAME)); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | if (address != null) { |
| 213 | Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS, address); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 214 | mAddress=address; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 215 | if (DBG) Log.d(TAG,"Stored Bluetoothaddress: " + |
| 216 | Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_ADDRESS)); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | public IBluetooth registerAdapter(IBluetoothManagerCallback callback){ |
| 221 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 222 | "Need BLUETOOTH permission"); |
| 223 | Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_ADAPTER); |
| 224 | msg.obj = callback; |
| 225 | mHandler.sendMessage(msg); |
| 226 | synchronized(mConnection) { |
| 227 | return mBluetooth; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | public void unregisterAdapter(IBluetoothManagerCallback callback) { |
| 232 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 233 | "Need BLUETOOTH permission"); |
| 234 | Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_ADAPTER); |
| 235 | msg.obj = callback; |
| 236 | mHandler.sendMessage(msg); |
| 237 | } |
| 238 | |
| 239 | public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) { |
| 240 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 241 | "Need BLUETOOTH permission"); |
| 242 | Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_STATE_CHANGE_CALLBACK); |
| 243 | msg.obj = callback; |
| 244 | mHandler.sendMessage(msg); |
| 245 | } |
| 246 | |
| 247 | public void unregisterStateChangeCallback(IBluetoothStateChangeCallback callback) { |
| 248 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 249 | "Need BLUETOOTH permission"); |
| 250 | Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK); |
| 251 | msg.obj = callback; |
| 252 | mHandler.sendMessage(msg); |
| 253 | } |
| 254 | |
| 255 | public boolean isEnabled() { |
| 256 | synchronized(mConnection) { |
| 257 | try { |
| 258 | return (mBluetooth != null && mBluetooth.isEnabled()); |
| 259 | } catch (RemoteException e) { |
| 260 | Log.e(TAG, "isEnabled()", e); |
| 261 | } |
| 262 | } |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | public void getNameAndAddress() { |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 267 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 268 | Log.d(TAG,"getNameAndAddress(): mBluetooth = " + mBluetooth + |
| 269 | " mBinding = " + mBinding); |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 270 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 271 | synchronized(mConnection) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 272 | if (mBinding) return; |
| 273 | if (mConnection == null) mBinding = true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 274 | } |
| 275 | Message msg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS); |
| 276 | mHandler.sendMessage(msg); |
| 277 | } |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 278 | public boolean enableNoAutoConnect() |
| 279 | { |
| 280 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 281 | "Need BLUETOOTH ADMIN permission"); |
| 282 | if (DBG) { |
| 283 | Log.d(TAG,"enableNoAutoConnect(): mBluetooth =" + mBluetooth + |
| 284 | " mBinding = " + mBinding); |
| 285 | } |
| 286 | if (Binder.getCallingUid() != android.os.Process.NFC_UID) { |
| 287 | throw new SecurityException("no permission to enable Bluetooth quietly"); |
| 288 | } |
| 289 | synchronized(mConnection) { |
| 290 | if (mBinding) { |
| 291 | Log.w(TAG,"enableNoAutoConnect(): binding in progress. Returning.."); |
| 292 | return true; |
| 293 | } |
| 294 | if (mConnection == null) mBinding = true; |
| 295 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 296 | |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 297 | Message msg = mHandler.obtainMessage(MESSAGE_ENABLE); |
| 298 | msg.arg1=0; //No persist |
| 299 | msg.arg2=1; //Quiet mode |
| 300 | mHandler.sendMessage(msg); |
| 301 | return true; |
| 302 | |
| 303 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 304 | public boolean enable() { |
| 305 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 306 | "Need BLUETOOTH ADMIN permission"); |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 307 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 308 | Log.d(TAG,"enable(): mBluetooth =" + mBluetooth + |
| 309 | " mBinding = " + mBinding); |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 310 | } |
| 311 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 312 | synchronized(mConnection) { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 313 | if (mBinding) { |
| 314 | Log.w(TAG,"enable(): binding in progress. Returning.."); |
| 315 | return true; |
| 316 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 317 | if (mConnection == null) mBinding = true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 318 | } |
fredc | 116d1d46 | 2012-04-20 14:47:08 -0700 | [diff] [blame] | 319 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 320 | Message msg = mHandler.obtainMessage(MESSAGE_ENABLE); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 321 | msg.arg1=1; //persist |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 322 | msg.arg2=0; //No Quiet Mode |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 323 | mHandler.sendMessage(msg); |
| 324 | return true; |
| 325 | } |
| 326 | |
| 327 | public boolean disable(boolean persist) { |
| 328 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 329 | "Need BLUETOOTH ADMIN permissicacheNameAndAddresson"); |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 330 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 331 | Log.d(TAG,"disable(): mBluetooth = " + mBluetooth + |
| 332 | " mBinding = " + mBinding); |
| 333 | } |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 334 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 335 | synchronized(mConnection) { |
| 336 | if (mBluetooth == null) return false; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 337 | } |
| 338 | Message msg = mHandler.obtainMessage(MESSAGE_DISABLE); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 339 | msg.arg1=(persist?1:0); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 340 | mHandler.sendMessage(msg); |
| 341 | return true; |
| 342 | } |
| 343 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 344 | public void unbindAndFinish() { |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 345 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 346 | Log.d(TAG,"unbindAndFinish(): " + mBluetooth + |
| 347 | " mBinding = " + mBinding); |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 348 | } |
| 349 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 350 | synchronized (mConnection) { |
| 351 | if (mUnbinding) return; |
| 352 | mUnbinding = true; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 353 | if (mConnection != null) { |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 354 | if (!mConnection.isGetNameAddressOnly()) { |
| 355 | //Unregister callback object |
| 356 | try { |
| 357 | mBluetooth.unregisterCallback(mBluetoothCallback); |
| 358 | } catch (RemoteException re) { |
| 359 | Log.e(TAG, "Unable to register BluetoothCallback",re); |
| 360 | } |
| 361 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 362 | if (DBG) Log.d(TAG, "Sending unbind request."); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 363 | mBluetooth = null; |
| 364 | //Unbind |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 365 | mContext.unbindService(mConnection); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 366 | mUnbinding = false; |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 367 | } else { |
| 368 | mUnbinding=false; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 373 | private void sendBluetoothStateCallback(boolean isUp) { |
| 374 | int n = mStateChangeCallbacks.beginBroadcast(); |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 375 | if (DBG) Log.d(TAG,"Broadcasting onBluetoothStateChange("+isUp+") to " + n + " receivers."); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 376 | for (int i=0; i <n;i++) { |
| 377 | try { |
| 378 | mStateChangeCallbacks.getBroadcastItem(i).onBluetoothStateChange(isUp); |
| 379 | } catch (RemoteException e) { |
| 380 | Log.e(TAG, "Unable to call onBluetoothStateChange() on callback #" + i , e); |
| 381 | } |
| 382 | } |
| 383 | mStateChangeCallbacks.finishBroadcast(); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Inform BluetoothAdapter instances that Adapter service is down |
| 388 | */ |
| 389 | private void sendBluetoothServiceDownCallback() { |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 390 | if (!mConnection.isGetNameAddressOnly()) { |
| 391 | if (DBG) Log.d(TAG,"Calling onBluetoothServiceDown callbacks"); |
| 392 | int n = mCallbacks.beginBroadcast(); |
| 393 | Log.d(TAG,"Broadcasting onBluetoothServiceDown() to " + n + " receivers."); |
| 394 | for (int i=0; i <n;i++) { |
| 395 | try { |
| 396 | mCallbacks.getBroadcastItem(i).onBluetoothServiceDown(); |
| 397 | } catch (RemoteException e) { |
| 398 | Log.e(TAG, "Unable to call onBluetoothServiceDown() on callback #" + i, e); |
| 399 | } |
| 400 | } |
| 401 | mCallbacks.finishBroadcast(); |
| 402 | } |
| 403 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 404 | public String getAddress() { |
| 405 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 406 | "Need BLUETOOTH ADMIN permission"); |
fredc | 116d1d46 | 2012-04-20 14:47:08 -0700 | [diff] [blame] | 407 | synchronized(mConnection) { |
| 408 | if (mBluetooth != null) { |
| 409 | try { |
| 410 | return mBluetooth.getAddress(); |
| 411 | } catch (RemoteException e) { |
| 412 | Log.e(TAG, "getAddress(): Unable to retrieve address remotely..Returning cached address",e); |
| 413 | } |
| 414 | } |
| 415 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 416 | // mAddress is accessed from outside. |
| 417 | // It is alright without a lock. Here, bluetooth is off, no other thread is |
| 418 | // changing mAddress |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 419 | return mAddress; |
| 420 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 421 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 422 | public String getName() { |
| 423 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 424 | "Need BLUETOOTH ADMIN permission"); |
fredc | 116d1d46 | 2012-04-20 14:47:08 -0700 | [diff] [blame] | 425 | synchronized(mConnection) { |
| 426 | if (mBluetooth != null) { |
| 427 | try { |
| 428 | return mBluetooth.getName(); |
| 429 | } catch (RemoteException e) { |
| 430 | Log.e(TAG, "getName(): Unable to retrieve name remotely..Returning cached name",e); |
| 431 | } |
| 432 | } |
| 433 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 434 | // mName is accessed from outside. |
| 435 | // It alright without a lock. Here, bluetooth is off, no other thread is |
| 436 | // changing mName |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 437 | return mName; |
| 438 | } |
| 439 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 440 | private class BluetoothServiceConnection implements ServiceConnection { |
| 441 | |
| 442 | private boolean mGetNameAddressOnly; |
| 443 | |
| 444 | public void setGetNameAddressOnly(boolean getOnly) { |
| 445 | mGetNameAddressOnly = getOnly; |
| 446 | } |
| 447 | |
| 448 | public boolean isGetNameAddressOnly() { |
| 449 | return mGetNameAddressOnly; |
| 450 | } |
| 451 | |
| 452 | public void onServiceConnected(ComponentName className, IBinder service) { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 453 | if (DBG) Log.d(TAG, "BluetoothServiceConnection: connected to AdapterService"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 454 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED); |
| 455 | msg.obj = service; |
| 456 | mHandler.sendMessage(msg); |
| 457 | } |
| 458 | |
| 459 | public void onServiceDisconnected(ComponentName className) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 460 | // Called if we unexpected disconnected. |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 461 | if (DBG) Log.d(TAG, "BluetoothServiceConnection: disconnected from AdapterService"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 462 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED); |
| 463 | mHandler.sendMessage(msg); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | private BluetoothServiceConnection mConnection = new BluetoothServiceConnection(); |
| 468 | |
| 469 | private final Handler mHandler = new Handler() { |
| 470 | @Override |
| 471 | public void handleMessage(Message msg) { |
| 472 | if (DBG) Log.d (TAG, "Message: " + msg.what); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 473 | switch (msg.what) { |
| 474 | case MESSAGE_GET_NAME_AND_ADDRESS: { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 475 | if (DBG) Log.d(TAG,"MESSAGE_GET_NAME_AND_ADDRESS"); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 476 | synchronized(mConnection) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 477 | //Start bind request |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 478 | if (mBluetooth == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 479 | if (DBG) Log.d(TAG, "Binding to service to get name and address"); |
| 480 | mConnection.setGetNameAddressOnly(true); |
| 481 | //Start bind timeout and bind |
| 482 | Message timeoutMsg = mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND); |
| 483 | mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS); |
| 484 | Intent i = new Intent(IBluetooth.class.getName()); |
| 485 | if (!mContext.bindService(i, mConnection, |
| 486 | Context.BIND_AUTO_CREATE)) { |
| 487 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
| 488 | Log.e(TAG, "fail to bind to: " + IBluetooth.class.getName()); |
| 489 | } |
| 490 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 491 | else { |
| 492 | Message saveMsg= mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS); |
| 493 | mHandler.sendMessage(saveMsg); |
| 494 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 495 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 496 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 497 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 498 | case MESSAGE_SAVE_NAME_AND_ADDRESS: { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 499 | if (DBG) Log.d(TAG,"MESSAGE_SAVE_NAME_AND_ADDRESS"); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 500 | synchronized(mConnection) { |
| 501 | if (mBluetooth != null) { |
| 502 | String name = null; |
| 503 | String address = null; |
| 504 | try { |
| 505 | name = mBluetooth.getName(); |
| 506 | address = mBluetooth.getAddress(); |
| 507 | } catch (RemoteException re) { |
| 508 | Log.e(TAG,"",re); |
| 509 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 510 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 511 | if (name != null && address != null) { |
| 512 | storeNameAndAddress(name,address); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 513 | sendBluetoothServiceDownCallback(); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 514 | unbindAndFinish(); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 515 | } else { |
| 516 | if (msg.arg1 < MAX_SAVE_RETRIES) { |
| 517 | Message retryMsg = mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS); |
| 518 | retryMsg.arg1= 1+msg.arg1; |
| 519 | if (DBG) Log.d(TAG,"Retrying name/address remote retrieval and save.....Retry count =" + retryMsg.arg1); |
| 520 | mHandler.sendMessageDelayed(retryMsg, TIMEOUT_SAVE_MS); |
| 521 | } else { |
| 522 | Log.w(TAG,"Maximum name/address remote retrieval retry exceeded"); |
| 523 | sendBluetoothServiceDownCallback(); |
| 524 | unbindAndFinish(); |
| 525 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 529 | break; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 530 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 531 | case MESSAGE_ENABLE: |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 532 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 533 | Log.d(TAG, "MESSAGE_ENABLE: mBluetooth = " + mBluetooth); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 534 | } |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 535 | |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 536 | handleEnable(msg.arg1 == 1, msg.arg2 ==1); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 537 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 538 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 539 | case MESSAGE_DISABLE: |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 540 | handleDisable(msg.arg1 == 1); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 541 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 542 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 543 | case MESSAGE_REGISTER_ADAPTER: |
| 544 | { |
| 545 | IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 546 | boolean added = mCallbacks.register(callback); |
| 547 | Log.d(TAG,"Added callback: " + (callback == null? "null": callback) +":" +added ); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 548 | } |
| 549 | break; |
| 550 | case MESSAGE_UNREGISTER_ADAPTER: |
| 551 | { |
| 552 | IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 553 | boolean removed = mCallbacks.unregister(callback); |
| 554 | Log.d(TAG,"Removed callback: " + (callback == null? "null": callback) +":" + removed); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 555 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 556 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 557 | case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK: |
| 558 | { |
| 559 | IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 560 | mStateChangeCallbacks.register(callback); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 561 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 562 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 563 | case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK: |
| 564 | { |
| 565 | IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 566 | mStateChangeCallbacks.unregister(callback); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 567 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 568 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 569 | case MESSAGE_BLUETOOTH_SERVICE_CONNECTED: |
| 570 | { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 571 | if (DBG) Log.d(TAG,"MESSAGE_BLUETOOTH_SERVICE_CONNECTED"); |
| 572 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 573 | //Remove timeout |
| 574 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
| 575 | |
| 576 | IBinder service = (IBinder) msg.obj; |
| 577 | synchronized(mConnection) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 578 | mBinding = false; |
| 579 | mBluetooth = IBluetooth.Stub.asInterface(service); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 580 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 581 | if (mConnection.isGetNameAddressOnly()) { |
| 582 | //Request GET NAME AND ADDRESS |
| 583 | Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS); |
| 584 | mHandler.sendMessage(getMsg); |
| 585 | return; |
| 586 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 587 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 588 | //Register callback object |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 589 | try { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 590 | mBluetooth.registerCallback(mBluetoothCallback); |
| 591 | } catch (RemoteException re) { |
| 592 | Log.e(TAG, "Unable to register BluetoothCallback",re); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 593 | } |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 594 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 595 | //Inform BluetoothAdapter instances that service is up |
| 596 | int n = mCallbacks.beginBroadcast(); |
| 597 | Log.d(TAG,"Broadcasting onBluetoothServiceUp() to " + n + " receivers."); |
| 598 | for (int i=0; i <n;i++) { |
| 599 | try { |
| 600 | mCallbacks.getBroadcastItem(i).onBluetoothServiceUp(mBluetooth); |
| 601 | } catch (RemoteException e) { |
| 602 | Log.e(TAG, "Unable to call onBluetoothServiceUp() on callback #" + i, e); |
| 603 | } |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 604 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 605 | mCallbacks.finishBroadcast(); |
| 606 | |
| 607 | //Do enable request |
| 608 | try { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 609 | if (mQuietEnable == false) { |
| 610 | if(!mBluetooth.enable()) { |
| 611 | Log.e(TAG,"IBluetooth.enable() returned false"); |
| 612 | } |
| 613 | } |
| 614 | else |
| 615 | { |
| 616 | if(!mBluetooth.enableNoAutoConnect()) { |
| 617 | Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false"); |
| 618 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 619 | } |
| 620 | } catch (RemoteException e) { |
| 621 | Log.e(TAG,"Unable to call enable()",e); |
| 622 | } |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 623 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 624 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 625 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 626 | case MESSAGE_TIMEOUT_BIND: { |
| 627 | Log.e(TAG, "MESSAGE_TIMEOUT_BIND"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 628 | synchronized(mConnection) { |
| 629 | mBinding = false; |
| 630 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 631 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 632 | } |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 633 | case MESSAGE_BLUETOOTH_STATE_CHANGE: |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 634 | { |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 635 | int prevState = msg.arg1; |
| 636 | int newState = msg.arg2; |
| 637 | if (DBG) Log.d(TAG, "MESSAGE_BLUETOOTH_STATE_CHANGE: prevState = " + prevState + ", newState=" + newState); |
| 638 | if (prevState != newState) { |
| 639 | //Notify all proxy objects first of adapter state change |
| 640 | if (newState == BluetoothAdapter.STATE_ON || newState == BluetoothAdapter.STATE_OFF) { |
| 641 | boolean isUp = (newState==BluetoothAdapter.STATE_ON); |
| 642 | sendBluetoothStateCallback(isUp); |
| 643 | |
| 644 | //If Bluetooth is off, send service down event to proxy objects, and unbind |
| 645 | if (!isUp) { |
| 646 | sendBluetoothServiceDownCallback(); |
| 647 | unbindAndFinish(); |
| 648 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 649 | } |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 650 | |
| 651 | //Send broadcast message to everyone else |
| 652 | Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED); |
| 653 | intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState); |
| 654 | intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState); |
| 655 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 656 | if (DBG) Log.d(TAG,"Bluetooth State Change Intent: " + prevState + " -> " + newState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 657 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL, |
| 658 | BLUETOOTH_PERM); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 659 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 660 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 661 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 662 | case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: |
| 663 | { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 664 | if (DBG) Log.d(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED"); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 665 | sendBluetoothServiceDownCallback(); |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 666 | |
| 667 | // Send BT state broadcast to update |
| 668 | // the BT icon correctly |
| 669 | Message stateChangeMsg = mHandler.obtainMessage( |
| 670 | MESSAGE_BLUETOOTH_STATE_CHANGE); |
| 671 | stateChangeMsg.arg1 = BluetoothAdapter.STATE_ON; |
| 672 | stateChangeMsg.arg2 = |
| 673 | BluetoothAdapter.STATE_TURNING_OFF; |
| 674 | mHandler.sendMessage(stateChangeMsg); |
| 675 | synchronized(mConnection) { |
| 676 | mBluetooth = null; |
| 677 | } |
| 678 | // Send a Bluetooth Restart message |
| 679 | Message restartMsg = mHandler.obtainMessage( |
| 680 | MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 681 | mHandler.sendMessageDelayed(restartMsg, |
| 682 | SERVICE_RESTART_TIME_MS); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 683 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 684 | } |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 685 | case MESSAGE_RESTART_BLUETOOTH_SERVICE: |
| 686 | { |
| 687 | Log.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE:" |
| 688 | +" Restart IBluetooth service"); |
| 689 | /* Enable without persisting the setting as |
| 690 | it doesnt change when IBluetooth |
| 691 | service restarts */ |
| 692 | handleEnable(false, mQuietEnable); |
| 693 | break; |
| 694 | } |
| 695 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 696 | case MESSAGE_TIMEOUT_UNBIND: |
| 697 | { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 698 | Log.e(TAG, "MESSAGE_TIMEOUT_UNBIND"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 699 | synchronized(mConnection) { |
| 700 | mUnbinding = false; |
| 701 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 702 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 703 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | }; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 707 | |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 708 | private void handleEnable(boolean persist, boolean quietMode) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 709 | if (persist) { |
| 710 | persistBluetoothSetting(true); |
| 711 | } |
| 712 | |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 713 | mQuietEnable = quietMode; |
| 714 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 715 | synchronized(mConnection) { |
| 716 | if (mBluetooth == null) { |
| 717 | //Start bind timeout and bind |
| 718 | Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND); |
| 719 | mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS); |
| 720 | mConnection.setGetNameAddressOnly(false); |
| 721 | Intent i = new Intent(IBluetooth.class.getName()); |
| 722 | if (!mContext.bindService(i, mConnection,Context.BIND_AUTO_CREATE)) { |
| 723 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
| 724 | Log.e(TAG, "Fail to bind to: " + IBluetooth.class.getName()); |
| 725 | } |
| 726 | } else { |
| 727 | //Check if name and address is loaded if not get it first. |
| 728 | if (!isNameAndAddressSet()) { |
| 729 | try { |
| 730 | if (DBG) Log.d(TAG,"Getting and storing Bluetooth name and address prior to enable."); |
| 731 | storeNameAndAddress(mBluetooth.getName(),mBluetooth.getAddress()); |
| 732 | } catch (RemoteException e) {Log.e(TAG, "", e);}; |
| 733 | } |
| 734 | |
| 735 | //Enable bluetooth |
| 736 | try { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 737 | if (!mQuietEnable) { |
| 738 | if(!mBluetooth.enable()) { |
| 739 | Log.e(TAG,"IBluetooth.enable() returned false"); |
| 740 | } |
| 741 | } |
| 742 | else { |
| 743 | if(!mBluetooth.enableNoAutoConnect()) { |
| 744 | Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false"); |
| 745 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 746 | } |
| 747 | } catch (RemoteException e) { |
| 748 | Log.e(TAG,"Unable to call enable()",e); |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | private void handleDisable(boolean persist) { |
| 755 | synchronized(mConnection) { |
| 756 | if (mBluetooth != null ) { |
| 757 | if (persist) { |
| 758 | persistBluetoothSetting(false); |
| 759 | } |
| 760 | mConnection.setGetNameAddressOnly(false); |
| 761 | if (DBG) Log.d(TAG,"Sending off request."); |
| 762 | |
| 763 | try { |
| 764 | if(!mBluetooth.disable()) { |
| 765 | Log.e(TAG,"IBluetooth.disable() returned false"); |
| 766 | } |
| 767 | } catch (RemoteException e) { |
| 768 | Log.e(TAG,"Unable to call disable()",e); |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 773 | } |