Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CompanionDeviceManager 'onDeviceFound' Callback Function Not Being Called

We are trying to use CompanionDeviceManager class to pair our BLE device with our Android (Version 10) phone without need of location permission.

For testing purposes we activited bluetooth of multiple phones and ble devices around our test device.

We are using the example code from official site without success.

Added these codes to AndroidManifest file:

<uses-feature android:name="android.hardware.bluetooth"/>
<uses-feature android:name="android.software.companion_device_setup"/>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

Complete code of MainActivity :

public class MainActivity extends AppCompatActivity {


    private CompanionDeviceManager deviceManager;
    private AssociationRequest pairingRequest;
    private BluetoothDeviceFilter deviceFilter;

    private static final int SELECT_DEVICE_REQUEST_CODE = 42;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.e("ArkSigner", "onCreate called.");

        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("Test"))
                //.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();

        List<String> associations = deviceManager.getAssociations();

        // 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) {
                        try {

                            Log.e("ArkSigner", "onDeviceFound called.");

                            startIntentSenderForResult(chooserLauncher,
                                    SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0);
                        } catch (IntentSender.SendIntentException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(CharSequence error) {
                        Log.e("ArkSigner", "onFailure called.");
                    }
                },
                null);


    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        
        Log.e("ArkSigner", "onActivityResult called.");

        if (requestCode == SELECT_DEVICE_REQUEST_CODE &&
                resultCode == Activity.RESULT_OK) {
            // User has chosen to pair with the Bluetooth device.
            BluetoothDevice deviceToPair =
                    data.getParcelableExtra(CompanionDeviceManager.EXTRA_DEVICE);
            deviceToPair.createBond();

            // ... Continue interacting with the paired device.
        }
    }
}

After we run our Android application, we are not seeing any dialog about found devices.(Bluetooth or BLE devices)

Edit: If we give a devicename in line ".setNamePattern(Pattern.compile("Test"))" or use ".setSingleDevice(true)" it doesn't change anything.

like image 686
ArkSigner Avatar asked Jul 10 '20 11:07

ArkSigner


1 Answers

You should use BluetoothLeDeviceFilter instead since BluetoothDeviceFilter is for non-BLE devices according to the documentation at https://developer.android.com/reference/android/companion/BluetoothDeviceFilter.

Also expect the parcelable extra to contain a android.bluetooth.le.ScanResult rather than a BluetoothDevice for BLE devices.

like image 50
Emil Avatar answered Sep 29 '22 09:09

Emil