在 Kotlin 中制作函数块

2024-04-06

我很高兴这可能已经得到解答,但我无法找到适合我的解决方案。

Tl;dr:如何制作功能块?

我有以下用 Kotlin 为 Android API 28 编写的 BLE 相关代码。

override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) {

    for (gattService: BluetoothGattService in gatt!!.services) {

        for (gattChar: BluetoothGattCharacteristic in gattService.characteristics) {

                if (gattChar.uuid.toString().contains(ADC_SAMPLESET_0) && !subscribed_0) {

                    subscribed_0 = true

                    gatt.setCharacteristicNotification(gattChar, true)                   

                    val descriptor = gattChar.getDescriptor(
                            UUID.fromString(BleNamesResolver.CLIENT_CHARACTERISTIC_CONFIG)
                    )
                    descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
                    gatt.writeDescriptor(descriptor)
                }

上面的 if 语句重复多次,以便于订阅多个 BLE 特性。不幸的是,gatt.writeDescriptor()函数异步运行。我需要等它返回再打电话gatt.writeDescriptor()对于下一个特征。我该如何实现这一目标?

我尝试过使用runBlocking and GlobalScope.launch in kotlinx.coroutines.experimental.*但我并不完全确定它们是正确的。

谢谢, 亚当


The onDescriptorWrite()方法可能会有所帮助。你应该已经覆盖它了。

请尝试以下操作:

private var canContinue = false;

override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) { //gatt shouldn't be null, so the null-safe ? isn't needed
    loopAsync(gatt);
}

override fun onDescriptorWrite(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) {
    canContinue = true; //allow the loop to continue once a descriptor is written
}

private fun loopAsync(gatt: BluetoothGatt) {
    async { //Run it async
        gatt.services.forEach { gattService -> //Kotlin has a handy Collections.forEach() extension function
            gattService.characteristics.forEach { gattChar -> //Same for this one
                if (gattChar.uuid.toString().contains(ADC_SAMPLESET_0) && !subscribed_0) {
                    subscribed_0 = true

                    gatt.setCharacteristicNotification(gattChar, true)

                    val descriptor = gattChar.getDescriptor(
                            UUID.fromString(BleNamesResolver.CLIENT_CHARACTERISTIC_CONFIG)
                    }
                    descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
                    gatt.writeDescriptor(descriptor)
                    while(!canContinue); //wait until canContinue becomes true and then continue
                }
            }
        }
    }
}

这有点hacky。可能有一种方法可以通过递归来做到这一点,但是嵌套的 for 循环使这变得很棘手。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Kotlin 中制作函数块 的相关文章

随机推荐

  • Angularjs jquery UI 自动完成

    我正在尝试在 Angular 指令中实现 jquery 的自动完成功能 我收到的源数据来自 websocket 响应 它不起作用 我认为响应延迟导致了这里的问题 如果有人能解释下面的代码 我将不胜感激 是否有任何优雅的技术可以使用某种请求
  • 创建新的 django 项目时出现 Pydev 错误

    每次我使用钛 eclipse 和 pydev 创建一个新的 django 项目时 我都会收到此错误 实际上 它确实创建了文件settings py 我想知道是什么导致了这个错误以及如何修复它 这实际上是 Django 1 4 的 PyDev
  • 枚举程序集的所有已安装版本(在 GAC 中)

    是否可以使用 C 枚举 GAC 中程序集的所有已安装版本 例如 我有一个名为 My Assembly 的程序集 该程序集可能有各种版本 1 0 0 0 2 3 4 5 0 1 2 4 等 并且可以针对各种平台 x86 x64 任何 CPU
  • HTML 输入不更新值

    我有一个非常奇怪的问题 至少对我来说 我动态创建文本框 效果很好 但问题是 当我尝试写入这些内容时 如果我检查 html 代码 我写入的值不会出现 我不知道为什么会发生这种情况 也不知道如何解决这个问题 这是我的代码示例 ul class
  • 如何重置reactiveValues?

    重置单个无功值只需通过reactiveVal NULL 然而怎样才能彻底重置reactiveValues 虚拟应用程序包含我的一些方法来保留新鲜和干净的反应值 但它们都没有真正做到我希望它们做的事情 此外 观察时似乎有一种奇怪的行为reac
  • Geotools:wgs84 中缓冲区的边界框

    我需要一个 Java 函数来生成缓冲区周围的边界框 矩形 缓冲区由中心点 WGS84 坐标 和半径 以米为单位 定义 在 ITS 中获取缓冲区的边界框似乎非常简单 Point center Geometry boundingBox cent
  • jQuery 无法识别动态添加的 HTML

    阅读这里的其他一些内容 这个类似的问题 但我不确定如何将其应用于我的困境 我有一个 jquery 函数可以替换列表中的一些 HTML 例如 在函数运行之前 ul li blah blah blah li li blah blah blah
  • 如何在 R markdown SQL 块中使用 SQL 参数

    在 Rmd SQL 代码块中包含参数的正确方法是什么 这是一个 Rmd 文件示例 title Rmd Example output html document df print paged pdf document default r Sy
  • 如果 spring.mail.host 不在 application.properties 中,则 JavaMailSenderImpl 自动装配错误

    我在使用 JavaMailSenderImpl 在 Spring Boot 应用程序中发送电子邮件时遇到了一些 问题 我正在尝试动态设置所有属性 我希望将来从数据库中读取它们 但是 由于我不知道的原因 自动装配 JavaMailSender
  • Excel进程的最大数量?

    如果我这样做 例如 for int i 0 i lt 22 i var app new Excel Application 然后创建22个excel进程 但是 如果我这样做 for int i 0 i lt 25 i var app new
  • java中的DAO模式什么是业务对象

    直接来自this http www oracle com technetwork java dataaccessobject 138824 htmlOracle 关于 J2EE DAO 模式的文章 事实上 除了业务对象 参与者 他们这么称呼
  • 定时器可以提早吗?

    显然 System Threading Timer 回调应该会延迟一点 然而 可以提前调用吗 例如 如果您启动秒表并安排计时器在 1000 毫秒内运行回调 那么秒表是否有可能在回调中显示 999 或者我们可以指望它必须显示 1000 或更多
  • 收到警告“NDK 缺少“平台”目录。”没有 NDK [重复]

    这个问题在这里已经有答案了 我有一个仅使用 Android SDK 而不是 NDK 的项目 但每当我使用 gradle 构建时都会收到有关 NDK 的警告 NDK is missing a platforms directory If yo
  • 为什么说微软堆栈成本高? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 在 SQLite 上连接表时如何进行更新?

    我试过 UPDATE closure JOIN item ON item id id SET checked 0 WHERE ancestor id 1 And UPDATE closure item SET checked 0 WHERE
  • 穷人的 SQL 枢轴。将每个用户的问题和答案列在一行中

    当前查询 SELECT order id AS OrderNumber ordName ordLastName question answer FROM cart survey JOIN orders ON cart survey orde
  • ASP.NET MVC 2.0 Prev 1 和 SPARK?

    我正在尝试将 ASP NET MVC 1 0 应用程序升级到 2 0 预览版 1 我使用 Spark 作为视图引擎 问题 Spark 使用 System Web MVC 1 0 0 0 因此我获取了源代码并使用 2 0 0 0 重新编译并使
  • 无法解析 ACRA 4.7.0 中的方法“formKey”

    我今天尝试为我的 Android 项目设置 acra 但没有成功 我按照说明操作 在 gradle 中导入了 acra lib 编译 ch acra acra 4 7 0 然后我添加了这个 ReportsCrashes formKey ma
  • 告诉 urllib2 使用自定义 DNS

    我想告诉urllib2 urlopen or a 定制开瓶器 使用127 0 0 1 or 1 来解析地址 我不会改变我的 etc resolv conf 然而 一种可能的解决方案是使用类似的工具dnspython查询地址和httplib构
  • 在 Kotlin 中制作函数块

    我很高兴这可能已经得到解答 但我无法找到适合我的解决方案 Tl dr 如何制作功能块 我有以下用 Kotlin 为 Android API 28 编写的 BLE 相关代码 override fun onServicesDiscovered