几乎所有的多设备体验都始于寻找可用的设备。接收者 可简化这项常见任务,因此我们提供了 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
接受两个参数:
设备过滤器列表(请参阅下文)和 startComponentRequest
。通过
startComponentRequest
用于指示应在哪个 activity 上启动
接收方设备,以及向用户显示的请求原因。
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());
接受连接请求
当用户在设备选择器中选择设备时,
请求用户接受连接。一旦接受,
启动了目标 activity,这可在 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)
}
设备过滤条件
在发现设备时,您通常希望过滤这些设备, 显示与当前用例相关的选项。例如:
- 过滤出带有摄像头的设备,以帮助扫描二维码
- 进行过滤,仅显示电视,以提供大屏幕观看体验
对于此开发者预览版,我们首先提供过滤功能 同一用户拥有的设备。
您可以使用 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
个关键字。