Android NDK开发详解连接性之Device discovery API

Android NDK开发详解连接性之Device discovery API

几乎所有的多设备体验都是从查找可用设备开始的。为了简化这项常见任务,我们提供了 Device Discovery API。
包含与附近用户分享选项的对话框
图 1:与附近的用户分享。

启动设备选择对话框

设备发现功能使用系统对话框来让用户选择目标设备。如需启动设备选择对话框,您首先需要获取设备发现客户端并注册结果接收器。请注意,与 registerForActivityResult 类似,此接收器必须无条件地注册为 activity 或 fragment 初始化路径的一部分。
注意 :如果用户收到已卸载应用的请求,则需要安装该应用并再次尝试发出请求。未来,我们希望利用 Play 商店 API 来延续现有的意图。
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 用于指示应在接收设备上启动哪个 activity,以及向用户显示请求的原因。
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());

接受连接请求

当用户在设备选择器中选择设备时,接收设备上会显示一个对话框,要求用户接受连接。被接受后,系统会启动目标 activity,您可以在 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)
}

设备过滤条件

注意 :此功能将在后续版本的开发者预览版中提供。

发现设备时,通常希望对这些设备进行过滤,以仅显示与当前用例相关的设备。例如:

仅过滤出带相机的设备,以便扫描二维码
过滤出仅电视的内容,畅享大屏幕观看体验

对于此开发者预览版,我们首先能够过滤属于同一用户的设备。

您可以使用 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 关键字的异步函数。

如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。

最后更新时间 (UTC):2023-11-02。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五一编程

程序之路有我与你同行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值