@@ -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