# CompanionDeviceManager.Callback

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

# Callback

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/android/companion/CompanionDeviceManager.Callback "View this page in Java")  

```
abstract class Callback
```

|---|--------------------------------------------------------------------------------------------------------------------------------------------------------|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)                                                                         ||
| ↳ | [android.companion.CompanionDeviceManager.Callback](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager.Callback#) |

Callback for applications to receive updates about and the outcome of[AssociationRequest](https://developer.android.com/reference/kotlin/android/companion/AssociationRequest)issued via`associate()`call.

The[Callback.onAssociationPending(IntentSender)](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager.Callback#onAssociationPending(android.content.IntentSender))is invoked after the[AssociationRequest](https://developer.android.com/reference/kotlin/android/companion/AssociationRequest)has been checked by the Companion Device Manager Service and is pending user's approval. The[IntentSender](https://developer.android.com/reference/kotlin/android/content/IntentSender.html#)received as an argument to[Callback.onAssociationPending(IntentSender)](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager.Callback#onAssociationPending(android.content.IntentSender))"encapsulates" an[Activity](https://developer.android.com/reference/kotlin/android/app/Activity.html#)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](https://developer.android.com/reference/kotlin/android/app/Activity.html#)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](https://developer.android.com/reference/kotlin/android/app/Activity.html#)was launched and became visible. Applications are expected to launch the UI using the received[IntentSender](https://developer.android.com/reference/kotlin/android/content/IntentSender.html#)via[Activity.startIntentSenderForResult(IntentSender, int, Intent, int, int, int)](https://developer.android.com/reference/kotlin/android/app/Activity.html#startIntentSenderForResult(android.content.IntentSender,%20kotlin.Int,%20android.content.Intent,%20kotlin.Int,%20kotlin.Int,%20kotlin.Int)).

<br />

Upon receiving user's confirmation Companion Device Manager Service will create an association and will send an[AssociationInfo](https://developer.android.com/reference/kotlin/android/companion/AssociationInfo)object that represents the created association back to the application both via[Callback.onAssociationCreated(AssociationInfo)](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager.Callback#onAssociationCreated(android.companion.AssociationInfo))and via[Activity.setResult(int, Intent)](https://developer.android.com/reference/kotlin/android/app/Activity.html#setResult(kotlin.Int,%20android.content.Intent)). In the latter the`resultCode`will be set to[Activity.RESULT_OK](https://developer.android.com/reference/kotlin/android/app/Activity.html#RESULT_OK:kotlin.Int)and the`data`[Intent](https://developer.android.com/reference/kotlin/android/content/Intent.html#)will contain[AssociationInfo](https://developer.android.com/reference/kotlin/android/companion/AssociationInfo)extra named[EXTRA_ASSOCIATION](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager#EXTRA_ASSOCIATION:kotlin.String).  

```kotlin
<code>
    if (resultCode == Activity.RESULT_OK) {
      AssociationInfo associationInfo = data.getParcelableExtra(EXTRA_ASSOCIATION);
    }
  </code>
```

<br />

If the Companion Device Manager Service is not able to create an association, it will invoke[Callback.onFailure(CharSequence)](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager.Callback#onFailure(kotlin.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)](https://developer.android.com/reference/kotlin/android/app/Activity.html#setResult(kotlin.Int))with the[Activity.RESULT_CANCELED](https://developer.android.com/reference/kotlin/android/app/Activity.html#RESULT_CANCELED:kotlin.Int)`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)](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager.Callback#onAssociationCreated(android.companion.AssociationInfo))may be invoked right away.

## Summary

|                                                          Public constructors                                                          ||
|------------------------------------------------------------------------------------------------------------------------------------|---|
| [Callback](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager.Callback#Callback())`()` <br /> |

|                                                                                                                                                                                                                                              Public methods                                                                                                                                                                                                                                               ||
|--------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| open[Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)     | [onAssociationCreated](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager.Callback#onAssociationCreated(android.companion.AssociationInfo))`(`associationInfo:` `[AssociationInfo](https://developer.android.com/reference/kotlin/android/companion/AssociationInfo)`)` Invoked when the association is created.                                                               |
| open[Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)     | [onAssociationPending](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager.Callback#onAssociationPending(android.content.IntentSender))`(`intentSender:` `[IntentSender](https://developer.android.com/reference/kotlin/android/content/IntentSender.html#)`)` Invoked when the association needs to approved by the user.                                                      |
| open[Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)     | [onDeviceFound](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager.Callback#onDeviceFound(android.content.IntentSender))`(`intentSender:` `[IntentSender](https://developer.android.com/reference/kotlin/android/content/IntentSender.html#)`)` <br />                                                                                                                         |
| open[Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)     | [onFailure](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager.Callback#onFailure(kotlin.Int,%20kotlin.CharSequence))`(`errorCode:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`, `error:` `[CharSequence](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-sequence/index.html)?`)` Invoked if the association could not be created. |
| abstract[Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [onFailure](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager.Callback#onFailure(kotlin.CharSequence))`(`error:` `[CharSequence](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-sequence/index.html)?`)` Invoked if the association could not be created.                                                                                                          |

## Public constructors

### Callback

```
Callback()
```

## Public methods

### onAssociationCreated

Added in[API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun onAssociationCreated(associationInfo: AssociationInfo): Unit
```

Invoked when the association is created.

|                                                                                                Parameters                                                                                                 ||
|-------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `associationInfo` | [AssociationInfo](https://developer.android.com/reference/kotlin/android/companion/AssociationInfo):contains details of the newly-established association. This value cannot be`null`. |

### onAssociationPending

Added in[API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun onAssociationPending(intentSender: IntentSender): Unit
```

Invoked when the association needs to approved by the user. Applications should launch the[Activity](https://developer.android.com/reference/kotlin/android/app/Activity.html#)"encapsulated" in`intentSender`[IntentSender](https://developer.android.com/reference/kotlin/android/content/IntentSender.html#)object by calling[Activity.startIntentSenderForResult(IntentSender, int, Intent, int, int, int)](https://developer.android.com/reference/kotlin/android/app/Activity.html#startIntentSenderForResult(android.content.IntentSender,%20kotlin.Int,%20android.content.Intent,%20kotlin.Int,%20kotlin.Int,%20kotlin.Int)).

|                                                                                                                                                                Parameters                                                                                                                                                                ||
|----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `intentSender` | [IntentSender](https://developer.android.com/reference/kotlin/android/content/IntentSender.html#):an[IntentSender](https://developer.android.com/reference/kotlin/android/content/IntentSender.html#)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](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  
Deprecated in[API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun onDeviceFound(intentSender: IntentSender): Unit
```

**Deprecated:** *method was renamed to onAssociationPending() to provide better clarity; both methods are functionally equivalent and only one needs to be overridden.*

|                                                                  Parameters                                                                   ||
|----------------|-------------------------------------------------------------------------------------------------------------------------------|
| `intentSender` | [IntentSender](https://developer.android.com/reference/kotlin/android/content/IntentSender.html#):This value cannot be`null`. |

**See Also**

- [#onAssociationPending(IntentSender)](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager.Callback#onAssociationPending(android.content.IntentSender))  

### onFailure

Added in[API level 36](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun onFailure(
    errorCode: Int, 
    error: CharSequence?
): Unit
```

Invoked if the association could not be created. Please note that both[onFailure(java.lang.CharSequence)](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager.Callback#onFailure(kotlin.CharSequence))and this API will be called if the association could not be created.

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       Parameters                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ||
|-------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `errorCode` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html):indicate the particular error code why the association could not be created. Value is[android.companion.CompanionDeviceManager#RESULT_OK](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager#RESULT_OK:kotlin.Int),[android.companion.CompanionDeviceManager#RESULT_CANCELED](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager#RESULT_CANCELED:kotlin.Int),[android.companion.CompanionDeviceManager#RESULT_USER_REJECTED](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager#RESULT_USER_REJECTED:kotlin.Int),[android.companion.CompanionDeviceManager#RESULT_DISCOVERY_TIMEOUT](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager#RESULT_DISCOVERY_TIMEOUT:kotlin.Int), or[android.companion.CompanionDeviceManager#RESULT_INTERNAL_ERROR](https://developer.android.com/reference/kotlin/android/companion/CompanionDeviceManager#RESULT_INTERNAL_ERROR:kotlin.Int) |
| `error`     | [CharSequence](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-sequence/index.html)?:error message. This value may be`null`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |

### onFailure

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
abstract fun onFailure(error: CharSequence?): Unit
```

Invoked if the association could not be created.

|                                                                   Parameters                                                                    ||
|---------|----------------------------------------------------------------------------------------------------------------------------------------|
| `error` | [CharSequence](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-sequence/index.html)?:error message. This value may be`null`. |