幾乎所有裝置相關體驗都從尋找可用的裝置開始。為簡化此常見工作,我們提供 Device Discovery API。
啟動裝置選取對話方塊
裝置探索功能會使用系統對話方塊,讓使用者選取目標裝置。如要啟動裝置選取對話方塊,您必須先取得裝置用戶資產評估器,並註冊結果接收器。請注意,與 registerForActivityResult
類似,這個接收器必須無條件註冊為活動或片段初始化路徑的一部分。
Kotlin
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) devicePickerLauncher = Discovery.create(this).registerForResult(this, handleDevices) }
Java
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); devicePickerLauncher = Discovery.create(this).registerForResult(this, handleDevices); }
在上方的程式碼片段中,我們有未定義的 handleDevices
物件。使用者選擇要連接的裝置,且 SDK 成功連線至其他裝置後,此回呼就會接收選取的 Participants
清單。
Kotlin
handleDevices = OnDevicePickerResultListener { participants -> participants.forEach { // Use participant info } }
Java
handleDevices = participants -> { for (Participant participant : 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" }, )
Java
devicePickerLauncher.launchDevicePickerFuture( Collections.emptyList(), new StartComponentRequest.Builder() .setAction("com.example.crossdevice.MAIN") .setReason("I want to say hello to you") .build());
接受連線要求
使用者在裝置挑選器中選取裝置時,接收端裝置會顯示對話方塊,要求使用者接受連線。接受後,系統就會啟動目標活動,並可在 onCreate
和 onNewIntent
中處理。
Kotlin
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) handleIntent(getIntent()) } override fun onNewIntent(intent: Intent) { super.onNewIntent(intent) handleIntent(intent) } private fun handleIntent(intent: Intent) { val participant = Discovery.create(this).getParticipantFromIntent(intent) // Accept connection from participant (see below) }
Java
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); handleIntent(getIntent()); } @Override public void onNewIntent(Intent intent) { super.onNewIntent(intent); handleIntent(intent); } private void handleIntent(Intent intent) { Participant participant = Discovery.create(this).getParticipantFromIntent(intent); // Accept connection from participant (see below) }
裝置篩選器
尋找裝置時,我們通常會想要篩選這些裝置,只顯示與目前用途相關的裝置。例如:
- 篩選僅篩選出有相機的裝置,以便掃描 QR code
- 篩選僅透過電視觀看,以便享受大螢幕觀看體驗
在這個開發人員預覽版中,我們首先要提供篩選同一位使用者裝置的功能。
您可以使用 DeviceFilter
類別指定裝置篩選器:
Kotlin
val deviceFilters = listOf(DeviceFilter.trustRelationshipFilter(MY_DEVICES_ONLY))
Java
List<DeviceFilter> deviceFilters = Arrays.asList(DeviceFilter.trustRelationshipFilter(MY_DEVICES_ONLY));
定義裝置篩選條件後,即可開始探索裝置。
Kotlin
devicePickerLauncher.launchDevicePicker(deviceFilters, startComponentRequest)
Java
Futures.addCallback( devicePickerLauncher.launchDevicePickerFuture(deviceFilters, startComponentRequest), new FutureCallback<Void>() { @Override public void onSuccess(Void result) { // do nothing, result will be returned to handleDevices callback } @Override public void onFailure(Throwable t) { // handle error } }, mainExecutor);
請注意,launchDevicePicker
是使用 suspend
關鍵字的非同步函式。