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.