Skip to content

Commit 58de142

Browse files
Matthew XieAndroid (Google) Code Review
authored andcommitted
Merge "Implement enableNoAutoconnect()" into jb-mr1-dev
2 parents caaeda9 + fffa86b commit 58de142

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

core/java/android/bluetooth/BluetoothAdapter.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,10 @@ public int getState() {
504504
* immediate error
505505
*/
506506
public boolean enable() {
507-
boolean enabled = false;
507+
if (isEnabled() == true){
508+
if (DBG) Log.d(TAG, "enable(): BT is already enabled..!");
509+
return true;
510+
}
508511
try {
509512
return mManagerService.enable();
510513
} catch (RemoteException e) {Log.e(TAG, "", e);}
@@ -1246,8 +1249,14 @@ public void onBluetoothServiceDown() {
12461249
* @hide
12471250
*/
12481251
public boolean enableNoAutoConnect() {
1249-
// TODO avoid auto-connect in the new stack.
1250-
return enable();
1252+
if (isEnabled() == true){
1253+
if (DBG) Log.d(TAG, "enableNoAutoConnect(): BT is already enabled..!");
1254+
return true;
1255+
}
1256+
try {
1257+
return mManagerService.enableNoAutoConnect();
1258+
} catch (RemoteException e) {Log.e(TAG, "", e);}
1259+
return false;
12511260
}
12521261

12531262
/**

core/java/android/bluetooth/IBluetoothManager.aidl

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ interface IBluetoothManager
2121
void unregisterStateChangeCallback(in IBluetoothStateChangeCallback callback);
2222
boolean isEnabled();
2323
boolean enable();
24+
boolean enableNoAutoConnect();
2425
boolean disable(boolean persist);
2526

2627
String getAddress();

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

100644100755
Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.os.Message;
2323
import android.os.RemoteCallbackList;
2424
import android.os.RemoteException;
25+
import android.os.Binder;
2526
import android.provider.Settings;
2627
import android.util.Log;
2728
import 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

Comments
 (0)