简体   繁体   English

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

[英]Android BLE Gatt Server with multiple services - onCharacteristicWriteRequest() no char for handle

I am trying to build a BLE Gatt Server with multiple custom services and multiple characteristics.我正在尝试构建具有多个自定义服务和多个特征的 BLE Gatt 服务器。

To begin with I used the Google Example: https://github.com/androidthings/sample-bluetooth-le-gattserver/tree/master/kotlin首先,我使用了 Google 示例: https : //github.com/androidthings/sample-bluetooth-le-gattserver/tree/master/kotlin

This was straight forward and worked very well.这很简单,而且效果很好。 I modified the UUIDs to fit mine and I could receive notifications and write to the chars with no problem.我修改了 UUID 以适应我的情况,我可以毫无问题地接收通知并写入字符。

This is where I define the services and chars:这是我定义服务和字符的地方:

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
    }

And here I am applying them to my server:在这里,我将它们应用于我的服务器:

 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())
    }

I would expect that everything would be working like before after adding the second service.我希望在添加第二个服务后一切都会像以前一样工作。 But now if I try to write/subscribe to any of the characteristics (doesn't matter in which service) I just receive this:但是现在,如果我尝试编写/订阅任何特征(在哪个服务中无关紧要),我只会收到以下信息:

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

I found what was going wrong.我发现出了什么问题。 Apparently you cannot just add all services at once like I did.显然,您不能像我一样一次性添加所有服务。 Adding the second service before the first one was confirmed lead to an Exception setting the services to null.在确认第一个服务之前添加第二个服务会导致将服务设置为空的异常。

In the end I solved this by adding only one service initially.最后我通过最初只添加一项服务来解决这个问题。 Then in the onServiceAdded() Callback of the BleGattServerCallback() I started one after another.然后在 BleGattServerCallback() 的 onServiceAdded() 回调中,我一个接一个地开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM