Search in sources :

Example 1 with BluetoothDeviceFilter

use of android.companion.BluetoothDeviceFilter in project Gadgetbridge by Freeyourgadget.

the class BondingUtil method companionDeviceManagerBond.

/**
 * Uses the CompanionDeviceManager bonding method
 */
@RequiresApi(Build.VERSION_CODES.O)
private static void companionDeviceManagerBond(BondingInterface bondingInterface, final GBDeviceCandidate deviceCandidate) {
    BluetoothDeviceFilter deviceFilter = new BluetoothDeviceFilter.Builder().setAddress(deviceCandidate.getMacAddress()).build();
    AssociationRequest pairingRequest = new AssociationRequest.Builder().addDeviceFilter(deviceFilter).setSingleDevice(true).build();
    CompanionDeviceManager manager = (CompanionDeviceManager) bondingInterface.getContext().getSystemService(Context.COMPANION_DEVICE_SERVICE);
    LOG.debug(String.format("Searching for %s associations", deviceCandidate.getMacAddress()));
    for (String association : manager.getAssociations()) {
        LOG.debug(String.format("Already associated with: %s", association));
        if (association.equals(deviceCandidate.getMacAddress())) {
            LOG.info("The device has already been bonded through CompanionDeviceManager, using regular");
            // If it's already "associated", we should immediately pair
            // because the callback is never called (AFAIK?)
            BondingUtil.bluetoothBond(bondingInterface, deviceCandidate);
            return;
        }
    }
    LOG.debug("Starting association request");
    manager.associate(pairingRequest, getCompanionDeviceManagerCallback(bondingInterface), null);
}
Also used : CompanionDeviceManager(android.companion.CompanionDeviceManager) AssociationRequest(android.companion.AssociationRequest) BluetoothDeviceFilter(android.companion.BluetoothDeviceFilter) RequiresApi(androidx.annotation.RequiresApi)

Aggregations

AssociationRequest (android.companion.AssociationRequest)1 BluetoothDeviceFilter (android.companion.BluetoothDeviceFilter)1 CompanionDeviceManager (android.companion.CompanionDeviceManager)1 RequiresApi (androidx.annotation.RequiresApi)1