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