Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d31c3221 authored by Zhihai Xu's avatar Zhihai Xu Committed by Matthew Xie
Browse files

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
parent 6bd881bd
Loading
Loading
Loading
Loading
+3 −3
Original line number Original line Diff line number Diff line
@@ -650,9 +650,9 @@
         speech -->
         speech -->
    <bool name="config_bluetooth_wide_band_speech">true</bool>
    <bool name="config_bluetooth_wide_band_speech">true</bool>


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


    <!-- The default data-use polling period. -->
    <!-- The default data-use polling period. -->
    <integer name="config_datause_polling_period_sec">600</integer>
    <integer name="config_datause_polling_period_sec">600</integer>
+1 −1
Original line number Original line Diff line number Diff line
@@ -245,7 +245,7 @@
  <java-symbol type="bool" name="action_bar_embed_tabs_pre_jb" />
  <java-symbol type="bool" name="action_bar_embed_tabs_pre_jb" />
  <java-symbol type="bool" name="action_bar_expanded_action_views_exclusive" />
  <java-symbol type="bool" name="action_bar_expanded_action_views_exclusive" />
  <java-symbol type="bool" name="config_allowActionMenuItemTextWithIcon" />
  <java-symbol type="bool" name="config_allowActionMenuItemTextWithIcon" />
  <java-symbol type="bool" name="config_bluetooth_adapter_quick_switch" />
  <java-symbol type="bool" name="config_bluetooth_address_validation" />
  <java-symbol type="bool" name="config_bluetooth_sco_off_call" />
  <java-symbol type="bool" name="config_bluetooth_sco_off_call" />
  <java-symbol type="bool" name="config_cellBroadcastAppLinks" />
  <java-symbol type="bool" name="config_cellBroadcastAppLinks" />
  <java-symbol type="bool" name="config_duplicate_port_omadm_wappush" />
  <java-symbol type="bool" name="config_duplicate_port_omadm_wappush" />
+40 −22
Original line number Original line Diff line number Diff line
@@ -53,6 +53,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
    private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
    private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
    private static final String ACTION_SERVICE_STATE_CHANGED="com.android.bluetooth.btservice.action.STATE_CHANGED";
    private static final String ACTION_SERVICE_STATE_CHANGED="com.android.bluetooth.btservice.action.STATE_CHANGED";
    private static final String EXTRA_ACTION="action";
    private static final String EXTRA_ACTION="action";
    private static final String SECURE_SETTINGS_BLUETOOTH_ADDR_VALID="bluetooth_addr_valid";
    private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address";
    private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address";
    private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name";
    private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name";
    private static final int TIMEOUT_BIND_MS = 3000; //Maximum msec to wait for a bind
    private static final int TIMEOUT_BIND_MS = 3000; //Maximum msec to wait for a bind
@@ -174,7 +175,9 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
            //Enable
            //Enable
            if (DBG) Log.d(TAG, "Auto-enabling Bluetooth.");
            if (DBG) Log.d(TAG, "Auto-enabling Bluetooth.");
            enableHelper();
            enableHelper();
        } else if (!isNameAndAddressSet()) {
        }

        if (!isNameAndAddressSet()) {
            //Sync the Bluetooth name and address from the Bluetooth Adapter
            //Sync the Bluetooth name and address from the Bluetooth Adapter
            if (DBG) Log.d(TAG,"Retrieving Bluetooth Adapter name and address...");
            if (DBG) Log.d(TAG,"Retrieving Bluetooth Adapter name and address...");
            getNameAndAddress();
            getNameAndAddress();
@@ -222,11 +225,16 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
     */
     */
    private void loadStoredNameAndAddress() {
    private void loadStoredNameAndAddress() {
        if (DBG) Log.d(TAG, "Loading stored name and address");
        if (DBG) Log.d(TAG, "Loading stored name and address");
        if (mContext.getResources().getBoolean
            (com.android.internal.R.bool.config_bluetooth_address_validation) &&
             Settings.Secure.getInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 0) == 0) {
            // if the valid flag is not set, don't load the address and name
            if (DBG) Log.d(TAG, "invalid bluetooth name and address stored");
            return;
        }
        mName = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME);
        mName = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME);
        mAddress = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS);
        mAddress = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS);
        if (mName == null || mAddress == null) {
        if (DBG) Log.d(TAG, "Stored bluetooth Name=" + mName + ",Address=" + mAddress);
            if (DBG) Log.d(TAG, "Name or address not cached...");
        }
    }
    }


    /**
    /**
@@ -249,6 +257,10 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
            if (DBG)  Log.d(TAG,"Stored Bluetoothaddress: " +
            if (DBG)  Log.d(TAG,"Stored Bluetoothaddress: " +
                Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_ADDRESS));
                Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_ADDRESS));
        }
        }

        if ((name != null) && (address != null)) {
            Settings.Secure.putInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 1);
        }
    }
    }


    public IBluetooth registerAdapter(IBluetoothManagerCallback callback){
    public IBluetooth registerAdapter(IBluetoothManagerCallback callback){
@@ -560,7 +572,18 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                    break;
                    break;
                }
                }
                case MESSAGE_SAVE_NAME_AND_ADDRESS: {
                case MESSAGE_SAVE_NAME_AND_ADDRESS: {
                    boolean unbind = false;
                    if (DBG) Log.d(TAG,"MESSAGE_SAVE_NAME_AND_ADDRESS");
                    if (DBG) Log.d(TAG,"MESSAGE_SAVE_NAME_AND_ADDRESS");
                    synchronized(mConnection) {
                        if (!mEnable && mBluetooth != null) {
                            try {
                                mBluetooth.enable();
                            } catch (RemoteException e) {
                                Log.e(TAG,"Unable to call enable()",e);
                            }
                        }
                    }
                    if (mBluetooth != null) waitForOnOff(true, false);
                    synchronized(mConnection) {
                    synchronized(mConnection) {
                        if (mBluetooth != null) {
                        if (mBluetooth != null) {
                            String name =  null;
                            String name =  null;
@@ -575,7 +598,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                            if (name != null && address != null) {
                            if (name != null && address != null) {
                                storeNameAndAddress(name,address);
                                storeNameAndAddress(name,address);
                                if (mConnection.isGetNameAddressOnly()) {
                                if (mConnection.isGetNameAddressOnly()) {
                                    unbindAndFinish();
                                    unbind = true;
                                }
                                }
                            } else {
                            } else {
                                if (msg.arg1 < MAX_SAVE_RETRIES) {
                                if (msg.arg1 < MAX_SAVE_RETRIES) {
@@ -586,10 +609,17 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                                } else {
                                } else {
                                    Log.w(TAG,"Maximum name/address remote retrieval retry exceeded");
                                    Log.w(TAG,"Maximum name/address remote retrieval retry exceeded");
                                    if (mConnection.isGetNameAddressOnly()) {
                                    if (mConnection.isGetNameAddressOnly()) {
                                        unbindAndFinish();
                                        unbind = true;
                                    }
                                    }
                                }
                                }
                            }
                            }
                            if (!mEnable) {
                                try {
                                    mBluetooth.disable();
                                } catch (RemoteException e) {
                                    Log.e(TAG,"Unable to call disable()",e);
                                }
                            }
                        } else {
                        } else {
                            // rebind service by Request GET NAME AND ADDRESS
                            // rebind service by Request GET NAME AND ADDRESS
                            // if service is unbinded by disable or
                            // if service is unbinded by disable or
@@ -598,6 +628,10 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                            mHandler.sendMessage(getMsg);
                            mHandler.sendMessage(getMsg);
                        }
                        }
                    }
                    }
                    if (!mEnable && mBluetooth != null) waitForOnOff(false, true);
                    if (unbind) {
                        unbindAndFinish();
                    }
                    break;
                    break;
                }
                }
                case MESSAGE_ENABLE:
                case MESSAGE_ENABLE:
@@ -677,14 +711,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                        //Inform BluetoothAdapter instances that service is up
                        //Inform BluetoothAdapter instances that service is up
                        sendBluetoothServiceUpCallback();
                        sendBluetoothServiceUpCallback();


                        //Check if name and address is loaded if not get it first.
                        if (!isNameAndAddressSet()) {
                            try {
                                storeNameAndAddress(mBluetooth.getName(),
                                                    mBluetooth.getAddress());
                            } catch (RemoteException e) {Log.e(TAG, "", e);};
                        }

                        //Do enable request
                        //Do enable request
                        try {
                        try {
                            if (mQuietEnable == false) {
                            if (mQuietEnable == false) {
@@ -873,14 +899,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                    sendBluetoothServiceUpCallback();
                    sendBluetoothServiceUpCallback();
                }
                }


                //Check if name and address is loaded if not get it first.
                if (!isNameAndAddressSet()) {
                    try {
                        if (DBG) Log.d(TAG,"Getting and storing Bluetooth name and address prior to enable.");
                        storeNameAndAddress(mBluetooth.getName(),mBluetooth.getAddress());
                    } catch (RemoteException e) {Log.e(TAG, "", e);};
                }

                //Enable bluetooth
                //Enable bluetooth
                try {
                try {
                    if (!mQuietEnable) {
                    if (!mQuietEnable) {