Skip to content

Commit a8c6df0

Browse files
FredAndroid (Google) Code Review
authored andcommitted
Removed startService() call from BluetoothManagerService.
BT enable()/disable() are handled by using the binder interface Change-Id: I0bb8d4984129706e424320241ca3ea7e12caf0d3 Conflicts: core/java/android/bluetooth/IBluetooth.aidl
1 parent 9631314 commit a8c6df0

File tree

2 files changed

+41
-45
lines changed

2 files changed

+41
-45
lines changed

core/java/android/bluetooth/IBluetooth.aidl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ interface IBluetooth
3333
int getState();
3434
boolean enable();
3535
boolean enableNoAutoConnect();
36-
boolean disable(boolean persist);
36+
boolean disable();
3737

3838
String getAddress();
3939
ParcelUuid[] getUuids();

services/java/com/android/server/BluetoothManagerService.java

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
3030
private static final String TAG = "BluetoothManagerService";
3131
private static final boolean DBG = true;
3232

33-
private static final boolean ALWAYS_SYNC_NAME_ADDRESS=false; //true; //If true, always load name and address
3433
private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN;
3534
private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
3635
private static final String ACTION_SERVICE_STATE_CHANGED="com.android.bluetooth.btservice.action.STATE_CHANGED";
@@ -94,7 +93,7 @@ public void onReceive(Context context, Intent intent) {
9493
String action = intent.getAction();
9594
if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action)) {
9695
String newName = intent.getStringExtra(BluetoothAdapter.EXTRA_LOCAL_NAME);
97-
Log.d(TAG, "Bluetooth Adapter name changed to " + newName);
96+
if (DBG) Log.d(TAG, "Bluetooth Adapter name changed to " + newName);
9897
if (newName != null) {
9998
storeNameAndAddress(newName, null);
10099
}
@@ -134,7 +133,7 @@ public void onReceive(Context context, Intent intent) {
134133
//Enable
135134
if (DBG) Log.d(TAG, "Auto-enabling Bluetooth.");
136135
enable();
137-
} else if (ALWAYS_SYNC_NAME_ADDRESS || !isNameAndAddressSet()) {
136+
} else if (!isNameAndAddressSet()) {
138137
//Sync the Bluetooth name and address from the Bluetooth Adapter
139138
if (DBG) Log.d(TAG,"Retrieving Bluetooth Adapter name and address...");
140139
getNameAndAddress();
@@ -352,7 +351,7 @@ public void unbindAndFinish() {
352351

353352
private void sendBluetoothStateCallback(boolean isUp) {
354353
int n = mStateChangeCallbacks.beginBroadcast();
355-
Log.d(TAG,"Broadcasting onBluetoothStateChange("+isUp+") to " + n + " receivers.");
354+
if (DBG) Log.d(TAG,"Broadcasting onBluetoothStateChange("+isUp+") to " + n + " receivers.");
356355
for (int i=0; i <n;i++) {
357356
try {
358357
mStateChangeCallbacks.getBroadcastItem(i).onBluetoothStateChange(isUp);
@@ -482,10 +481,6 @@ public void handleMessage(Message msg) {
482481

483482
if (name != null && address != null) {
484483
storeNameAndAddress(name,address);
485-
Intent i = new Intent(IBluetooth.class.getName());
486-
i.putExtra(EXTRA_ACTION, ACTION_SERVICE_STATE_CHANGED);
487-
i.putExtra(BluetoothAdapter.EXTRA_STATE,BluetoothAdapter.STATE_OFF);
488-
mContext.startService(i);
489484
sendBluetoothServiceDownCallback();
490485
unbindAndFinish();
491486
} else {
@@ -517,41 +512,40 @@ public void handleMessage(Message msg) {
517512
Log.d(TAG, "MESSAGE_ENABLE: mBluetooth = " + mBluetooth +
518513
" isConnected = " + isConnected());
519514
}
520-
boolean persist = (1==msg.arg1);
515+
516+
boolean persist = (1==msg.arg1);
521517
if (persist) {
522518
persistBluetoothSetting(true);
523519
}
520+
524521
if (mBluetooth == null) {
525-
//Start bind request
526-
if (!isConnected()) {
527-
//Start bind timeout and bind
528-
Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
529-
mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS);
530-
/*
531-
Intent i = new Intent(IBluetooth.class.getName());
532-
i.putExtra(EXTRA_ACTION, ACTION_SERVICE_STATE_CHANGED);
533-
i.putExtra(BluetoothAdapter.EXTRA_STATE,BluetoothAdapter.STATE_ON);
534-
mContext.startService(i);
535-
*/
536-
mConnection.setGetNameAddressOnly(false);
537-
Intent i = new Intent(IBluetooth.class.getName());
538-
if (!mContext.bindService(i, mConnection,Context.BIND_AUTO_CREATE)) {
539-
mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
540-
Log.e(TAG, "Fail to bind to: " + IBluetooth.class.getName());
541-
}
522+
//Start bind timeout and bind
523+
Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
524+
mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS);
525+
mConnection.setGetNameAddressOnly(false);
526+
Intent i = new Intent(IBluetooth.class.getName());
527+
if (!mContext.bindService(i, mConnection,Context.BIND_AUTO_CREATE)) {
528+
mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
529+
Log.e(TAG, "Fail to bind to: " + IBluetooth.class.getName());
542530
}
543531
} else {
544532
//Check if name and address is loaded if not get it first.
545-
if (ALWAYS_SYNC_NAME_ADDRESS || !isNameAndAddressSet()) {
533+
if (!isNameAndAddressSet()) {
546534
try {
547535
if (DBG) Log.d(TAG,"Getting and storing Bluetooth name and address prior to enable.");
548536
storeNameAndAddress(mBluetooth.getName(),mBluetooth.getAddress());
549537
} catch (RemoteException e) {Log.e(TAG, "", e);};
550538
}
551-
Intent i = new Intent(IBluetooth.class.getName());
552-
i.putExtra(EXTRA_ACTION, ACTION_SERVICE_STATE_CHANGED);
553-
i.putExtra(BluetoothAdapter.EXTRA_STATE,BluetoothAdapter.STATE_ON);
554-
mContext.startService(i);
539+
540+
//Enable bluetooth
541+
try {
542+
if(!mBluetooth.enable()) {
543+
Log.e(TAG,"IBluetooth.enable() returned false");
544+
}
545+
} catch (RemoteException e) {
546+
Log.e(TAG,"Unable to call enable()",e);
547+
}
548+
555549
}
556550
// TODO(BT) what if service failed to start:
557551
// [fc] fixed: watch for bind timeout and handle accordingly
@@ -573,16 +567,15 @@ public void handleMessage(Message msg) {
573567
}
574568
mConnection.setGetNameAddressOnly(false);
575569
if (DBG) Log.d(TAG,"Sending off request.");
576-
Intent i = new Intent(IBluetooth.class.getName());
577-
i.putExtra(EXTRA_ACTION, ACTION_SERVICE_STATE_CHANGED);
578-
i.putExtra(BluetoothAdapter.EXTRA_STATE,BluetoothAdapter.STATE_OFF);
579-
mContext.startService(i);
580-
}
581-
// TODO(BT) what if service failed to stop:
582-
// [fc] fixed: watch for disable event and unbind accordingly
583-
// TODO(BT) persist the setting depending on argument
584-
// [fc]: let AdapterServiceHandle
585570

571+
try {
572+
if(!mBluetooth.disable()) {
573+
Log.e(TAG,"IBluetooth.disable() returned false");
574+
}
575+
} catch (RemoteException e) {
576+
Log.e(TAG,"Unable to call disable()",e);
577+
}
578+
}
586579
break;
587580
case MESSAGE_REGISTER_ADAPTER:
588581
{
@@ -650,10 +643,13 @@ public void handleMessage(Message msg) {
650643
mCallbacks.finishBroadcast();
651644

652645
//Do enable request
653-
Intent i = new Intent(IBluetooth.class.getName());
654-
i.putExtra(EXTRA_ACTION, ACTION_SERVICE_STATE_CHANGED);
655-
i.putExtra(BluetoothAdapter.EXTRA_STATE,BluetoothAdapter.STATE_ON);
656-
mContext.startService(i);
646+
try {
647+
if(!mBluetooth.enable()) {
648+
Log.e(TAG,"IBluetooth.enable() returned false");
649+
}
650+
} catch (RemoteException e) {
651+
Log.e(TAG,"Unable to call enable()",e);
652+
}
657653
}
658654
break;
659655
case MESSAGE_TIMEOUT_BIND: {

0 commit comments

Comments
 (0)