با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
تقریباً هر تجربه چند دستگاهی با یافتن دستگاه های موجود آغاز می شود. برای سادهسازی این کار رایج، Device Discovery API را ارائه میکنیم.
شکل 1 : اشتراک گذاری با کاربران اطراف.
گفتگوی انتخاب دستگاه را اجرا کنید
Device Discovery از یک گفتگوی سیستم استفاده می کند تا به کاربر اجازه دهد دستگاه مورد نظر را انتخاب کند. برای شروع گفتگوی انتخاب دستگاه، ابتدا باید یک کلاینت کشف دستگاه دریافت کنید و یک گیرنده نتیجه را ثبت کنید. توجه داشته باشید که مشابه registerForActivityResult ، این گیرنده باید بدون قید و شرط به عنوان بخشی از مسیر اولیه فعالیت یا قطعه ثبت شود.
در قطعه کد بالا، یک شی handleDevices تعریف نشده داریم. پس از اینکه کاربر دستگاههایی را برای اتصال انتخاب کرد و پس از اینکه SDK با موفقیت به دستگاههای دیگر متصل شد، این پاسخ تماس فهرستی از Participants انتخاب شده را دریافت میکند.
کاتلین
handleDevices=OnDevicePickerResultListener{participants->participants.forEach{// Use participant info}}
جاوا
handleDevices=participants->{for(Participantparticipant:participants){// Use participant info}}
پس از اینکه انتخابگر دستگاه ثبت شد، آن را با استفاده از نمونه devicePickerLauncher راه اندازی کنید. DevicePickerLauncher.launchDevicePicker دو پارامتر را می گیرد - یک لیست از فیلترهای دستگاه (به بخش زیر مراجعه کنید) و یک startComponentRequest . startComponentRequest برای نشان دادن اینکه کدام فعالیت باید در دستگاه دریافت کننده شروع شود و دلیل درخواستی که به کاربر نشان داده می شود استفاده می شود.
کاتلین
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 مدیریت کرد.
کاتلین
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)}
فیلترهای دستگاه
هنگام کشف دستگاهها، معمول است که بخواهید این دستگاهها را فیلتر کنید تا فقط موارد مربوط به مورد استفاده را نشان دهید. به عنوان مثال:
فیلتر کردن فقط برای دستگاههای دارای دوربین برای کمک به اسکن کد QR
فیلتر کردن فقط روی تلویزیون ها برای تجربه تماشای صفحه بزرگ
برای این پیشنمایش توسعهدهنده، ما با قابلیت فیلتر کردن به دستگاههای متعلق به یک کاربر شروع میکنیم.
می توانید فیلتر دستگاه را با استفاده از کلاس DeviceFilter مشخص کنید:
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 و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی."],[],[],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."]]