Skip to content

Commit d31c322

Browse files
Zhihai XuMatthew Xie
authored andcommitted
Fix for BluetoothAdapter.getAddress() when BT is off on some devices
There are two problem, 1.If we have wrong bluetooth address in global settings db, we never will update it 2. We need enable bluetooth to get the bluetooth address for some devices. For 1, we fix it by add a valid flag in global setting db, this flag will be set when we stored correct address and name to db. We only load the name and address from global setting db when this valid flag is set during power up. For2. we will read BT address after bluetooth is at ON state. bug 7440409 Change-Id: Ic4740b3f0b2fcd214c7ca8393f7331c140eec66d
1 parent 6bd881b commit d31c322

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

core/res/res/values/config.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,9 @@
650650
speech -->
651651
<bool name="config_bluetooth_wide_band_speech">true</bool>
652652

653-
<!-- Boolean indicating if current platform supports quick switch-on/off of
654-
Bluetooth Module -->
655-
<bool name="config_bluetooth_adapter_quick_switch">true</bool>
653+
<!-- Boolean indicating if current platform need do one-time bluetooth address
654+
re-validation -->
655+
<bool name="config_bluetooth_address_validation">false</bool>
656656

657657
<!-- The default data-use polling period. -->
658658
<integer name="config_datause_polling_period_sec">600</integer>

core/res/res/values/symbols.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@
245245
<java-symbol type="bool" name="action_bar_embed_tabs_pre_jb" />
246246
<java-symbol type="bool" name="action_bar_expanded_action_views_exclusive" />
247247
<java-symbol type="bool" name="config_allowActionMenuItemTextWithIcon" />
248-
<java-symbol type="bool" name="config_bluetooth_adapter_quick_switch" />
248+
<java-symbol type="bool" name="config_bluetooth_address_validation" />
249249
<java-symbol type="bool" name="config_bluetooth_sco_off_call" />
250250
<java-symbol type="bool" name="config_cellBroadcastAppLinks" />
251251
<java-symbol type="bool" name="config_duplicate_port_omadm_wappush" />

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

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
5353
private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
5454
private static final String ACTION_SERVICE_STATE_CHANGED="com.android.bluetooth.btservice.action.STATE_CHANGED";
5555
private static final String EXTRA_ACTION="action";
56+
private static final String SECURE_SETTINGS_BLUETOOTH_ADDR_VALID="bluetooth_addr_valid";
5657
private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address";
5758
private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name";
5859
private static final int TIMEOUT_BIND_MS = 3000; //Maximum msec to wait for a bind
@@ -174,7 +175,9 @@ public void onReceive(Context context, Intent intent) {
174175
//Enable
175176
if (DBG) Log.d(TAG, "Auto-enabling Bluetooth.");
176177
enableHelper();
177-
} else if (!isNameAndAddressSet()) {
178+
}
179+
180+
if (!isNameAndAddressSet()) {
178181
//Sync the Bluetooth name and address from the Bluetooth Adapter
179182
if (DBG) Log.d(TAG,"Retrieving Bluetooth Adapter name and address...");
180183
getNameAndAddress();
@@ -222,11 +225,16 @@ private boolean isNameAndAddressSet() {
222225
*/
223226
private void loadStoredNameAndAddress() {
224227
if (DBG) Log.d(TAG, "Loading stored name and address");
228+
if (mContext.getResources().getBoolean
229+
(com.android.internal.R.bool.config_bluetooth_address_validation) &&
230+
Settings.Secure.getInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 0) == 0) {
231+
// if the valid flag is not set, don't load the address and name
232+
if (DBG) Log.d(TAG, "invalid bluetooth name and address stored");
233+
return;
234+
}
225235
mName = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME);
226236
mAddress = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS);
227-
if (mName == null || mAddress == null) {
228-
if (DBG) Log.d(TAG, "Name or address not cached...");
229-
}
237+
if (DBG) Log.d(TAG, "Stored bluetooth Name=" + mName + ",Address=" + mAddress);
230238
}
231239

232240
/**
@@ -249,6 +257,10 @@ private void storeNameAndAddress(String name, String address) {
249257
if (DBG) Log.d(TAG,"Stored Bluetoothaddress: " +
250258
Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_ADDRESS));
251259
}
260+
261+
if ((name != null) && (address != null)) {
262+
Settings.Secure.putInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 1);
263+
}
252264
}
253265

254266
public IBluetooth registerAdapter(IBluetoothManagerCallback callback){
@@ -560,7 +572,18 @@ public void handleMessage(Message msg) {
560572
break;
561573
}
562574
case MESSAGE_SAVE_NAME_AND_ADDRESS: {
575+
boolean unbind = false;
563576
if (DBG) Log.d(TAG,"MESSAGE_SAVE_NAME_AND_ADDRESS");
577+
synchronized(mConnection) {
578+
if (!mEnable && mBluetooth != null) {
579+
try {
580+
mBluetooth.enable();
581+
} catch (RemoteException e) {
582+
Log.e(TAG,"Unable to call enable()",e);
583+
}
584+
}
585+
}
586+
if (mBluetooth != null) waitForOnOff(true, false);
564587
synchronized(mConnection) {
565588
if (mBluetooth != null) {
566589
String name = null;
@@ -575,7 +598,7 @@ public void handleMessage(Message msg) {
575598
if (name != null && address != null) {
576599
storeNameAndAddress(name,address);
577600
if (mConnection.isGetNameAddressOnly()) {
578-
unbindAndFinish();
601+
unbind = true;
579602
}
580603
} else {
581604
if (msg.arg1 < MAX_SAVE_RETRIES) {
@@ -586,10 +609,17 @@ public void handleMessage(Message msg) {
586609
} else {
587610
Log.w(TAG,"Maximum name/address remote retrieval retry exceeded");
588611
if (mConnection.isGetNameAddressOnly()) {
589-
unbindAndFinish();
612+
unbind = true;
590613
}
591614
}
592615
}
616+
if (!mEnable) {
617+
try {
618+
mBluetooth.disable();
619+
} catch (RemoteException e) {
620+
Log.e(TAG,"Unable to call disable()",e);
621+
}
622+
}
593623
} else {
594624
// rebind service by Request GET NAME AND ADDRESS
595625
// if service is unbinded by disable or
@@ -598,6 +628,10 @@ public void handleMessage(Message msg) {
598628
mHandler.sendMessage(getMsg);
599629
}
600630
}
631+
if (!mEnable && mBluetooth != null) waitForOnOff(false, true);
632+
if (unbind) {
633+
unbindAndFinish();
634+
}
601635
break;
602636
}
603637
case MESSAGE_ENABLE:
@@ -677,14 +711,6 @@ public void handleMessage(Message msg) {
677711
//Inform BluetoothAdapter instances that service is up
678712
sendBluetoothServiceUpCallback();
679713

680-
//Check if name and address is loaded if not get it first.
681-
if (!isNameAndAddressSet()) {
682-
try {
683-
storeNameAndAddress(mBluetooth.getName(),
684-
mBluetooth.getAddress());
685-
} catch (RemoteException e) {Log.e(TAG, "", e);};
686-
}
687-
688714
//Do enable request
689715
try {
690716
if (mQuietEnable == false) {
@@ -873,14 +899,6 @@ private void handleEnable(boolean persist, boolean quietMode) {
873899
sendBluetoothServiceUpCallback();
874900
}
875901

876-
//Check if name and address is loaded if not get it first.
877-
if (!isNameAndAddressSet()) {
878-
try {
879-
if (DBG) Log.d(TAG,"Getting and storing Bluetooth name and address prior to enable.");
880-
storeNameAndAddress(mBluetooth.getName(),mBluetooth.getAddress());
881-
} catch (RemoteException e) {Log.e(TAG, "", e);};
882-
}
883-
884902
//Enable bluetooth
885903
try {
886904
if (!mQuietEnable) {

0 commit comments

Comments
 (0)