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

Commit 34084af5 authored by Hall Liu's avatar Hall Liu
Browse files

Speculative fix for BT race condition

When the BT stack goes down very soon after a device added broadcast is
received, we have the potential for a device to stick around in the
device manager with no active BT service, leading to repeated failure to
connect to said device. This change checks to make sure the service is
non null before adding the device.

Change-Id: Ibee8ba2c611d750147249ce1c750a8d433ebff5b
Fixes: 119123459
Test: unit
parent 85e1fde6
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -194,6 +194,10 @@ public class BluetoothDeviceManager {
                mHearingAidDeviceSyncIds.put(device, hiSyncId);
                targetDeviceMap = mHearingAidDevicesByAddress;
            } else {
                if (mBluetoothHeadsetService == null) {
                    Log.w(this, "Headset service null when receiving device added broadcast");
                    return;
                }
                targetDeviceMap = mHfpDevicesByAddress;
            }
            if (!targetDeviceMap.containsKey(device.getAddress())) {
+14 −0
Original line number Diff line number Diff line
@@ -106,6 +106,20 @@ public class BluetoothDeviceManagerTest extends TelecomTestCase {
        assertEquals(0, mBluetoothDeviceManager.getNumConnectedDevices());
    }

    @SmallTest
    @Test
    public void testAddDeviceFailsWhenServicesAreNull() {
        mBluetoothDeviceManager.setHeadsetServiceForTesting(null);
        mBluetoothDeviceManager.setHearingAidServiceForTesting(null);

        receiverUnderTest.onReceive(mContext,
                buildConnectionActionIntent(BluetoothHeadset.STATE_CONNECTED, device1, false));
        receiverUnderTest.onReceive(mContext,
                buildConnectionActionIntent(BluetoothHeadset.STATE_CONNECTED, device2, true));

        assertEquals(0, mBluetoothDeviceManager.getNumConnectedDevices());
    }
    
    @SmallTest
    @Test
    public void testMultiDeviceConnectAndDisconnect() {