diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManager.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManager.java index 5662ce6bd8084..6bc1160a8d0ab 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManager.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManager.java @@ -356,7 +356,7 @@ public class CachedBluetoothDeviceManager { * @return {@code true}, if the device should pair automatically; Otherwise, return * {@code false}. */ - public synchronized boolean shouldPairByCsip(BluetoothDevice device, int groupId) { + private synchronized boolean shouldPairByCsip(BluetoothDevice device, int groupId) { boolean isOngoingSetMemberPair = mOngoingSetMemberPair != null; int bondState = device.getBondState(); if (isOngoingSetMemberPair || bondState != BluetoothDevice.BOND_NONE @@ -365,12 +365,46 @@ public class CachedBluetoothDeviceManager { + " , device.getBondState: " + bondState); return false; } - - Log.d(TAG, "Bond " + device.getName() + " by CSIP"); - mOngoingSetMemberPair = device; return true; } + /** + * Called when we found a set member of a group. The function will check the {@code groupId} if + * it exists and the bond state of the device is BOND_NONE, and if there isn't any ongoing pair + * , and then pair the device automatically. + * + * @param device The found device + * @param groupId The group id of the found device + */ + public synchronized void pairDeviceByCsip(BluetoothDevice device, int groupId) { + if (!shouldPairByCsip(device, groupId)) { + return; + } + Log.d(TAG, "Bond " + device.getAnonymizedAddress() + " by CSIP"); + mOngoingSetMemberPair = device; + syncConfigFromMainDevice(device, groupId); + device.createBond(BluetoothDevice.TRANSPORT_LE); + } + + private void syncConfigFromMainDevice(BluetoothDevice device, int groupId) { + if (!isOngoingPairByCsip(device)) { + return; + } + CachedBluetoothDevice memberDevice = findDevice(device); + CachedBluetoothDevice mainDevice = mCsipDeviceManager.findMainDevice(memberDevice); + if (mainDevice == null) { + mainDevice = mCsipDeviceManager.getCachedDevice(groupId); + } + + if (mainDevice == null || mainDevice.equals(memberDevice)) { + Log.d(TAG, "no mainDevice"); + return; + } + + // The memberDevice set PhonebookAccessPermission + device.setPhonebookAccessPermission(mainDevice.getDevice().getPhonebookAccessPermission()); + } + /** * Called when the bond state change. If the bond state change is related with the * ongoing set member pair, the cachedBluetoothDevice will be created but the UI diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CsipDeviceManager.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CsipDeviceManager.java index d5de3f0525a09..20a6cd8e09ce2 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CsipDeviceManager.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CsipDeviceManager.java @@ -101,7 +101,14 @@ public class CsipDeviceManager { return groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID; } - private CachedBluetoothDevice getCachedDevice(int groupId) { + /** + * To find the device with {@code groupId}. + * + * @param groupId The group id + * @return if we could find a device with this {@code groupId} return this device. Otherwise, + * return null. + */ + public CachedBluetoothDevice getCachedDevice(int groupId) { log("getCachedDevice: groupId: " + groupId); for (int i = mCachedDevices.size() - 1; i >= 0; i--) { CachedBluetoothDevice cachedDevice = mCachedDevices.get(i); diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManagerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManagerTest.java index 62552f9144594..61802a87361cf 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManagerTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManagerTest.java @@ -582,4 +582,24 @@ public class CachedBluetoothDeviceManagerTest { assertThat(mCachedDeviceManager.isSubDevice(mDevice2)).isTrue(); assertThat(mCachedDeviceManager.isSubDevice(mDevice3)).isFalse(); } + + @Test + public void pairDeviceByCsip_device2AndCapGroup1_device2StartsPairing() { + doReturn(CAP_GROUP1).when(mCsipSetCoordinatorProfile).getGroupUuidMapByDevice(mDevice1); + when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); + when(mDevice1.getPhonebookAccessPermission()).thenReturn(BluetoothDevice.ACCESS_ALLOWED); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); + assertThat(cachedDevice1).isNotNull(); + when(mDevice2.getBondState()).thenReturn(BluetoothDevice.BOND_NONE); + CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2); + assertThat(cachedDevice2).isNotNull(); + + int groupId = CAP_GROUP1.keySet().stream().findFirst().orElse( + BluetoothCsipSetCoordinator.GROUP_ID_INVALID); + assertThat(groupId).isNotEqualTo(BluetoothCsipSetCoordinator.GROUP_ID_INVALID); + mCachedDeviceManager.pairDeviceByCsip(mDevice2, groupId); + + verify(mDevice2).setPhonebookAccessPermission(BluetoothDevice.ACCESS_ALLOWED); + verify(mDevice2).createBond(BluetoothDevice.TRANSPORT_LE); + } }