2222import android .os .Message ;
2323import android .os .RemoteCallbackList ;
2424import android .os .RemoteException ;
25+ import android .os .Binder ;
2526import android .provider .Settings ;
2627import android .util .Log ;
2728import java .util .List ;
@@ -66,6 +67,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
6667 private IBluetooth mBluetooth ;
6768 private boolean mBinding ;
6869 private boolean mUnbinding ;
70+ private boolean mQuietEnable = false ;
6971
7072 private void registerForAirplaneMode (IntentFilter filter ) {
7173 final ContentResolver resolver = mContext .getContentResolver ();
@@ -105,7 +107,7 @@ public void onReceive(Context context, Intent intent) {
105107 } else {
106108 if (isBluetoothPersistedStateOn ()) {
107109 // enable without persisting the setting
108- handleEnable (false );
110+ handleEnable (false , false );
109111 }
110112 }
111113 }
@@ -269,7 +271,32 @@ public void getNameAndAddress() {
269271 Message msg = mHandler .obtainMessage (MESSAGE_GET_NAME_AND_ADDRESS );
270272 mHandler .sendMessage (msg );
271273 }
274+ public boolean enableNoAutoConnect ()
275+ {
276+ mContext .enforceCallingOrSelfPermission (BLUETOOTH_ADMIN_PERM ,
277+ "Need BLUETOOTH ADMIN permission" );
278+ if (DBG ) {
279+ Log .d (TAG ,"enableNoAutoConnect(): mBluetooth =" + mBluetooth +
280+ " mBinding = " + mBinding );
281+ }
282+ if (Binder .getCallingUid () != android .os .Process .NFC_UID ) {
283+ throw new SecurityException ("no permission to enable Bluetooth quietly" );
284+ }
285+ synchronized (mConnection ) {
286+ if (mBinding ) {
287+ Log .w (TAG ,"enableNoAutoConnect(): binding in progress. Returning.." );
288+ return true ;
289+ }
290+ if (mConnection == null ) mBinding = true ;
291+ }
272292
293+ Message msg = mHandler .obtainMessage (MESSAGE_ENABLE );
294+ msg .arg1 =0 ; //No persist
295+ msg .arg2 =1 ; //Quiet mode
296+ mHandler .sendMessage (msg );
297+ return true ;
298+
299+ }
273300 public boolean enable () {
274301 mContext .enforceCallingOrSelfPermission (BLUETOOTH_ADMIN_PERM ,
275302 "Need BLUETOOTH ADMIN permission" );
@@ -288,6 +315,7 @@ public boolean enable() {
288315
289316 Message msg = mHandler .obtainMessage (MESSAGE_ENABLE );
290317 msg .arg1 =1 ; //persist
318+ msg .arg2 =0 ; //No Quiet Mode
291319 mHandler .sendMessage (msg );
292320 return true ;
293321 }
@@ -501,7 +529,7 @@ public void handleMessage(Message msg) {
501529 Log .d (TAG , "MESSAGE_ENABLE: mBluetooth = " + mBluetooth );
502530 }
503531
504- handleEnable (msg .arg1 == 1 );
532+ handleEnable (msg .arg1 == 1 , msg . arg2 == 1 );
505533 break ;
506534
507535 case MESSAGE_DISABLE :
@@ -574,14 +602,21 @@ public void handleMessage(Message msg) {
574602
575603 //Do enable request
576604 try {
577- if (!mBluetooth .enable ()) {
578- Log .e (TAG ,"IBluetooth.enable() returned false" );
605+ if (mQuietEnable == false ) {
606+ if (!mBluetooth .enable ()) {
607+ Log .e (TAG ,"IBluetooth.enable() returned false" );
608+ }
609+ }
610+ else
611+ {
612+ if (!mBluetooth .enableNoAutoConnect ()) {
613+ Log .e (TAG ,"IBluetooth.enableNoAutoConnect() returned false" );
614+ }
579615 }
580616 } catch (RemoteException e ) {
581617 Log .e (TAG ,"Unable to call enable()" ,e );
582618 }
583619 }
584-
585620 break ;
586621 }
587622 case MESSAGE_TIMEOUT_BIND : {
@@ -637,11 +672,13 @@ public void handleMessage(Message msg) {
637672 }
638673 };
639674
640- private void handleEnable (boolean persist ) {
675+ private void handleEnable (boolean persist , boolean quietMode ) {
641676 if (persist ) {
642677 persistBluetoothSetting (true );
643678 }
644679
680+ mQuietEnable = quietMode ;
681+
645682 synchronized (mConnection ) {
646683 if (mBluetooth == null ) {
647684 //Start bind timeout and bind
@@ -664,8 +701,15 @@ private void handleEnable(boolean persist) {
664701
665702 //Enable bluetooth
666703 try {
667- if (!mBluetooth .enable ()) {
668- Log .e (TAG ,"IBluetooth.enable() returned false" );
704+ if (!mQuietEnable ) {
705+ if (!mBluetooth .enable ()) {
706+ Log .e (TAG ,"IBluetooth.enable() returned false" );
707+ }
708+ }
709+ else {
710+ if (!mBluetooth .enableNoAutoConnect ()) {
711+ Log .e (TAG ,"IBluetooth.enableNoAutoConnect() returned false" );
712+ }
669713 }
670714 } catch (RemoteException e ) {
671715 Log .e (TAG ,"Unable to call enable()" ,e );
0 commit comments