devicePickerLauncher.launchDevicePicker(listOf(),startComponentRequest{action="com.example.crossdevice.MAIN"reason="I want to say hello to you"},)
Java
devicePickerLauncher.launchDevicePickerFuture(Collections.emptyList(),newStartComponentRequest.Builder().setAction("com.example.crossdevice.MAIN").setReason("I want to say hello to you").build());
overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)handleIntent(getIntent())}overridefunonNewIntent(intent:Intent){super.onNewIntent(intent)handleIntent(intent)}privatefunhandleIntent(intent:Intent){valparticipant=Discovery.create(this).getParticipantFromIntent(intent)// Accept connection from participant (see below)}
Java
@OverridepublicvoidonCreate(@NullableBundlesavedInstanceState){super.onCreate(savedInstanceState);handleIntent(getIntent());}@OverridepublicvoidonNewIntent(Intentintent){super.onNewIntent(intent);handleIntent(intent);}privatevoidhandleIntent(Intentintent){Participantparticipant=Discovery.create(this).getParticipantFromIntent(intent);// Accept connection from participant (see below)}
Futures.addCallback(devicePickerLauncher.launchDevicePickerFuture(deviceFilters,startComponentRequest),newFutureCallback<Void>(){@OverridepublicvoidonSuccess(Voidresult){// do nothing, result will be returned to handleDevices callback}@OverridepublicvoidonFailure(Throwablet){// handle error}},mainExecutor);
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Device discovery API\n\nAlmost every multidevice experience begins with finding available devices. To\nsimplify this common task, we offer the Device Discovery API. \n**Figure 1**: Share with nearby users.\n\n\u003cbr /\u003e\n\nLaunch the device selection dialog\n----------------------------------\n\nDevice discovery uses a system dialog to let the user select a target device. To\ninitiate the device selection dialog, you first need to get a device discovery\nclient and register a result receiver. Note that similar to\n`registerForActivityResult`, this receiver must be registered unconditionally as\npart of the activity or fragment initialization path.\n**Note:** If a user receives a request from an uninstalled app, they'll need to install it and try the request again. In the future, we hope to take advantage of Play Store APIs to continue the existing intent. \n\n### Kotlin\n\n```kotlin\noverride fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n devicePickerLauncher = Discovery.create(this).registerForResult(this, handleDevices)\n}\n```\n\n### Java\n\n```java\n@Override\npublic void onCreate(@Nullable Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n devicePickerLauncher = Discovery.create(this).registerForResult(this, handleDevices);\n}\n```\n\nIn the code snippet above, we have an undefined `handleDevices` object. After\nthe user chooses the devices to connect to, and once the SDK successfully\nconnects to the other devices, this callback receives the list of `Participants`\nselected. \n\n### Kotlin\n\n```kotlin\nhandleDevices = OnDevicePickerResultListener { participants -\u003e participants.forEach {\n // Use participant info\n }\n}\n```\n\n### Java\n\n```java\nhandleDevices = participants -\u003e {\n for (Participant participant : participants) {\n // Use participant info\n }\n}\n```\n\nAfter the device picker is registered, launch it using the `devicePickerLauncher`\ninstance. `DevicePickerLauncher.launchDevicePicker` takes two parameters -- a\nlist of device filters (see section below) and a `startComponentRequest`. The\n`startComponentRequest` is used to indicate which activity should be started on\nthe receiving device, and the reason for the request that is shown to the user. \n\n### Kotlin\n\n```kotlin\ndevicePickerLauncher.launchDevicePicker(\n listOf(),\n startComponentRequest {\n action = \"com.example.crossdevice.MAIN\"\n reason = \"I want to say hello to you\"\n },\n)\n```\n\n### Java\n\n```java\ndevicePickerLauncher.launchDevicePickerFuture(\n Collections.emptyList(),\n new StartComponentRequest.Builder()\n .setAction(\"com.example.crossdevice.MAIN\")\n .setReason(\"I want to say hello to you\")\n .build());\n```\n\nAccept connection requests\n--------------------------\n\nWhen the user selects a device in the device picker, a dialog appears on the\nreceiving device to ask the user to accept the connection. Once accepted, the\ntarget activity is launched, which can be handled in `onCreate` and\n`onNewIntent`. \n\n### Kotlin\n\n```kotlin\noverride fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n handleIntent(getIntent())\n}\n\noverride fun onNewIntent(intent: Intent) {\n super.onNewIntent(intent)\n handleIntent(intent)\n}\n\nprivate fun handleIntent(intent: Intent) {\n val participant = Discovery.create(this).getParticipantFromIntent(intent)\n // Accept connection from participant (see below)\n}\n```\n\n### Java\n\n```java\n@Override\npublic void onCreate(@Nullable Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n handleIntent(getIntent());\n}\n\n@Override\npublic void onNewIntent(Intent intent) {\n super.onNewIntent(intent);\n handleIntent(intent);\n}\n\nprivate void handleIntent(Intent intent) {\n Participant participant = Discovery.create(this).getParticipantFromIntent(intent);\n // Accept connection from participant (see below)\n}\n```\n\nDevice filters\n--------------\n\n| **Note:** This feature will be available in a later version of the developer preview.\n\nWhen discovering devices, it's common to want to filter these devices to only\nshow the ones relevant to the use case at hand. For example:\n\n- Filtering to only devices with camera to help scan a QR code\n- Filtering to only TVs for a big screen viewing experience\n\nFor this developer preview, we are starting with the ability to filter to\ndevices owned by the same user.\n\nYou can specify the device filter using class `DeviceFilter`: \n\n### Kotlin\n\n```kotlin\nval deviceFilters = listOf(DeviceFilter.trustRelationshipFilter(MY_DEVICES_ONLY))\n```\n\n### Java\n\n```java\nList\u003cDeviceFilter\u003e deviceFilters =\n Arrays.asList(DeviceFilter.trustRelationshipFilter(MY_DEVICES_ONLY));\n```\n\nOnce you define the device filters, you can initiate device discovery. \n\n### Kotlin\n\n```kotlin\ndevicePickerLauncher.launchDevicePicker(deviceFilters, startComponentRequest)\n```\n\n### Java\n\n```java\nFutures.addCallback(\n devicePickerLauncher.launchDevicePickerFuture(deviceFilters, startComponentRequest),\n new FutureCallback\u003cVoid\u003e() {\n @Override\n public void onSuccess(Void result) {\n // do nothing, result will be returned to handleDevices callback\n }\n\n @Override\n public void onFailure(Throwable t) {\n // handle error\n }\n },\n mainExecutor);\n```\n\nNotice that the `launchDevicePicker` is an asynchronous function that uses the\n`suspend` keyword."]]