거의 모든 멀티스크린 환경은 사용 가능한 기기를 찾는 것으로 시작합니다. 받는사람
Google은 Device Discovery API를 제공합니다.
<ph type="x-smartling-placeholder"></ph>
<ph type="x-smartling-placeholder"></ph>
그림 1: 주변 사용자와 공유하기.
기기 선택 대화상자 실행
기기 검색은 시스템 대화상자를 사용하여 사용자가 대상 기기를 선택할 수 있도록 합니다. 받는사람
기기 선택 대화상자 시작, 먼저 기기 검색을 가져와야 함
결과 수신자를 등록합니다. 참고:
registerForActivityResult, 이 수신자는 무조건
일부에 호출될 수 있습니다.
위의 코드 스니펫에는 정의되지 않은 handleDevices 객체가 있습니다. 후(After)
사용자가 연결할 기기를 선택하고 SDK가 성공적으로
다른 기기에 연결되면 이 콜백은 Participants 목록을 수신합니다.
선택합니다.
Kotlin
handleDevices=OnDevicePickerResultListener{participants->participants.forEach{// Use participant info}}
자바
handleDevices=participants->{for(Participantparticipant:participants){// Use participant info}}
기기 선택 도구가 등록되면 devicePickerLauncher를 사용하여 실행합니다.
인스턴스를 만들 수 있습니다 DevicePickerLauncher.launchDevicePicker는
기기 필터 (아래 섹션 참고) 및 startComponentRequest의 목록. 이
startComponentRequest는 시작되어야 하는 활동을 나타내는 데 사용됩니다.
수신 기기, 사용자에게 표시된 요청 이유
Kotlin
devicePickerLauncher.launchDevicePicker(listOf(),startComponentRequest{action="com.example.crossdevice.MAIN"reason="I want to say hello to you"},)
자바
devicePickerLauncher.launchDevicePickerFuture(Collections.emptyList(),newStartComponentRequest.Builder().setAction("com.example.crossdevice.MAIN").setReason("I want to say hello to you").build());
연결 요청 수락
사용자가 기기 선택 도구에서 기기를 선택하면
연결 수락을 요청합니다. 승인되면
타겟 활동이 실행되며 이는 onCreate에서 처리할 수 있습니다.
onNewIntent
Kotlin
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)}
자바
@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);
launchDevicePicker는
suspend 키워드
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[null,null,["최종 업데이트: 2025-07-27(UTC)"],[],[],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."]]