Class PolarCompanionDeviceManager
-
- All Implemented Interfaces:
public final class PolarCompanionDeviceManagerWrapper around Android's CompanionDeviceManager (CDM), integrated with the classic BLE scan/connect flow already implemented by com.polar.androidcommunications.enpoints.ble.bluedroid.host.BDScanCallback and com.polar.androidcommunications.enpoints.ble.bluedroid.host.BDDeviceListenerImpl.
This class purposefully keeps a narrow surface and delegates the actual
IntentSenderlaunch (user consent dialog) to the calling Activity, since the base library has no Activity context of its own.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public interfacePolarCompanionDeviceManager.AssociationCallbackCallback for the outcome of an association request created with buildAssociationRequest.
-
Constructor Summary
Constructors Constructor Description PolarCompanionDeviceManager(Context context)
-
Method Summary
Modifier and Type Method Description final BooleanisAvailable()final AssociationRequestbuildAssociationRequest(String namePrefix, List<UUID> serviceUuids, Boolean singleDevice, String macAddress)Builds an AssociationRequest that matches nearby BLE devices advertising one of the given serviceUuids, optionally restricted to devices whose name starts with namePrefix. final Unitassociate(AssociationRequest request, Executor executor, PolarCompanionDeviceManager.AssociationCallback callback)Starts the CDM association flow. final UnitlaunchAssociation(Activity activity, IntentSender intentSender, Integer requestCode)Convenience helper for launching the association IntentSender from an Activity using the legacy startIntentSenderForResultAPI (for callers not yet using the Activity Result APIs).final List<String>associatedDeviceAddresses()final BooleanisAssociated(String macAddress)final Unitdisassociate(String macAddress)Removes the CDM association for macAddress, if any. final BooleanstartObservingDevicePresence(String macAddress)Starts observing device presence for an already-associated device. final BooleanstopObservingDevicePresence(String macAddress)Stops observing device presence previously started with startObservingDevicePresence. final BooleanrequestBackgroundExecutionPrivilege(Activity activity, Integer requestCode)Requests the "run in background" companion app privilege (API 33+). final static BooleanisAvailable(Context context)-
-
Constructor Detail
-
PolarCompanionDeviceManager
PolarCompanionDeviceManager(Context context)
-
-
Method Detail
-
isAvailable
final Boolean isAvailable()
- Returns:
true if CDM is available on this device/OS version for this instance.
-
buildAssociationRequest
final AssociationRequest buildAssociationRequest(String namePrefix, List<UUID> serviceUuids, Boolean singleDevice, String macAddress)
Builds an AssociationRequest that matches nearby BLE devices advertising one of the given serviceUuids, optionally restricted to devices whose name starts with namePrefix.
- Parameters:
namePrefix- optional required BLE device name prefix, e.g.serviceUuids- BLE advertised service UUIDs to match, e.g.singleDevice- if true, the system association dialog will only allow single selection and, on API 33+, may skip the dialog entirely if exactly one strong match is found.macAddress- optional BT MAC address; when set, only that exact device can match, so the chooser can never offer (or auto-pick) a different nearby device.
-
associate
final Unit associate(AssociationRequest request, Executor executor, PolarCompanionDeviceManager.AssociationCallback callback)
Starts the CDM association flow. Result is delivered asynchronously via callback.
-
launchAssociation
final Unit launchAssociation(Activity activity, IntentSender intentSender, Integer requestCode)
Convenience helper for launching the association IntentSender from an Activity using the legacy
startIntentSenderForResultAPI (for callers not yet using the Activity Result APIs). PreferActivityResultContracts.StartIntentSenderForResultwhen possible.
-
associatedDeviceAddresses
final List<String> associatedDeviceAddresses()
- Returns:
list of BT MAC addresses of currently CDM-associated devices for this app.
-
isAssociated
final Boolean isAssociated(String macAddress)
- Returns:
true if macAddress is already associated via CDM.
-
disassociate
final Unit disassociate(String macAddress)
Removes the CDM association for macAddress, if any.
-
startObservingDevicePresence
final Boolean startObservingDevicePresence(String macAddress)
Starts observing device presence for an already-associated device. Once observing, the system will deliver android.companion.CompanionDeviceService.onDeviceAppeared /
onDeviceDisappearedcallbacks (and may even start the app's declared android.companion.CompanionDeviceService to handle them), improving reconnection responsiveness without requiring continuous foreground BLE scanning.
-
stopObservingDevicePresence
final Boolean stopObservingDevicePresence(String macAddress)
Stops observing device presence previously started with startObservingDevicePresence.
-
requestBackgroundExecutionPrivilege
final Boolean requestBackgroundExecutionPrivilege(Activity activity, Integer requestCode)
Requests the "run in background" companion app privilege (API 33+). This relaxes background execution / BLE scan throttling limits normally imposed on apps once they are backgrounded, for apps that are companion apps of an associated device. Must be called from an Activity; the result is delivered to
onActivityResult(requestCode, ...)of that activity (or via anActivityResultLauncher<Intent>).Note: the
REQUEST_COMPANION_RUN_IN_BACKGROUNDaction constant is not exposed in the public Android SDK stubs (compileSdk), so the documented action string literal is used directly.- Returns:
true if the request intent was launched, false if not supported/available.
-
isAvailable
final static Boolean isAvailable(Context context)
- Returns:
true if the Companion Device Manager APIs are available on this OS version and this device supports the companion device setup feature.
-
-
-
-