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