首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android :在一个服务下从多个特性发出通知

Android :在一个服务下从多个特性发出通知
EN

Stack Overflow用户
提问于 2020-01-23 23:02:17
回答 2查看 1.6K关注 0票数 0

问题

我正在尝试通过BLE从多个特性获得通知,我在互联网上看到了一些解决方案,需要等到onDescriptorWrite()回调完成(我认为我在这里做了吗?),但是我不能第二次为FILE_TX (下面的代码)通知做onDescriptorWrite()。所有这些都是在onServicesDiscovery()下执行的--当我建立BLE连接时。

我在这里做错什么了吗?

一次只能有一个出色的关贸总协定操作。在这种情况下,在等待到第一个调用完成之前,您需要执行两个writeDescriptor调用。您必须等待https://developer.android.com/reference/android/bluetooth/BluetoothGattCallback.html#onDescriptorWrite(android.bluetooth.BluetoothGatt,直到您可以发送下一个。

这是我能找到的最好的答案,但是你怎么知道onDescriptorWrite已经完成了呢?

我试着把Thread.sleep(500)放在中间工作,但我也不工作。

在onServicesDiscovery - gattCallback下

代码语言:javascript
复制
for (gattCharacteristic in gattCharacteristics) {
                        uuid = gattCharacteristic.uuid
                        // // Log.d("GATT", "$uuid")

                        if (gattCharacteristic.uuid.equals(UUID_CHARACTERISTIC_READ_FILE_TX)) {
                            gatt.setCharacteristicNotification(gattCharacteristic, true)
                            val descriptorfile: BluetoothGattDescriptor = gattCharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID) ?: error("Required Client Characteristic Configuration not found")
                            descriptorfile.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)
                            isSuccess = gatt.writeDescriptor(descriptorfile)
                            Log.d("tagfile", "FILE_TX Successful ? " + isSuccess)
                            gattCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE)
                            Log.d("tagfile", "Found Transparent service File Tx characteristics")
                        }

                        else if (gattCharacteristic.uuid.equals(UUID_CHARACTERISTIC_TX)) {
                            gatt.setCharacteristicNotification(gattCharacteristic, true)
                            val descriptor: BluetoothGattDescriptor = gattCharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID) ?: error("Required Client Characteristic Configuration not found")
                            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)
                            isSuccess = gatt.writeDescriptor(descriptor)
                            Log.d("tagfile", "TX Successful ? " + isSuccess)
                            gattCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE)
                            Log.d("tagfile", "Found Transparent service Tx characteristics")
                        }

                        if (gattCharacteristic.uuid.equals(UUID_CHARACTERISTIC_RX)) {
                            gattCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE)
                            Log.d("tagfile", "Found Transparent service Rx characteristics")
                        }

                        else if (gattCharacteristic.uuid.equals(UUID_CHARACTERISTIC_READ_FILE_RX)) {
                            gattCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE)
                            Log.d("tagfile", "Found Transparent service File Rx characteristics")
                        }
}

在onDescriptorWrite - gattCallback下

代码语言:javascript
复制
override fun onDescriptorWrite(
            gatt: BluetoothGatt?,
            descriptor: BluetoothGattDescriptor?,
            status: Int
        ) {

            Log.d("tagfile", "Status of gatt : " + status + "       GATT FAILURE : " + BluetoothGatt.GATT_FAILURE)
        }

结果:

代码语言:javascript
复制
2020-01-24 09:41:51.359 8565-8587/com.example.ricco_ble D/tagfile: TX Successful ? true
2020-01-24 09:41:53.359 8565-8587/com.example.ricco_ble D/tagfile: Found Transparent service Tx characteristics
2020-01-24 09:41:53.360 8565-8587/com.example.ricco_ble D/tagfile: Found Transparent service Rx characteristics
2020-01-24 09:41:53.371 8565-8587/com.example.ricco_ble D/tagfile: FILE_TX Successful ? false
2020-01-24 09:41:53.371 8565-8587/com.example.ricco_ble D/tagfile: Found Transparent service File Tx characteristics
2020-01-24 09:41:53.372 8565-8587/com.example.ricco_ble D/tagfile: Found Transparent service File Rx characteristics
2020-01-24 09:41:53.424 8565-8587/com.example.ricco_ble D/tagfile: Status of gatt : 0       GATT FAILURE : 257
EN

回答 2

Stack Overflow用户

发布于 2020-02-06 17:07:08

与其自己构建系统,我建议使用北欧Android BLE库 SemiConductor。我花了四个多月的时间自己去做。这个库允许您使用带有定义回调的异步执行调用,而不必担心这样的问题。

票数 1
EN

Stack Overflow用户

发布于 2020-02-06 19:26:06

在使用Android堆栈时,我发现遵循两点是有用的。

  1. 在任何关贸总协定回调中,只执行结果复制,并安排要执行的下一个任务(而不是要执行的)。以便能够尽快释放回调函数。
  2. 按顺序执行操作。在收到上一次操作的回调之前,不要执行下一次操作。

从上面的代码后,您可以在serviceDiscovery回调中嵌套编写描述符。另外,在接收之前的回调之前,要编写下一个描述符。

要获得更稳定/可预测的性能,可以将代码重构为以下内容

代码语言:javascript
复制
BluetoothGattCallback(){
    onServiceDiscovery(gatt){
        MainHandler.post((gatt)->setFirstNotification(gatt)) //do the scheduling, not direct execution here.
    }

    onDescriptorWrite(gatt){
         if(the first descriptor write success) {
        MainHandler.post((gatt)->setSecondNotification(gatt)) //do the scheduling
         } else if (the second descriptor write success) {
        MainHandler.post((gatt)->otherLogic(gatt)) //do the scheduling
        }
    } 
}

fun setFirstNotification(gatt){
    //your code to set descriptor value
}


fun setSecondNotification(gatt){
    //your code to set descriptor value
}

fun otherLogic(gatt){
    //your other code
}

如果您想直接使用Android堆栈构建通信应用程序,这就是您将如何处理它的想法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59888269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档