BluetoothGattServerCallback:onCharacteristicReadRequest()多次调用

问题描述 投票:1回答:1

我有一个Android手机充当中央设备,另一个Android手机充当外围设备。

从Central,我正在请求使用mBluetoothGatt.readCharacteristic ( characteristic )读取Peripheral的特性

在外围设备上,方法

void onCharacteristicReadRequest ( // Bluetooth GATT Server Callback Method
            BluetoothDevice device,
            int requestId,
            int offset,
            BluetoothGattCharacteristic characteristic
)

调用,我在做两件事:1。初始化byte [] valuebyte形式存储String。 2.使用方法mGattServer.sendResponse()向客户发送响应。

@Override public void onCharacteristicReadRequest (
        BluetoothDevice device, int requestId, int offset,
        BluetoothGattCharacteristic characteristic ) {
    super.onCharacteristicReadRequest ( device, requestId, offset, characteristic );
    String string = "This is a data to be transferred";
    byte[] value = string.getBytes ( Charset.forName ( "UTF-8" ) );
    mGattServer.sendResponse ( device, requestId, BluetoothGatt.GATT_SUCCESS, 0, value );
}

我的问题是,当我从中央设备发出一次读取请求时,上面的方法被多次调用。

因此,我在中央设备上获取数据

This is a data to be transferredThis is a data to be transferredThis is a...

我也尝试过其他特色。但对于那些,回调方法被调用一次。

你能指出我做错的地方吗?

编辑:从外设,我已调试我发送32字节的数据。当我将string值从This is a data to be transferred更改为DATA时,onCharacteristicReadRequest()方法仅调用一次。

使用字段string的进一步实验使我得出结论,响应中要发送的最大字节数是21字节。如果是这种情况,那么我该如何从GATT服务器发送响应,以便中央设备只能获得准确的数据?

android bluetooth-lowenergy bluetooth-gatt
1个回答
0
投票

这来自C#,但我认为它可以提供帮助。

byte[] result = DataBytes.Skip(offset).ToArray();
gattServer.SendResponse(device, requestId, GattStatus.Success, offset, result);
© www.soinside.com 2019 - 2024. All rights reserved.