CompanionDeviceManager.Callback


public static abstract class CompanionDeviceManager.Callback
extends Object

java.lang.Object
   ↳ android.companion.CompanionDeviceManager.Callback


Callback for applications to receive updates about and the outcome of AssociationRequest issued via associate() call.

The Callback.onAssociationPending(IntentSender) is invoked after the AssociationRequest has been checked by the Companion Device Manager Service and is pending user's approval. The IntentSender received as an argument to Callback.onAssociationPending(IntentSender) "encapsulates" an Activity that has UI for the user to:

  • choose the device to associate the application with (if multiple eligible devices are available)
  • confirm the association
  • approve the privileges the application will be granted if the association is to be created
If the Companion Device Manager Service needs to scan for the devices, the Activity will also display the status and the progress of the scan. Note that Companion Device Manager Service will only start the scanning after the Activity was launched and became visible. Applications are expected to launch the UI using the received IntentSender via Activity.startIntentSenderForResult(IntentSender, int, Intent, int, int, int).

Upon receiving user's confirmation Companion Device Manager Service will create an association and will send an AssociationInfo object that represents the created association back to the application both via Callback.onAssociationCreated(AssociationInfo) and via Activity.setResult(int, Intent). In the latter the resultCode will be set to Activity.RESULT_OK and the data Intent will contain AssociationInfo extra named CompanionDeviceManager.EXTRA_ASSOCIATION.

 
   if (resultCode == Activity.RESULT_OK) {
     AssociationInfo associationInfo = data.getParcelableExtra(EXTRA_ASSOCIATION);
   }
 
 

If the Companion Device Manager Service is not able to create an association, it will invoke Callback.onFailure(CharSequence). If this happened after the application has launched the UI (eg. the user chose to reject the association), the outcome will also be delivered to the applications via Activity.setResult(int) with the Activity.RESULT_CANCELED resultCode.

Note that in some cases the Companion Device Manager Service may not need to collect user's approval for creating an association. In such cases, this method will not be invoked, and onAssociationCreated(android.companion.AssociationInfo) may be invoked right away.

Summary

Public constructors

Callback()

Public methods

void onAssociationCreated(AssociationInfo associationInfo)

Invoked when the association is created.

void onAssociationPending(IntentSender intentSender)

Invoked when the association needs to approved by the user.

void onDeviceFound(IntentSender intentSender)

This method was deprecated in API level 33. method was renamed to onAssociationPending() to provide better clarity; both methods are functionally equivalent and only one needs to be overridden.

void onFailure(int errorCode, CharSequence error)

Invoked if the association could not be created.

abstract void onFailure(CharSequence error)

Invoked if the association could not be created.

Inherited methods

Public constructors

Callback

public Callback ()

Public methods

onAssociationCreated

Added in API level 33
public void onAssociationCreated (AssociationInfo associationInfo)

Invoked when the association is created.

Parameters
associationInfo AssociationInfo: contains details of the newly-established association. This value cannot be null.

onAssociationPending

Added in API level 33
public void onAssociationPending (IntentSender intentSender)

Invoked when the association needs to approved by the user. Applications should launch the Activity "encapsulated" in intentSender IntentSender object by calling Activity.startIntentSenderForResult(IntentSender, int, Intent, int, int, int).

Parameters
intentSender IntentSender: an IntentSender which applications should use to launch the UI for the user to confirm the association. This value cannot be null.

onDeviceFound

Added in API level 26
Deprecated in API level 33
public void onDeviceFound (IntentSender intentSender)

This method was deprecated in API level 33.
method was renamed to onAssociationPending() to provide better clarity; both methods are functionally equivalent and only one needs to be overridden.

Parameters
intentSender IntentSender: This value cannot be null.

onFailure

public void onFailure (int errorCode, 
                CharSequence error)

Invoked if the association could not be created. Please note that both onFailure(java.lang.CharSequence) and this API will be called if the association could not be created.

Parameters
errorCode int: indicate the particular error code why the association could not be created. Value is CompanionDeviceManager.RESULT_OK, CompanionDeviceManager.RESULT_CANCELED, CompanionDeviceManager.RESULT_USER_REJECTED, CompanionDeviceManager.RESULT_DISCOVERY_TIMEOUT, or CompanionDeviceManager.RESULT_INTERNAL_ERROR

error CharSequence: error message. This value may be null.

onFailure

Added in API level 26
public abstract void onFailure (CharSequence error)

Invoked if the association could not be created.

Parameters
error CharSequence: error message. This value may be null.