Skip to content

Commit ba4dd93

Browse files
Martijn CoenenAndroid (Google) Code Review
authored andcommitted
Merge "Allow enabling Bluetooth without auto-connecting."
2 parents f03f64d + 6c614b7 commit ba4dd93

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

core/java/android/bluetooth/BluetoothAdapter.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,18 @@ public void closeProfileProxy(int profile, BluetoothProfile proxy) {
12311231
}
12321232
}
12331233

1234+
/**
1235+
* Enable the Bluetooth Adapter, but don't auto-connect devices
1236+
* and don't persist state. Only for use by system applications.
1237+
* @hide
1238+
*/
1239+
public boolean enableNoAutoConnect() {
1240+
try {
1241+
return mService.enableNoAutoConnect();
1242+
} catch (RemoteException e) {Log.e(TAG, "", e);}
1243+
return false;
1244+
}
1245+
12341246
/**
12351247
* Enable control of the Bluetooth Adapter for a single application.
12361248
*

core/java/android/bluetooth/IBluetooth.aidl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ interface IBluetooth
3434
boolean isEnabled();
3535
int getBluetoothState();
3636
boolean enable();
37+
boolean enableNoAutoConnect();
3738
boolean disable(boolean persistSetting);
3839

3940
String getAddress();

core/java/android/server/BluetoothService.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ private static class ServiceRecordClient {
165165
private static String mDockAddress;
166166
private String mDockPin;
167167

168+
private boolean mAllowConnect = true;
169+
168170
private int mAdapterConnectionState = BluetoothAdapter.STATE_DISCONNECTED;
169171
private BluetoothPanProfileHandler mBluetoothPanProfileHandler;
170172
private BluetoothInputProfileHandler mBluetoothInputProfileHandler;
@@ -472,28 +474,48 @@ synchronized void cleanNativeAfterShutoffBluetooth() {
472474

473475
/** Bring up BT and persist BT on in settings */
474476
public boolean enable() {
475-
return enable(true);
477+
return enable(true, true);
476478
}
477479

478480
/**
479481
* Enable this Bluetooth device, asynchronously.
480482
* This turns on/off the underlying hardware.
481483
*
482484
* @param saveSetting If true, persist the new state of BT in settings
485+
* @param allowConnect If true, auto-connects device when BT is turned on
486+
* and allows incoming A2DP/HSP connections
483487
* @return True on success (so far)
484488
*/
485-
public synchronized boolean enable(boolean saveSetting) {
489+
public synchronized boolean enable(boolean saveSetting, boolean allowConnect) {
486490
mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
487491
"Need BLUETOOTH_ADMIN permission");
488492

489493
// Airplane mode can prevent Bluetooth radio from being turned on.
490494
if (mIsAirplaneSensitive && isAirplaneModeOn() && !mIsAirplaneToggleable) {
491495
return false;
492496
}
497+
mAllowConnect = allowConnect;
493498
mBluetoothState.sendMessage(BluetoothAdapterStateMachine.USER_TURN_ON, saveSetting);
494499
return true;
495500
}
496501

502+
/**
503+
* Enable this Bluetooth device, asynchronously, but does not
504+
* auto-connect devices. In this state the Bluetooth adapter
505+
* also does not allow incoming A2DP/HSP connections (that
506+
* must go through this service), but does allow communication
507+
* on RFCOMM sockets implemented outside of this service (ie BTOPP).
508+
* This method is used to temporarily enable Bluetooth
509+
* for data transfer, without changing
510+
*
511+
* This turns on/off the underlying hardware.
512+
*
513+
* @return True on success (so far)
514+
*/
515+
public boolean enableNoAutoConnect() {
516+
return enable(false, false);
517+
}
518+
497519
/**
498520
* Turn on Bluetooth Module, Load firmware, and do all the preparation
499521
* needed to get the Bluetooth Module ready but keep it not discoverable
@@ -2441,6 +2463,13 @@ private void initProfileState() {
24412463
}
24422464

24432465
private void autoConnect() {
2466+
synchronized (this) {
2467+
if (!mAllowConnect) {
2468+
Log.d(TAG, "Not auto-connecting devices because of temporary BT on state.");
2469+
return;
2470+
}
2471+
}
2472+
24442473
String[] bonds = getKnownDevices();
24452474
if (bonds == null) {
24462475
return;
@@ -2457,6 +2486,12 @@ private void autoConnect() {
24572486
}
24582487

24592488
public boolean notifyIncomingConnection(String address, boolean rejected) {
2489+
synchronized (this) {
2490+
if (!mAllowConnect) {
2491+
Log.d(TAG, "Not allowing incoming connection because of temporary BT on state.");
2492+
return false;
2493+
}
2494+
}
24602495
BluetoothDeviceProfileState state = mDeviceProfileState.get(address);
24612496
if (state != null) {
24622497
Message msg = new Message();
@@ -2478,6 +2513,13 @@ public boolean notifyIncomingConnection(String address, boolean rejected) {
24782513
}
24792514

24802515
/*package*/ boolean notifyIncomingA2dpConnection(String address, boolean rejected) {
2516+
synchronized (this) {
2517+
if (!mAllowConnect) {
2518+
Log.d(TAG, "Not allowing a2dp connection because of temporary BT on state.");
2519+
return false;
2520+
}
2521+
}
2522+
24812523
BluetoothDeviceProfileState state = mDeviceProfileState.get(address);
24822524
if (state != null) {
24832525
Message msg = new Message();

0 commit comments

Comments
 (0)