3

I am trying to use the new CompanionDeviceManager but i am not able to figure out how to connect to the device for the second time (after pairing was successful), associate needs to be called only one time and cant be used again.

The example is very simple:

public class MyDeviceSelectionActivity extends Activity {
    private CompanionDeviceManager deviceManager;
    private AssociationRequest pairingRequest;
    private BluetoothDeviceFilter deviceFilter;

    private static final int SELECT_DEVICE_REQUEST_CODE = 42;

    @override
    public void onCreate() {
        // ...
        deviceManager = getSystemService(CompanionDeviceManager.class);

        // To skip filtering based on name and supported feature flags (UUIDs),
        // don't include calls to setNamePattern() and addServiceUuid(),
        // respectively. This example uses Bluetooth.
        deviceFilter = new BluetoothDeviceFilter.Builder()
                .setNamePattern(Pattern.compile("My device"))
                .addServiceUuid(new ParcelUuid(new UUID(0x123abcL, -1L)), null)
                .build();

        // The argument provided in setSingleDevice() determines whether a single
        // device name or a list of device names is presented to the user as
        // pairing options.
        pairingRequest = new AssociationRequest.Builder()
                .addDeviceFilter(deviceFilter)
                .setSingleDevice(true)
                .build();

        // When the app tries to pair with the Bluetooth device, show the
        // appropriate pairing request dialog to the user.
        deviceManager.associate(pairingRequest,
                new CompanionDeviceManager.Callback() {
                    @Override
                    public void onDeviceFound(IntentSender chooserLauncher) {
                        startIntentSenderForResult(chooserLauncher,
                                SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0);
                    }
                },
                null);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == SELECT_DEVICE_REQUEST_CODE &&
                resultCode == Activity.RESULT_OK) {
            // User has chosen to pair with the Bluetooth device.
            ScanResult scanResult =
                    data.getParcelableExtra(CompanionDeviceManager.EXTRA_DEVICE);
            deviceToPair.createBond();

           
        }
    }
}

after the device is paired i can get the ScanResult in onActivityResult.

The question is how can i get the ScanResult without calling associate again.

Maxim Toyberman
  • 1,906
  • 1
  • 20
  • 35

2 Answers2

0

You can use getAssociation on your CompanionDeviceManager (see https://developer.android.com/reference/android/companion/CompanionDeviceManager#getAssociations()).

You will retrieve all the bleutooth devices that have been associated (and not dissociated) to your Android device.

deepo
  • 1
  • 1
  • Thank you for the answer, but the question is how to connect to an associated device already. I can't scan for devices – Maxim Toyberman Oct 13 '20 at 14:09
  • You can get them from the Bluetooh bonded devices: `BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();` and `Set pairedDevices = bluetoothAdapter.getBondedDevices();` (and use the mac address returned by "getAssociations" to filter the BondedDevices) – deepo Oct 14 '20 at 06:14
  • but it doesn't ensure that device is on and that i can connect to it. another issue that i see with using getAssociations is that users that already paired device and will update to the latest app will get an empty list when calling getAssociations. – Maxim Toyberman Oct 14 '20 at 11:47
  • To be able to use CompanionDeviceManager as a BLEScanner, you can remove all the associations (`deviceManager.disassociate(deviceHardwareAddress);`) before calling `associate` again. – deepo Oct 14 '20 at 16:14
  • lets say you called associate, and you have the mac address, you just call connect with the mac address to connect to the device ? – Maxim Toyberman Oct 19 '20 at 09:08
  • How can i know that device is in range – Maxim Toyberman Oct 19 '20 at 09:35
  • it may be very annoying from users perspective to see association popup everytime.. – Maxim Toyberman Oct 21 '20 at 08:25
  • Maybe you can check this answer : https://stackoverflow.com/questions/4715865/how-to-programmatically-tell-if-a-bluetooth-device-is-connected – deepo Oct 21 '20 at 13:17
  • @MaximToyberman You can receive OnDeviceAppeared and OnDeviceDisappeared callbacks using this method. But it's a new API (was added in level 31). https://developer.android.com/reference/android/companion/CompanionDeviceManager#startObservingDevicePresence(java.lang.String) – Mehmet AVŞAR Aug 10 '22 at 14:54
0

You can extend a CompanionDeviceService and call this API to get notified when device is nearby/disappeared: https://developer.android.com/reference/android/companion/CompanionDeviceManager#startObservingDevicePresence(java.lang.String)

samygj
  • 1
  • 2