blob: aa5ae3dd487f577c640d7ad4f76a7f606fe71b25 [file] [log] [blame]
fredc0f420372012-04-12 00:02:00 -07001/*
2 * Copyright (C) 2012 Google Inc.
3 */
4
5package com.android.server;
6
7import android.bluetooth.BluetoothAdapter;
8import android.bluetooth.IBluetooth;
fredcbf072a72012-05-09 16:52:50 -07009import android.bluetooth.IBluetoothCallback;
fredc0f420372012-04-12 00:02:00 -070010import android.bluetooth.IBluetoothManager;
11import android.bluetooth.IBluetoothManagerCallback;
12import android.bluetooth.IBluetoothStateChangeCallback;
fredc0f420372012-04-12 00:02:00 -070013import android.content.BroadcastReceiver;
14import android.content.ComponentName;
15import android.content.ContentResolver;
16import android.content.Context;
17import android.content.Intent;
18import android.content.IntentFilter;
19import android.content.ServiceConnection;
20import android.os.Handler;
fredc0f420372012-04-12 00:02:00 -070021import android.os.IBinder;
22import android.os.Message;
fredcd6883532012-04-25 17:46:13 -070023import android.os.RemoteCallbackList;
fredc0f420372012-04-12 00:02:00 -070024import android.os.RemoteException;
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -070025import android.os.Binder;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070026import android.os.UserHandle;
fredc0f420372012-04-12 00:02:00 -070027import android.provider.Settings;
28import android.util.Log;
29import java.util.List;
30import java.util.ArrayList;
fredc0f420372012-04-12 00:02:00 -070031class BluetoothManagerService extends IBluetoothManager.Stub {
32 private static final String TAG = "BluetoothManagerService";
33 private static final boolean DBG = true;
34
fredc0f420372012-04-12 00:02:00 -070035 private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN;
36 private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
fredc0f420372012-04-12 00:02:00 -070037 private static final String ACTION_SERVICE_STATE_CHANGED="com.android.bluetooth.btservice.action.STATE_CHANGED";
38 private static final String EXTRA_ACTION="action";
fredc0f420372012-04-12 00:02:00 -070039 private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address";
40 private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name";
fredc0f420372012-04-12 00:02:00 -070041 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 M1223e5a2012-08-29 18:07:26 +053043 //Maximum msec to wait for service restart
44 private static final int SERVICE_RESTART_TIME_MS = 200;
fredc0f420372012-04-12 00:02:00 -070045
46 private static final int MESSAGE_ENABLE = 1;
47 private static final int MESSAGE_DISABLE = 2;
fredc649fe492012-04-19 01:07:18 -070048 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 M1223e5a2012-08-29 18:07:26 +053054 private static final int MESSAGE_RESTART_BLUETOOTH_SERVICE = 42;
fredcbf072a72012-05-09 16:52:50 -070055 private static final int MESSAGE_BLUETOOTH_STATE_CHANGE=60;
fredc0f420372012-04-12 00:02:00 -070056 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 Xiecdce0b92012-07-12 19:06:15 -070063
64 // Locks are not provided for mName and mAddress.
65 // They are accessed in handler or broadcast receiver, same thread context.
fredc0f420372012-04-12 00:02:00 -070066 private String mAddress;
67 private String mName;
Matthew Xie6fde3092012-07-11 17:10:07 -070068 private final ContentResolver mContentResolver;
69 private final RemoteCallbackList<IBluetoothManagerCallback> mCallbacks;
70 private final RemoteCallbackList<IBluetoothStateChangeCallback> mStateChangeCallbacks;
fredc649fe492012-04-19 01:07:18 -070071 private IBluetooth mBluetooth;
72 private boolean mBinding;
73 private boolean mUnbinding;
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -070074 private boolean mQuietEnable = false;
fredc0f420372012-04-12 00:02:00 -070075
fredc649fe492012-04-19 01:07:18 -070076 private void registerForAirplaneMode(IntentFilter filter) {
77 final ContentResolver resolver = mContext.getContentResolver();
Christopher Tatec09cdce2012-09-10 16:50:14 -070078 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);
fredc649fe492012-04-19 01:07:18 -070082 boolean mIsAirplaneSensitive = airplaneModeRadios == null ? true :
Christopher Tatec09cdce2012-09-10 16:50:14 -070083 airplaneModeRadios.contains(Settings.Global.RADIO_BLUETOOTH);
fredc649fe492012-04-19 01:07:18 -070084 if (mIsAirplaneSensitive) {
85 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
86 }
87 }
88
fredcbf072a72012-05-09 16:52:50 -070089 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() {
fredc0f420372012-04-12 00:02:00 -070098 @Override
99 public void onReceive(Context context, Intent intent) {
100 String action = intent.getAction();
fredcbf072a72012-05-09 16:52:50 -0700101 if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action)) {
fredc0f420372012-04-12 00:02:00 -0700102 String newName = intent.getStringExtra(BluetoothAdapter.EXTRA_LOCAL_NAME);
Freda8c6df02012-07-11 10:25:23 -0700103 if (DBG) Log.d(TAG, "Bluetooth Adapter name changed to " + newName);
fredc0f420372012-04-12 00:02:00 -0700104 if (newName != null) {
105 storeNameAndAddress(newName, null);
106 }
fredc649fe492012-04-19 01:07:18 -0700107 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
108 if (isAirplaneModeOn()) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700109 // disable without persisting the setting
110 handleDisable(false);
fredc649fe492012-04-19 01:07:18 -0700111 } else {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700112 if (isBluetoothPersistedStateOn()) {
113 // enable without persisting the setting
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700114 handleEnable(false, false);
Matthew Xiecdce0b92012-07-12 19:06:15 -0700115 }
fredc649fe492012-04-19 01:07:18 -0700116 }
fredc0f420372012-04-12 00:02:00 -0700117 }
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();
fredcd6883532012-04-25 17:46:13 -0700129 mCallbacks = new RemoteCallbackList<IBluetoothManagerCallback>();
130 mStateChangeCallbacks = new RemoteCallbackList<IBluetoothStateChangeCallback>();
Matthew Xie6fde3092012-07-11 17:10:07 -0700131 IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
132 filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
133 registerForAirplaneMode(filter);
134 mContext.registerReceiver(mReceiver, filter);
fredc649fe492012-04-19 01:07:18 -0700135 boolean airplaneModeOn = isAirplaneModeOn();
136 boolean bluetoothOn = isBluetoothPersistedStateOn();
fredc0f420372012-04-12 00:02:00 -0700137 loadStoredNameAndAddress();
fredc649fe492012-04-19 01:07:18 -0700138 if (DBG) Log.d(TAG, "airplaneModeOn: " + airplaneModeOn + " bluetoothOn: " + bluetoothOn);
Andre Eisenbacha732ffd2012-05-02 00:39:24 -0700139 if (bluetoothOn) {
fredc0f420372012-04-12 00:02:00 -0700140 //Enable
fredc649fe492012-04-19 01:07:18 -0700141 if (DBG) Log.d(TAG, "Auto-enabling Bluetooth.");
fredc0f420372012-04-12 00:02:00 -0700142 enable();
Freda8c6df02012-07-11 10:25:23 -0700143 } else if (!isNameAndAddressSet()) {
fredc649fe492012-04-19 01:07:18 -0700144 //Sync the Bluetooth name and address from the Bluetooth Adapter
145 if (DBG) Log.d(TAG,"Retrieving Bluetooth Adapter name and address...");
fredc0f420372012-04-12 00:02:00 -0700146 getNameAndAddress();
147 }
148 }
149
fredc649fe492012-04-19 01:07:18 -0700150 /**
151 * Returns true if airplane mode is currently on
152 */
153 private final boolean isAirplaneModeOn() {
Christopher Tatec09cdce2012-09-10 16:50:14 -0700154 return Settings.Global.getInt(mContext.getContentResolver(),
155 Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
fredc649fe492012-04-19 01:07:18 -0700156 }
157
158 /**
159 * Returns true if the Bluetooth saved state is "on"
160 */
161 private final boolean isBluetoothPersistedStateOn() {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700162 return Settings.Global.getInt(mContentResolver,
163 Settings.Global.BLUETOOTH_ON, 0) ==1;
fredc649fe492012-04-19 01:07:18 -0700164 }
165
166 /**
167 * Save the Bluetooth on/off state
168 *
169 */
170 private void persistBluetoothSetting(boolean setOn) {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700171 Settings.Global.putInt(mContext.getContentResolver(),
172 Settings.Global.BLUETOOTH_ON,
fredc649fe492012-04-19 01:07:18 -0700173 setOn ? 1 : 0);
174 }
175
176 /**
177 * Returns true if the Bluetooth Adapter's name and address is
178 * locally cached
179 * @return
180 */
fredc0f420372012-04-12 00:02:00 -0700181 private boolean isNameAndAddressSet() {
182 return mName !=null && mAddress!= null && mName.length()>0 && mAddress.length()>0;
183 }
184
fredc649fe492012-04-19 01:07:18 -0700185 /**
186 * Retrieve the Bluetooth Adapter's name and address and save it in
187 * in the local cache
188 */
fredc0f420372012-04-12 00:02:00 -0700189 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
fredc649fe492012-04-19 01:07:18 -0700198 /**
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 */
fredc0f420372012-04-12 00:02:00 -0700204 private void storeNameAndAddress(String name, String address) {
205 if (name != null) {
206 Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME, name);
fredc0f420372012-04-12 00:02:00 -0700207 mName = name;
fredc649fe492012-04-19 01:07:18 -0700208 if (DBG) Log.d(TAG,"Stored Bluetooth name: " +
209 Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_NAME));
fredc0f420372012-04-12 00:02:00 -0700210 }
211
212 if (address != null) {
213 Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS, address);
fredc0f420372012-04-12 00:02:00 -0700214 mAddress=address;
fredc649fe492012-04-19 01:07:18 -0700215 if (DBG) Log.d(TAG,"Stored Bluetoothaddress: " +
216 Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_ADDRESS));
fredc0f420372012-04-12 00:02:00 -0700217 }
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() {
fredcf2458862012-04-16 15:18:27 -0700267 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700268 Log.d(TAG,"getNameAndAddress(): mBluetooth = " + mBluetooth +
269 " mBinding = " + mBinding);
fredcf2458862012-04-16 15:18:27 -0700270 }
fredc0f420372012-04-12 00:02:00 -0700271 synchronized(mConnection) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700272 if (mBinding) return;
273 if (mConnection == null) mBinding = true;
fredc0f420372012-04-12 00:02:00 -0700274 }
275 Message msg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
276 mHandler.sendMessage(msg);
277 }
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700278 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 }
fredc0f420372012-04-12 00:02:00 -0700296
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700297 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 }
fredc0f420372012-04-12 00:02:00 -0700304 public boolean enable() {
305 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
306 "Need BLUETOOTH ADMIN permission");
fredcf2458862012-04-16 15:18:27 -0700307 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700308 Log.d(TAG,"enable(): mBluetooth =" + mBluetooth +
309 " mBinding = " + mBinding);
fredcf2458862012-04-16 15:18:27 -0700310 }
311
fredc0f420372012-04-12 00:02:00 -0700312 synchronized(mConnection) {
fredc649fe492012-04-19 01:07:18 -0700313 if (mBinding) {
314 Log.w(TAG,"enable(): binding in progress. Returning..");
315 return true;
316 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700317 if (mConnection == null) mBinding = true;
fredc0f420372012-04-12 00:02:00 -0700318 }
fredc116d1d462012-04-20 14:47:08 -0700319
fredc0f420372012-04-12 00:02:00 -0700320 Message msg = mHandler.obtainMessage(MESSAGE_ENABLE);
fredc649fe492012-04-19 01:07:18 -0700321 msg.arg1=1; //persist
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700322 msg.arg2=0; //No Quiet Mode
fredc0f420372012-04-12 00:02:00 -0700323 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");
fredcf2458862012-04-16 15:18:27 -0700330 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700331 Log.d(TAG,"disable(): mBluetooth = " + mBluetooth +
332 " mBinding = " + mBinding);
333 }
fredcf2458862012-04-16 15:18:27 -0700334
fredc0f420372012-04-12 00:02:00 -0700335 synchronized(mConnection) {
336 if (mBluetooth == null) return false;
fredc0f420372012-04-12 00:02:00 -0700337 }
338 Message msg = mHandler.obtainMessage(MESSAGE_DISABLE);
fredc649fe492012-04-19 01:07:18 -0700339 msg.arg1=(persist?1:0);
fredc0f420372012-04-12 00:02:00 -0700340 mHandler.sendMessage(msg);
341 return true;
342 }
343
fredc649fe492012-04-19 01:07:18 -0700344 public void unbindAndFinish() {
fredcf2458862012-04-16 15:18:27 -0700345 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700346 Log.d(TAG,"unbindAndFinish(): " + mBluetooth +
347 " mBinding = " + mBinding);
fredcf2458862012-04-16 15:18:27 -0700348 }
349
fredc0f420372012-04-12 00:02:00 -0700350 synchronized (mConnection) {
351 if (mUnbinding) return;
352 mUnbinding = true;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700353 if (mConnection != null) {
fredcbf072a72012-05-09 16:52:50 -0700354 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 }
fredc0f420372012-04-12 00:02:00 -0700362 if (DBG) Log.d(TAG, "Sending unbind request.");
fredcd6883532012-04-25 17:46:13 -0700363 mBluetooth = null;
364 //Unbind
fredc0f420372012-04-12 00:02:00 -0700365 mContext.unbindService(mConnection);
fredcd6883532012-04-25 17:46:13 -0700366 mUnbinding = false;
fredcf2458862012-04-16 15:18:27 -0700367 } else {
368 mUnbinding=false;
fredc0f420372012-04-12 00:02:00 -0700369 }
370 }
371 }
372
fredcbf072a72012-05-09 16:52:50 -0700373 private void sendBluetoothStateCallback(boolean isUp) {
374 int n = mStateChangeCallbacks.beginBroadcast();
Freda8c6df02012-07-11 10:25:23 -0700375 if (DBG) Log.d(TAG,"Broadcasting onBluetoothStateChange("+isUp+") to " + n + " receivers.");
fredcbf072a72012-05-09 16:52:50 -0700376 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() {
fredcd6883532012-04-25 17:46:13 -0700390 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 }
fredc0f420372012-04-12 00:02:00 -0700404 public String getAddress() {
405 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
406 "Need BLUETOOTH ADMIN permission");
fredc116d1d462012-04-20 14:47:08 -0700407 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 Xiecdce0b92012-07-12 19:06:15 -0700416 // mAddress is accessed from outside.
417 // It is alright without a lock. Here, bluetooth is off, no other thread is
418 // changing mAddress
fredc0f420372012-04-12 00:02:00 -0700419 return mAddress;
420 }
fredc649fe492012-04-19 01:07:18 -0700421
fredc0f420372012-04-12 00:02:00 -0700422 public String getName() {
423 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
424 "Need BLUETOOTH ADMIN permission");
fredc116d1d462012-04-20 14:47:08 -0700425 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 Xiecdce0b92012-07-12 19:06:15 -0700434 // mName is accessed from outside.
435 // It alright without a lock. Here, bluetooth is off, no other thread is
436 // changing mName
fredc0f420372012-04-12 00:02:00 -0700437 return mName;
438 }
439
fredc0f420372012-04-12 00:02:00 -0700440 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) {
fredc649fe492012-04-19 01:07:18 -0700453 if (DBG) Log.d(TAG, "BluetoothServiceConnection: connected to AdapterService");
fredc0f420372012-04-12 00:02:00 -0700454 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED);
455 msg.obj = service;
456 mHandler.sendMessage(msg);
457 }
458
459 public void onServiceDisconnected(ComponentName className) {
fredc0f420372012-04-12 00:02:00 -0700460 // Called if we unexpected disconnected.
fredc649fe492012-04-19 01:07:18 -0700461 if (DBG) Log.d(TAG, "BluetoothServiceConnection: disconnected from AdapterService");
fredc0f420372012-04-12 00:02:00 -0700462 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);
fredc0f420372012-04-12 00:02:00 -0700473 switch (msg.what) {
474 case MESSAGE_GET_NAME_AND_ADDRESS: {
fredc649fe492012-04-19 01:07:18 -0700475 if (DBG) Log.d(TAG,"MESSAGE_GET_NAME_AND_ADDRESS");
Matthew Xiecdce0b92012-07-12 19:06:15 -0700476 synchronized(mConnection) {
fredc0f420372012-04-12 00:02:00 -0700477 //Start bind request
Matthew Xiecdce0b92012-07-12 19:06:15 -0700478 if (mBluetooth == null) {
fredc0f420372012-04-12 00:02:00 -0700479 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 Xiecdce0b92012-07-12 19:06:15 -0700491 else {
492 Message saveMsg= mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS);
493 mHandler.sendMessage(saveMsg);
494 }
fredc0f420372012-04-12 00:02:00 -0700495 }
fredc649fe492012-04-19 01:07:18 -0700496 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700497 }
fredc0f420372012-04-12 00:02:00 -0700498 case MESSAGE_SAVE_NAME_AND_ADDRESS: {
fredc649fe492012-04-19 01:07:18 -0700499 if (DBG) Log.d(TAG,"MESSAGE_SAVE_NAME_AND_ADDRESS");
Matthew Xiecdce0b92012-07-12 19:06:15 -0700500 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 }
fredc0f420372012-04-12 00:02:00 -0700510
Matthew Xiecdce0b92012-07-12 19:06:15 -0700511 if (name != null && address != null) {
512 storeNameAndAddress(name,address);
fredcbf072a72012-05-09 16:52:50 -0700513 sendBluetoothServiceDownCallback();
fredc649fe492012-04-19 01:07:18 -0700514 unbindAndFinish();
Matthew Xiecdce0b92012-07-12 19:06:15 -0700515 } 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 }
fredc0f420372012-04-12 00:02:00 -0700526 }
527 }
528 }
fredc649fe492012-04-19 01:07:18 -0700529 break;
fredc649fe492012-04-19 01:07:18 -0700530 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700531 case MESSAGE_ENABLE:
fredcf2458862012-04-16 15:18:27 -0700532 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700533 Log.d(TAG, "MESSAGE_ENABLE: mBluetooth = " + mBluetooth);
fredc649fe492012-04-19 01:07:18 -0700534 }
Freda8c6df02012-07-11 10:25:23 -0700535
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700536 handleEnable(msg.arg1 == 1, msg.arg2 ==1);
fredc649fe492012-04-19 01:07:18 -0700537 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700538
fredc0f420372012-04-12 00:02:00 -0700539 case MESSAGE_DISABLE:
Matthew Xiecdce0b92012-07-12 19:06:15 -0700540 handleDisable(msg.arg1 == 1);
fredc0f420372012-04-12 00:02:00 -0700541 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700542
fredc0f420372012-04-12 00:02:00 -0700543 case MESSAGE_REGISTER_ADAPTER:
544 {
545 IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -0700546 boolean added = mCallbacks.register(callback);
547 Log.d(TAG,"Added callback: " + (callback == null? "null": callback) +":" +added );
fredc0f420372012-04-12 00:02:00 -0700548 }
549 break;
550 case MESSAGE_UNREGISTER_ADAPTER:
551 {
552 IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -0700553 boolean removed = mCallbacks.unregister(callback);
554 Log.d(TAG,"Removed callback: " + (callback == null? "null": callback) +":" + removed);
fredc0f420372012-04-12 00:02:00 -0700555 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700556 }
fredc0f420372012-04-12 00:02:00 -0700557 case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK:
558 {
559 IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -0700560 mStateChangeCallbacks.register(callback);
fredc0f420372012-04-12 00:02:00 -0700561 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700562 }
fredc0f420372012-04-12 00:02:00 -0700563 case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK:
564 {
565 IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -0700566 mStateChangeCallbacks.unregister(callback);
fredc0f420372012-04-12 00:02:00 -0700567 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700568 }
fredc0f420372012-04-12 00:02:00 -0700569 case MESSAGE_BLUETOOTH_SERVICE_CONNECTED:
570 {
fredc649fe492012-04-19 01:07:18 -0700571 if (DBG) Log.d(TAG,"MESSAGE_BLUETOOTH_SERVICE_CONNECTED");
572
fredc0f420372012-04-12 00:02:00 -0700573 //Remove timeout
574 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
575
576 IBinder service = (IBinder) msg.obj;
577 synchronized(mConnection) {
fredc0f420372012-04-12 00:02:00 -0700578 mBinding = false;
579 mBluetooth = IBluetooth.Stub.asInterface(service);
fredc0f420372012-04-12 00:02:00 -0700580
Matthew Xiecdce0b92012-07-12 19:06:15 -0700581 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 }
fredc0f420372012-04-12 00:02:00 -0700587
Matthew Xiecdce0b92012-07-12 19:06:15 -0700588 //Register callback object
fredcbf072a72012-05-09 16:52:50 -0700589 try {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700590 mBluetooth.registerCallback(mBluetoothCallback);
591 } catch (RemoteException re) {
592 Log.e(TAG, "Unable to register BluetoothCallback",re);
fredcbf072a72012-05-09 16:52:50 -0700593 }
fredcbf072a72012-05-09 16:52:50 -0700594
Matthew Xiecdce0b92012-07-12 19:06:15 -0700595 //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 }
Freda8c6df02012-07-11 10:25:23 -0700604 }
Matthew Xiecdce0b92012-07-12 19:06:15 -0700605 mCallbacks.finishBroadcast();
606
607 //Do enable request
608 try {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700609 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 Xiecdce0b92012-07-12 19:06:15 -0700619 }
620 } catch (RemoteException e) {
621 Log.e(TAG,"Unable to call enable()",e);
622 }
Freda8c6df02012-07-11 10:25:23 -0700623 }
fredc649fe492012-04-19 01:07:18 -0700624 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700625 }
fredc649fe492012-04-19 01:07:18 -0700626 case MESSAGE_TIMEOUT_BIND: {
627 Log.e(TAG, "MESSAGE_TIMEOUT_BIND");
fredc0f420372012-04-12 00:02:00 -0700628 synchronized(mConnection) {
629 mBinding = false;
630 }
fredc649fe492012-04-19 01:07:18 -0700631 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700632 }
fredcbf072a72012-05-09 16:52:50 -0700633 case MESSAGE_BLUETOOTH_STATE_CHANGE:
fredc0f420372012-04-12 00:02:00 -0700634 {
fredcbf072a72012-05-09 16:52:50 -0700635 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 }
fredc0f420372012-04-12 00:02:00 -0700649 }
fredcbf072a72012-05-09 16:52:50 -0700650
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 Hackborn5ac72a22012-08-29 18:32:08 -0700657 mContext.sendBroadcastAsUser(intent, UserHandle.ALL,
658 BLUETOOTH_PERM);
fredc0f420372012-04-12 00:02:00 -0700659 }
fredc649fe492012-04-19 01:07:18 -0700660 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700661 }
fredc0f420372012-04-12 00:02:00 -0700662 case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED:
663 {
fredc649fe492012-04-19 01:07:18 -0700664 if (DBG) Log.d(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED");
fredcbf072a72012-05-09 16:52:50 -0700665 sendBluetoothServiceDownCallback();
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +0530666
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);
fredc649fe492012-04-19 01:07:18 -0700683 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700684 }
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +0530685 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
fredc0f420372012-04-12 00:02:00 -0700696 case MESSAGE_TIMEOUT_UNBIND:
697 {
fredc649fe492012-04-19 01:07:18 -0700698 Log.e(TAG, "MESSAGE_TIMEOUT_UNBIND");
fredc0f420372012-04-12 00:02:00 -0700699 synchronized(mConnection) {
700 mUnbinding = false;
701 }
fredc649fe492012-04-19 01:07:18 -0700702 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700703 }
fredc0f420372012-04-12 00:02:00 -0700704 }
705 }
706 };
Matthew Xiecdce0b92012-07-12 19:06:15 -0700707
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700708 private void handleEnable(boolean persist, boolean quietMode) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700709 if (persist) {
710 persistBluetoothSetting(true);
711 }
712
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700713 mQuietEnable = quietMode;
714
Matthew Xiecdce0b92012-07-12 19:06:15 -0700715 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 Battafffa86b2012-08-08 15:35:49 -0700737 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 Xiecdce0b92012-07-12 19:06:15 -0700746 }
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 }
fredc0f420372012-04-12 00:02:00 -0700773}