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() { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 162 | return Settings.Global.getInt(mContentResolver, |
| 163 | Settings.Global.BLUETOOTH_ON, 0) ==1; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Save the Bluetooth on/off state |
| 168 | * |
| 169 | */ |
| 170 | private void persistBluetoothSetting(boolean setOn) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 171 | Settings.Global.putInt(mContext.getContentResolver(), |
| 172 | Settings.Global.BLUETOOTH_ON, |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 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){ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 221 | Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_ADAPTER); |
| 222 | msg.obj = callback; |
| 223 | mHandler.sendMessage(msg); |
| 224 | synchronized(mConnection) { |
| 225 | return mBluetooth; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | public void unregisterAdapter(IBluetoothManagerCallback callback) { |
| 230 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 231 | "Need BLUETOOTH permission"); |
| 232 | Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_ADAPTER); |
| 233 | msg.obj = callback; |
| 234 | mHandler.sendMessage(msg); |
| 235 | } |
| 236 | |
| 237 | public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) { |
| 238 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 239 | "Need BLUETOOTH permission"); |
| 240 | Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_STATE_CHANGE_CALLBACK); |
| 241 | msg.obj = callback; |
| 242 | mHandler.sendMessage(msg); |
| 243 | } |
| 244 | |
| 245 | public void unregisterStateChangeCallback(IBluetoothStateChangeCallback callback) { |
| 246 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 247 | "Need BLUETOOTH permission"); |
| 248 | Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK); |
| 249 | msg.obj = callback; |
| 250 | mHandler.sendMessage(msg); |
| 251 | } |
| 252 | |
| 253 | public boolean isEnabled() { |
| 254 | synchronized(mConnection) { |
| 255 | try { |
| 256 | return (mBluetooth != null && mBluetooth.isEnabled()); |
| 257 | } catch (RemoteException e) { |
| 258 | Log.e(TAG, "isEnabled()", e); |
| 259 | } |
| 260 | } |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | public void getNameAndAddress() { |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 265 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 266 | Log.d(TAG,"getNameAndAddress(): mBluetooth = " + mBluetooth + |
| 267 | " mBinding = " + mBinding); |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 268 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 269 | synchronized(mConnection) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 270 | if (mBinding) return; |
| 271 | if (mConnection == null) mBinding = true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 272 | } |
| 273 | Message msg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS); |
| 274 | mHandler.sendMessage(msg); |
| 275 | } |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 276 | public boolean enableNoAutoConnect() |
| 277 | { |
| 278 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 279 | "Need BLUETOOTH ADMIN permission"); |
| 280 | if (DBG) { |
| 281 | Log.d(TAG,"enableNoAutoConnect(): mBluetooth =" + mBluetooth + |
| 282 | " mBinding = " + mBinding); |
| 283 | } |
| 284 | if (Binder.getCallingUid() != android.os.Process.NFC_UID) { |
| 285 | throw new SecurityException("no permission to enable Bluetooth quietly"); |
| 286 | } |
| 287 | synchronized(mConnection) { |
| 288 | if (mBinding) { |
| 289 | Log.w(TAG,"enableNoAutoConnect(): binding in progress. Returning.."); |
| 290 | return true; |
| 291 | } |
| 292 | if (mConnection == null) mBinding = true; |
| 293 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 294 | |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 295 | Message msg = mHandler.obtainMessage(MESSAGE_ENABLE); |
| 296 | msg.arg1=0; //No persist |
| 297 | msg.arg2=1; //Quiet mode |
| 298 | mHandler.sendMessage(msg); |
| 299 | return true; |
| 300 | |
| 301 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 302 | public boolean enable() { |
| 303 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 304 | "Need BLUETOOTH ADMIN permission"); |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 305 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 306 | Log.d(TAG,"enable(): mBluetooth =" + mBluetooth + |
| 307 | " mBinding = " + mBinding); |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 308 | } |
| 309 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 310 | synchronized(mConnection) { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 311 | if (mBinding) { |
| 312 | Log.w(TAG,"enable(): binding in progress. Returning.."); |
| 313 | return true; |
| 314 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 315 | if (mConnection == null) mBinding = true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 316 | } |
fredc | 116d1d46 | 2012-04-20 14:47:08 -0700 | [diff] [blame] | 317 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 318 | Message msg = mHandler.obtainMessage(MESSAGE_ENABLE); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 319 | msg.arg1=1; //persist |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 320 | msg.arg2=0; //No Quiet Mode |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 321 | mHandler.sendMessage(msg); |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | public boolean disable(boolean persist) { |
| 326 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 327 | "Need BLUETOOTH ADMIN permissicacheNameAndAddresson"); |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 328 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 329 | Log.d(TAG,"disable(): mBluetooth = " + mBluetooth + |
| 330 | " mBinding = " + mBinding); |
| 331 | } |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 332 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 333 | synchronized(mConnection) { |
| 334 | if (mBluetooth == null) return false; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 335 | } |
| 336 | Message msg = mHandler.obtainMessage(MESSAGE_DISABLE); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 337 | msg.arg1=(persist?1:0); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 338 | mHandler.sendMessage(msg); |
| 339 | return true; |
| 340 | } |
| 341 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 342 | public void unbindAndFinish() { |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 343 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 344 | Log.d(TAG,"unbindAndFinish(): " + mBluetooth + |
| 345 | " mBinding = " + mBinding); |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 346 | } |
| 347 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 348 | synchronized (mConnection) { |
| 349 | if (mUnbinding) return; |
| 350 | mUnbinding = true; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 351 | if (mConnection != null) { |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 352 | if (!mConnection.isGetNameAddressOnly()) { |
| 353 | //Unregister callback object |
| 354 | try { |
| 355 | mBluetooth.unregisterCallback(mBluetoothCallback); |
| 356 | } catch (RemoteException re) { |
| 357 | Log.e(TAG, "Unable to register BluetoothCallback",re); |
| 358 | } |
| 359 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 360 | if (DBG) Log.d(TAG, "Sending unbind request."); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 361 | mBluetooth = null; |
| 362 | //Unbind |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 363 | mContext.unbindService(mConnection); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 364 | mUnbinding = false; |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 365 | } else { |
| 366 | mUnbinding=false; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 371 | private void sendBluetoothStateCallback(boolean isUp) { |
| 372 | int n = mStateChangeCallbacks.beginBroadcast(); |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 373 | if (DBG) Log.d(TAG,"Broadcasting onBluetoothStateChange("+isUp+") to " + n + " receivers."); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 374 | for (int i=0; i <n;i++) { |
| 375 | try { |
| 376 | mStateChangeCallbacks.getBroadcastItem(i).onBluetoothStateChange(isUp); |
| 377 | } catch (RemoteException e) { |
| 378 | Log.e(TAG, "Unable to call onBluetoothStateChange() on callback #" + i , e); |
| 379 | } |
| 380 | } |
| 381 | mStateChangeCallbacks.finishBroadcast(); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Inform BluetoothAdapter instances that Adapter service is down |
| 386 | */ |
| 387 | private void sendBluetoothServiceDownCallback() { |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 388 | if (!mConnection.isGetNameAddressOnly()) { |
| 389 | if (DBG) Log.d(TAG,"Calling onBluetoothServiceDown callbacks"); |
| 390 | int n = mCallbacks.beginBroadcast(); |
| 391 | Log.d(TAG,"Broadcasting onBluetoothServiceDown() to " + n + " receivers."); |
| 392 | for (int i=0; i <n;i++) { |
| 393 | try { |
| 394 | mCallbacks.getBroadcastItem(i).onBluetoothServiceDown(); |
| 395 | } catch (RemoteException e) { |
| 396 | Log.e(TAG, "Unable to call onBluetoothServiceDown() on callback #" + i, e); |
| 397 | } |
| 398 | } |
| 399 | mCallbacks.finishBroadcast(); |
| 400 | } |
| 401 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 402 | public String getAddress() { |
| 403 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 404 | "Need BLUETOOTH ADMIN permission"); |
fredc | 116d1d46 | 2012-04-20 14:47:08 -0700 | [diff] [blame] | 405 | synchronized(mConnection) { |
| 406 | if (mBluetooth != null) { |
| 407 | try { |
| 408 | return mBluetooth.getAddress(); |
| 409 | } catch (RemoteException e) { |
| 410 | Log.e(TAG, "getAddress(): Unable to retrieve address remotely..Returning cached address",e); |
| 411 | } |
| 412 | } |
| 413 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 414 | // mAddress is accessed from outside. |
| 415 | // It is alright without a lock. Here, bluetooth is off, no other thread is |
| 416 | // changing mAddress |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 417 | return mAddress; |
| 418 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 419 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 420 | public String getName() { |
| 421 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 422 | "Need BLUETOOTH ADMIN permission"); |
fredc | 116d1d46 | 2012-04-20 14:47:08 -0700 | [diff] [blame] | 423 | synchronized(mConnection) { |
| 424 | if (mBluetooth != null) { |
| 425 | try { |
| 426 | return mBluetooth.getName(); |
| 427 | } catch (RemoteException e) { |
| 428 | Log.e(TAG, "getName(): Unable to retrieve name remotely..Returning cached name",e); |
| 429 | } |
| 430 | } |
| 431 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 432 | // mName is accessed from outside. |
| 433 | // It alright without a lock. Here, bluetooth is off, no other thread is |
| 434 | // changing mName |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 435 | return mName; |
| 436 | } |
| 437 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 438 | private class BluetoothServiceConnection implements ServiceConnection { |
| 439 | |
| 440 | private boolean mGetNameAddressOnly; |
| 441 | |
| 442 | public void setGetNameAddressOnly(boolean getOnly) { |
| 443 | mGetNameAddressOnly = getOnly; |
| 444 | } |
| 445 | |
| 446 | public boolean isGetNameAddressOnly() { |
| 447 | return mGetNameAddressOnly; |
| 448 | } |
| 449 | |
| 450 | public void onServiceConnected(ComponentName className, IBinder service) { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 451 | if (DBG) Log.d(TAG, "BluetoothServiceConnection: connected to AdapterService"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 452 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED); |
| 453 | msg.obj = service; |
| 454 | mHandler.sendMessage(msg); |
| 455 | } |
| 456 | |
| 457 | public void onServiceDisconnected(ComponentName className) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 458 | // Called if we unexpected disconnected. |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 459 | if (DBG) Log.d(TAG, "BluetoothServiceConnection: disconnected from AdapterService"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 460 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED); |
| 461 | mHandler.sendMessage(msg); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | private BluetoothServiceConnection mConnection = new BluetoothServiceConnection(); |
| 466 | |
| 467 | private final Handler mHandler = new Handler() { |
| 468 | @Override |
| 469 | public void handleMessage(Message msg) { |
| 470 | if (DBG) Log.d (TAG, "Message: " + msg.what); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 471 | switch (msg.what) { |
| 472 | case MESSAGE_GET_NAME_AND_ADDRESS: { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 473 | if (DBG) Log.d(TAG,"MESSAGE_GET_NAME_AND_ADDRESS"); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 474 | synchronized(mConnection) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 475 | //Start bind request |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 476 | if (mBluetooth == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 477 | if (DBG) Log.d(TAG, "Binding to service to get name and address"); |
| 478 | mConnection.setGetNameAddressOnly(true); |
| 479 | //Start bind timeout and bind |
| 480 | Message timeoutMsg = mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND); |
| 481 | mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS); |
| 482 | Intent i = new Intent(IBluetooth.class.getName()); |
| 483 | if (!mContext.bindService(i, mConnection, |
Matthew Xie | fca9d63 | 2012-10-04 12:25:28 -0700 | [diff] [blame^] | 484 | Context.BIND_AUTO_CREATE, UserHandle.USER_CURRENT)) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 485 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
| 486 | Log.e(TAG, "fail to bind to: " + IBluetooth.class.getName()); |
| 487 | } |
| 488 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 489 | else { |
| 490 | Message saveMsg= mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS); |
| 491 | mHandler.sendMessage(saveMsg); |
| 492 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 493 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 494 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 495 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 496 | case MESSAGE_SAVE_NAME_AND_ADDRESS: { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 497 | if (DBG) Log.d(TAG,"MESSAGE_SAVE_NAME_AND_ADDRESS"); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 498 | synchronized(mConnection) { |
| 499 | if (mBluetooth != null) { |
| 500 | String name = null; |
| 501 | String address = null; |
| 502 | try { |
| 503 | name = mBluetooth.getName(); |
| 504 | address = mBluetooth.getAddress(); |
| 505 | } catch (RemoteException re) { |
| 506 | Log.e(TAG,"",re); |
| 507 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 508 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 509 | if (name != null && address != null) { |
| 510 | storeNameAndAddress(name,address); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 511 | sendBluetoothServiceDownCallback(); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 512 | unbindAndFinish(); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 513 | } else { |
| 514 | if (msg.arg1 < MAX_SAVE_RETRIES) { |
| 515 | Message retryMsg = mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS); |
| 516 | retryMsg.arg1= 1+msg.arg1; |
| 517 | if (DBG) Log.d(TAG,"Retrying name/address remote retrieval and save.....Retry count =" + retryMsg.arg1); |
| 518 | mHandler.sendMessageDelayed(retryMsg, TIMEOUT_SAVE_MS); |
| 519 | } else { |
| 520 | Log.w(TAG,"Maximum name/address remote retrieval retry exceeded"); |
| 521 | sendBluetoothServiceDownCallback(); |
| 522 | unbindAndFinish(); |
| 523 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 527 | break; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 528 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 529 | case MESSAGE_ENABLE: |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 530 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 531 | Log.d(TAG, "MESSAGE_ENABLE: mBluetooth = " + mBluetooth); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 532 | } |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 533 | |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 534 | handleEnable(msg.arg1 == 1, msg.arg2 ==1); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 535 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 536 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 537 | case MESSAGE_DISABLE: |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 538 | handleDisable(msg.arg1 == 1); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 539 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 540 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 541 | case MESSAGE_REGISTER_ADAPTER: |
| 542 | { |
| 543 | IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 544 | boolean added = mCallbacks.register(callback); |
| 545 | Log.d(TAG,"Added callback: " + (callback == null? "null": callback) +":" +added ); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 546 | } |
| 547 | break; |
| 548 | case MESSAGE_UNREGISTER_ADAPTER: |
| 549 | { |
| 550 | IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 551 | boolean removed = mCallbacks.unregister(callback); |
| 552 | Log.d(TAG,"Removed callback: " + (callback == null? "null": callback) +":" + removed); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 553 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 554 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 555 | case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK: |
| 556 | { |
| 557 | IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 558 | mStateChangeCallbacks.register(callback); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 559 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 560 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 561 | case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK: |
| 562 | { |
| 563 | IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 564 | mStateChangeCallbacks.unregister(callback); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 565 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 566 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 567 | case MESSAGE_BLUETOOTH_SERVICE_CONNECTED: |
| 568 | { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 569 | if (DBG) Log.d(TAG,"MESSAGE_BLUETOOTH_SERVICE_CONNECTED"); |
| 570 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 571 | //Remove timeout |
| 572 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
| 573 | |
| 574 | IBinder service = (IBinder) msg.obj; |
| 575 | synchronized(mConnection) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 576 | mBinding = false; |
| 577 | mBluetooth = IBluetooth.Stub.asInterface(service); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 578 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 579 | if (mConnection.isGetNameAddressOnly()) { |
| 580 | //Request GET NAME AND ADDRESS |
| 581 | Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS); |
| 582 | mHandler.sendMessage(getMsg); |
| 583 | return; |
| 584 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 585 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 586 | //Register callback object |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 587 | try { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 588 | mBluetooth.registerCallback(mBluetoothCallback); |
| 589 | } catch (RemoteException re) { |
| 590 | Log.e(TAG, "Unable to register BluetoothCallback",re); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 591 | } |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 592 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 593 | //Inform BluetoothAdapter instances that service is up |
| 594 | int n = mCallbacks.beginBroadcast(); |
| 595 | Log.d(TAG,"Broadcasting onBluetoothServiceUp() to " + n + " receivers."); |
| 596 | for (int i=0; i <n;i++) { |
| 597 | try { |
| 598 | mCallbacks.getBroadcastItem(i).onBluetoothServiceUp(mBluetooth); |
| 599 | } catch (RemoteException e) { |
| 600 | Log.e(TAG, "Unable to call onBluetoothServiceUp() on callback #" + i, e); |
| 601 | } |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 602 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 603 | mCallbacks.finishBroadcast(); |
| 604 | |
| 605 | //Do enable request |
| 606 | try { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 607 | if (mQuietEnable == false) { |
| 608 | if(!mBluetooth.enable()) { |
| 609 | Log.e(TAG,"IBluetooth.enable() returned false"); |
| 610 | } |
| 611 | } |
| 612 | else |
| 613 | { |
| 614 | if(!mBluetooth.enableNoAutoConnect()) { |
| 615 | Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false"); |
| 616 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 617 | } |
| 618 | } catch (RemoteException e) { |
| 619 | Log.e(TAG,"Unable to call enable()",e); |
| 620 | } |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 621 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 622 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 623 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 624 | case MESSAGE_TIMEOUT_BIND: { |
| 625 | Log.e(TAG, "MESSAGE_TIMEOUT_BIND"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 626 | synchronized(mConnection) { |
| 627 | mBinding = false; |
| 628 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 629 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 630 | } |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 631 | case MESSAGE_BLUETOOTH_STATE_CHANGE: |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 632 | { |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 633 | int prevState = msg.arg1; |
| 634 | int newState = msg.arg2; |
| 635 | if (DBG) Log.d(TAG, "MESSAGE_BLUETOOTH_STATE_CHANGE: prevState = " + prevState + ", newState=" + newState); |
| 636 | if (prevState != newState) { |
| 637 | //Notify all proxy objects first of adapter state change |
| 638 | if (newState == BluetoothAdapter.STATE_ON || newState == BluetoothAdapter.STATE_OFF) { |
| 639 | boolean isUp = (newState==BluetoothAdapter.STATE_ON); |
| 640 | sendBluetoothStateCallback(isUp); |
| 641 | |
| 642 | //If Bluetooth is off, send service down event to proxy objects, and unbind |
| 643 | if (!isUp) { |
| 644 | sendBluetoothServiceDownCallback(); |
| 645 | unbindAndFinish(); |
| 646 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 647 | } |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 648 | |
| 649 | //Send broadcast message to everyone else |
| 650 | Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED); |
| 651 | intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState); |
| 652 | intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState); |
| 653 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 654 | if (DBG) Log.d(TAG,"Bluetooth State Change Intent: " + prevState + " -> " + newState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 655 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL, |
| 656 | BLUETOOTH_PERM); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 657 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 658 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 659 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 660 | case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: |
| 661 | { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 662 | if (DBG) Log.d(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED"); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 663 | sendBluetoothServiceDownCallback(); |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 664 | |
| 665 | // Send BT state broadcast to update |
| 666 | // the BT icon correctly |
| 667 | Message stateChangeMsg = mHandler.obtainMessage( |
| 668 | MESSAGE_BLUETOOTH_STATE_CHANGE); |
| 669 | stateChangeMsg.arg1 = BluetoothAdapter.STATE_ON; |
| 670 | stateChangeMsg.arg2 = |
| 671 | BluetoothAdapter.STATE_TURNING_OFF; |
| 672 | mHandler.sendMessage(stateChangeMsg); |
| 673 | synchronized(mConnection) { |
| 674 | mBluetooth = null; |
| 675 | } |
| 676 | // Send a Bluetooth Restart message |
| 677 | Message restartMsg = mHandler.obtainMessage( |
| 678 | MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 679 | mHandler.sendMessageDelayed(restartMsg, |
| 680 | SERVICE_RESTART_TIME_MS); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 681 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 682 | } |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 683 | case MESSAGE_RESTART_BLUETOOTH_SERVICE: |
| 684 | { |
| 685 | Log.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE:" |
| 686 | +" Restart IBluetooth service"); |
| 687 | /* Enable without persisting the setting as |
| 688 | it doesnt change when IBluetooth |
| 689 | service restarts */ |
| 690 | handleEnable(false, mQuietEnable); |
| 691 | break; |
| 692 | } |
| 693 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 694 | case MESSAGE_TIMEOUT_UNBIND: |
| 695 | { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 696 | Log.e(TAG, "MESSAGE_TIMEOUT_UNBIND"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 697 | synchronized(mConnection) { |
| 698 | mUnbinding = false; |
| 699 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 700 | break; |
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 | } |
| 703 | } |
| 704 | }; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 705 | |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 706 | private void handleEnable(boolean persist, boolean quietMode) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 707 | if (persist) { |
| 708 | persistBluetoothSetting(true); |
| 709 | } |
| 710 | |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 711 | mQuietEnable = quietMode; |
| 712 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 713 | synchronized(mConnection) { |
| 714 | if (mBluetooth == null) { |
| 715 | //Start bind timeout and bind |
| 716 | Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND); |
| 717 | mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS); |
| 718 | mConnection.setGetNameAddressOnly(false); |
| 719 | Intent i = new Intent(IBluetooth.class.getName()); |
Matthew Xie | fca9d63 | 2012-10-04 12:25:28 -0700 | [diff] [blame^] | 720 | if (!mContext.bindService(i, mConnection, Context.BIND_AUTO_CREATE, |
| 721 | UserHandle.USER_CURRENT)) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 722 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
| 723 | Log.e(TAG, "Fail to bind to: " + IBluetooth.class.getName()); |
| 724 | } |
| 725 | } else { |
| 726 | //Check if name and address is loaded if not get it first. |
| 727 | if (!isNameAndAddressSet()) { |
| 728 | try { |
| 729 | if (DBG) Log.d(TAG,"Getting and storing Bluetooth name and address prior to enable."); |
| 730 | storeNameAndAddress(mBluetooth.getName(),mBluetooth.getAddress()); |
| 731 | } catch (RemoteException e) {Log.e(TAG, "", e);}; |
| 732 | } |
| 733 | |
| 734 | //Enable bluetooth |
| 735 | try { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 736 | if (!mQuietEnable) { |
| 737 | if(!mBluetooth.enable()) { |
| 738 | Log.e(TAG,"IBluetooth.enable() returned false"); |
| 739 | } |
| 740 | } |
| 741 | else { |
| 742 | if(!mBluetooth.enableNoAutoConnect()) { |
| 743 | Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false"); |
| 744 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 745 | } |
| 746 | } catch (RemoteException e) { |
| 747 | Log.e(TAG,"Unable to call enable()",e); |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | private void handleDisable(boolean persist) { |
| 754 | synchronized(mConnection) { |
| 755 | if (mBluetooth != null ) { |
| 756 | if (persist) { |
| 757 | persistBluetoothSetting(false); |
| 758 | } |
| 759 | mConnection.setGetNameAddressOnly(false); |
| 760 | if (DBG) Log.d(TAG,"Sending off request."); |
| 761 | |
| 762 | try { |
| 763 | if(!mBluetooth.disable()) { |
| 764 | Log.e(TAG,"IBluetooth.disable() returned false"); |
| 765 | } |
| 766 | } catch (RemoteException e) { |
| 767 | Log.e(TAG,"Unable to call disable()",e); |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 772 | } |