具有多个服务的 Android BLE Gatt 服务器 - onCharacteristicWriteRequest() 没有用于句柄的字符

标签 android kotlin bluetooth-lowenergy gatt bluetooth-gatt

我正在尝试构建一个具有多个自定义服务和多个特性的 BLE Gatt 服务器。

首先,我使用了 Google 示例:https://github.com/androidthings/sample-bluetooth-le-gattserver/tree/master/kotlin

这很简单并且效果很好。我修改了 UUID 以适合我的,并且我可以毫无问题地接收通知并写入字符。

这是我定义服务和字符的地方:

fun createTimeService(): BluetoothGattService {
        val service = BluetoothGattService(TIME_SERVICE,
                BluetoothGattService.SERVICE_TYPE_PRIMARY)

        // Current Time characteristic
        val currentTime = BluetoothGattCharacteristic(CURRENT_TIME,
                //Read-only characteristic, supports notifications
                BluetoothGattCharacteristic.PROPERTY_READ or BluetoothGattCharacteristic.PROPERTY_NOTIFY,
                BluetoothGattCharacteristic.PERMISSION_READ)
        val configDescriptor = BluetoothGattDescriptor(CLIENT_CONFIG,
                //Read/write descriptor
                BluetoothGattDescriptor.PERMISSION_READ or BluetoothGattDescriptor.PERMISSION_WRITE)
        currentTime.addDescriptor(configDescriptor)

        // Local Time Information characteristic
        val localTime = BluetoothGattCharacteristic(LOCAL_TIME_INFO,
                BluetoothGattCharacteristic.PROPERTY_WRITE,
                BluetoothGattCharacteristic.PERMISSION_WRITE)

        service.addCharacteristic(currentTime)
        service.addCharacteristic(localTime)

        return service
    }

    fun createSerialService(): BluetoothGattService {
        val service = BluetoothGattService(serialPortServiceID,
                BluetoothGattService.SERVICE_TYPE_PRIMARY)

        val serialData = BluetoothGattCharacteristic(serialDataCharacteristicID,
        BluetoothGattCharacteristic.PROPERTY_WRITE,
        BluetoothGattCharacteristic.PERMISSION_WRITE)

        service.addCharacteristic(serialData)

        return service
    }

在这里,我将它们应用到我的服务器:

 private fun startServer() {
        bluetoothGattServer = bluetoothManager.openGattServer(this, gattServerCallback)

        bluetoothGattServer?.addService(TimeProfile.createTimeService())
                ?: Log.w(TAG, "Unable to create GATT server")

        bluetoothGattServer?.addService(TimeProfile.createSerialService())
                ?: Log.w(TAG, "Unable to create GATT server")

        // Initialize the local UI
        updateLocalUi(System.currentTimeMillis())
    }

我希望添加第二个服务后一切都会像以前一样工作。但现在,如果我尝试编写/订阅任何特征(无论在哪个服务中),我都会收到:

W/BluetoothGattServer: onCharacteristicWriteRequest() no char for handle 42
W/BluetoothGattServer: onDescriptorWriteRequest() no desc for handle 43

最佳答案

我发现出了什么问题。显然你不能像我一样一次性添加所有服务。在确认第一个服务之前添加第二个服务会导致将服务设置为 null 的异常。

最后我通过最初仅添加一项服务解决了这个问题。 然后在BleGattServerCallback()的onServiceAdded()回调中我就陆续启动了。

关于具有多个服务的 Android BLE Gatt 服务器 - onCharacteristicWriteRequest() 没有用于句柄的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56855046/

相关文章:

android - 如何在回收站 View 行中添加动态 View

android - Jetpack Compose 中没有涟漪效应

objective-c - 从 (CBPeripheral *)aPeripheral 的 NSUUID 到 NSString

android - 在 Android 中使用 android plaid sdk 未收到成功回调

android - Android通过NavDirections争论(Safe-Args)将ViewModel发送到Fragment

macos - 将 Macbook 变成 iBeacon

java - 蓝牙低功耗葡萄糖GATT配置文件测量解析值

android - Android 中如何获取当前时间和日期

java - 从网络获取对象队列并遍历上一个/下一个

java - 将 Android 平板电脑与蓝牙设备配对后使用 logcat