ほぼすべてのマルチデバイス エクスペリエンスは、利用可能なデバイスを見つけることから始まります。宛先 この一般的なタスクを簡素化するために、Device Discovery API を提供しています。

デバイス選択ダイアログを起動する
デバイスの検出では、システム ダイアログを使用してユーザーが対象デバイスを選択できるようにします。宛先
デバイス選択ダイアログを開始するには、まずデバイス検出を
結果レシーバを登録します。なお、
registerForActivityResult
: このレシーバは無条件に登録する必要があります。
初期化パスの一部である。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
devicePickerLauncher = Discovery.create(this).registerForResult(this, handleDevices)
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
devicePickerLauncher = Discovery.create(this).registerForResult(this, handleDevices);
}
上記のコード スニペットには、未定義の handleDevices
オブジェクトがあります。変更後
ユーザーが接続するデバイスを選択し、SDK が正常に処理されると、
他のデバイスに接続すると、このコールバックは Participants
のリストを受け取ります。
選択済みです。
handleDevices = OnDevicePickerResultListener { participants -> participants.forEach {
// Use participant info
}
}
handleDevices = participants -> {
for (Participant participant : participants) {
// Use participant info
}
}
デバイス選択ツールを登録したら、devicePickerLauncher
を使用して起動します。
構成されますDevicePickerLauncher.launchDevicePicker
は 2 つのパラメータを取ります。
デバイス フィルタのリスト(以下のセクションを参照)と startComponentRequest
。「
startComponentRequest
は、どのアクティビティを開始するかを示すために使用されます。
そして、ユーザーに提示されたリクエストの理由です。
devicePickerLauncher.launchDevicePicker(
listOf(),
startComponentRequest {
action = "com.example.crossdevice.MAIN"
reason = "I want to say hello to you"
},
)
devicePickerLauncher.launchDevicePickerFuture(
Collections.emptyList(),
new StartComponentRequest.Builder()
.setAction("com.example.crossdevice.MAIN")
.setReason("I want to say hello to you")
.build());
接続リクエストを承認する
ユーザーがデバイス選択ツールでデバイスを選択すると、
接続を受け入れるようユーザーに要求します。承諾すると、
ターゲット アクティビティが起動される。これは onCreate
と
onNewIntent
。
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)
}
@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 コードのスキャン用にカメラ搭載デバイスのみに絞り込む
- 大画面での視聴体験のためにテレビのみに絞り込む
このデベロッパー プレビューでは、まず以下をフィルタできます。 同じユーザーが所有するデバイスのみに適用されます。
クラス DeviceFilter
を使用して、デバイス フィルタを指定できます。
val deviceFilters = listOf(DeviceFilter.trustRelationshipFilter(MY_DEVICES_ONLY))
List<DeviceFilter> deviceFilters =
Arrays.asList(DeviceFilter.trustRelationshipFilter(MY_DEVICES_ONLY));
デバイス フィルタを定義したら、デバイスの検出を開始できます。
devicePickerLauncher.launchDevicePicker(deviceFilters, startComponentRequest)
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
キーワード。