Skip to content

Most visited

Recently visited

navigation

CompanionDeviceManager

public final class CompanionDeviceManager
extends Object

java.lang.Object
   ↳ android.companion.CompanionDeviceManager


System level service for managing companion devices

To obtain an instance call getSystemService(Class)(COMPANION_DEVICE_SERVICE) Then, call associate(AssociationRequest, Callback, Handler) to initiate the flow of associating current package with a device selected by user.

Instances of this class must be obtained using Context.getSystemService(Class) with the argument CompanionDeviceManager.class or Context.getSystemService(String) with the argument Context.COMPANION_DEVICE_SERVICE.

See also:

Summary

Nested classes

class CompanionDeviceManager.Callback

A callback to receive once at least one suitable device is found, or the search failed (e.g. 

Constants

String EXTRA_DEVICE

A device, returned in the activity result of the IntentSender received in onDeviceFound(IntentSender)

Public methods

void associate(AssociationRequest request, CompanionDeviceManager.Callback callback, Handler handler)

Associate this app with a companion device, selected by user

Once at least one appropriate device is found, callback will be called with a PendingIntent that can be used to show the list of available devices for the user to select.

void disassociate(String deviceMacAddress)

Remove the association between this app and the device with the given mac address.

List<String> getAssociations()

Calling this API requires a uses-feature FEATURE_COMPANION_DEVICE_SETUP declaration in the manifest

boolean hasNotificationAccess(ComponentName component)

Check whether the given component can access the notifications via a NotificationListenerService Your app must have an association with a device before calling this API

Calling this API requires a uses-feature FEATURE_COMPANION_DEVICE_SETUP declaration in the manifest

void requestNotificationAccess(ComponentName component)

Request notification access for the given component.

Inherited methods

From class java.lang.Object

Constants

EXTRA_DEVICE

added in API level 26
String EXTRA_DEVICE

A device, returned in the activity result of the IntentSender received in onDeviceFound(IntentSender)

Constant Value: "android.companion.extra.DEVICE"

Public methods

associate

added in API level 26
void associate (AssociationRequest request, 
                CompanionDeviceManager.Callback callback, 
                Handler handler)

Associate this app with a companion device, selected by user

Once at least one appropriate device is found, callback will be called with a PendingIntent that can be used to show the list of available devices for the user to select. It should be started for result (i.e. using startIntentSenderForResult(IntentSender, int, Intent, int, int, int)), as the resulting Intent will contain extra EXTRA_DEVICE, with the selected device. (e.g. BluetoothDevice)

If your app needs to be excluded from battery optimizations (run in the background) or to have unrestricted data access (use data in the background) you can declare that you use the ERROR(/android.Manifest.permission#RUN_IN_BACKGROUND) and ERROR(/android.Manifest.permission#USE_DATA_IN_BACKGROUND) respectively. Note that these special capabilities have a negative effect on the device's battery and user's data usage, therefore you should requested them when absolutely necessary.

You can call getAssociations() to get the list of currently associated devices, and disassociate(String) to remove an association. Consider doing so when the association is no longer relevant to avoid unnecessary battery and/or data drain resulting from special privileges that the association provides

Calling this API requires a uses-feature FEATURE_COMPANION_DEVICE_SETUP declaration in the manifest

Parameters
request AssociationRequest: specific details about this request

This value must never be null.

callback CompanionDeviceManager.Callback: will be called once there's at least one device found for user to choose from

This value must never be null.

handler Handler: A handler to control which thread the callback will be delivered on, or null, to deliver it on main thread

See also:

disassociate

added in API level 26
void disassociate (String deviceMacAddress)

Remove the association between this app and the device with the given mac address.

Any privileges provided via being associated with a given device will be revoked

Consider doing so when the association is no longer relevant to avoid unnecessary battery and/or data drain resulting from special privileges that the association provides

Calling this API requires a uses-feature FEATURE_COMPANION_DEVICE_SETUP declaration in the manifest

Parameters
deviceMacAddress String: the MAC address of device to disassociate from this app

This value must never be null.

getAssociations

added in API level 26
List<String> getAssociations ()

Calling this API requires a uses-feature FEATURE_COMPANION_DEVICE_SETUP declaration in the manifest

Returns
List<String> a list of MAC addresses of devices that have been previously associated with the current app. You can use these with disassociate(String)

This value will never be null.

hasNotificationAccess

added in API level 26
boolean hasNotificationAccess (ComponentName component)

Check whether the given component can access the notifications via a NotificationListenerService Your app must have an association with a device before calling this API

Calling this API requires a uses-feature FEATURE_COMPANION_DEVICE_SETUP declaration in the manifest

Parameters
component ComponentName: the name of the component

Returns
boolean whether the given component has the notification listener permission

requestNotificationAccess

added in API level 26
void requestNotificationAccess (ComponentName component)

Request notification access for the given component. The given component must follow the protocol specified in NotificationListenerService Only components from the same package as the calling app are allowed. Your app must have an association with a device before calling this API

Calling this API requires a uses-feature FEATURE_COMPANION_DEVICE_SETUP declaration in the manifest

Parameters
component ComponentName

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.