我有一个Android手机充当中央设备,另一个Android手机充当外围设备 .
从Central,我正在请求读取外围设备的特性,使用 mBluetoothGatt.readCharacteristic ( characteristic )
在外围设备上,方法
void onCharacteristicReadRequest ( // Bluetooth GATT Server Callback Method
BluetoothDevice device,
int requestId,
int offset,
BluetoothGattCharacteristic characteristic
)
调用,我在做两件事:1 . 初始化 byte [] value
以 byte
形式存储字符串 . 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...
我也尝试过其他特色 . 但对于那些,回调方法被调用一次 .
你能指出我做错的地方吗?
EDIT :从Peripheral,我调试了我发送32字节的数据 . 当我将 This is a data to be transferred
的值从 This is a data to be transferred
更改为 DATA
时,方法 onCharacteristicReadRequest()
仅调用一次 .
使用字段 string
的进一步实验使我得出结论,响应中要发送的最大字节数是21字节 . 如果是这种情况,那么我应该如何从GATT服务器发送响应,以便中央设备只能获得准确的数据?