对Android BluetoothGattServerCallback#onCharacteristicWriteRequest方法感到困惑。
对于参数preparedWrite
,我认为实施写操作是我的责任,回调如何知道何时应该对消息进行排队?
文档说An application must call BluetoothGattServer.sendResponse(BluetoothDevice, int, int, int, byte[]) to complete the request.
,如果responseNeeded
为假,该怎么办?
无论我设置为BluetoothGattServer#sendResponse(BluetoothDevice, int, int, int, byte[])
,调用value
似乎总是返回请求中的值。这是预期的吗?
答案 0 :(得分:1)
preparedWrite
用于支持长写(大于MTU)。
BluetoothGattServerCallback#onCharacteristicWriteRequest
与BluetoothGattServerCallback#onExecuteWrite
结合使用以重新组合对等设备发送的分段数据。要回答您的问题:
preparedWrite
为true,即数据需要排队直到更多数据到达。 BluetoothGattServerCallback#onExecuteWrite
将在收到最终片段并且已发送的特性值可以完全组装之后被调用。responseNeeded
为假,请勿致电BluetoothGattServer.sendResponse(BluetoothDevice, int, int, int, byte[])
。只需发送一个空的value
。
以下是onCharacteristicWriteRequest
的示例:
@Override
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value);
Log.d("SVC", "BluetoothGattServerCallback.onCharacteristicWriteRequest with " + value.length + " bytes");
if(preparedWrite) {
handleInputFragment(device, characteristic.getUuid(), value);
} else {
handleInputMessage(device, characteristic.getUuid(), value);
}
if(responseNeeded) {
Log.d("SVC", "sending response to write request for characteristic: " + characteristic.getUuid());
if(!gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, new byte[0])) {
Log.e("SVC", "response to characteristic write request failed");
}
}
}
和onExecuteWrite
@Override
public void onExecuteWrite(BluetoothDevice device, int requestId, boolean execute) {
Log.d("SVC", "BluetoothGattServerCallback.onExecuteWrite " + (execute ? "execute" : "cancelled"));
super.onExecuteWrite(device, requestId, execute);
Iterator<InputBuffer> itr = inputBuffers.iterator();
while(itr.hasNext()) {
InputBuffer buf = itr.next();
if(buf.device.equals(device)) {
itr.remove();
if(execute) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
for (byte[] b : buf.bytes) {
os.write(b, 0, b.length);
}
handleInputMessage(device, buf.characteristicUuid, os.toByteArray());
}
}
}
}
InputBuffer机制的实现类似于
private class InputBuffer {
final BluetoothDevice device;
final UUID characteristicUuid;
final List<byte[]> bytes;
InputBuffer(BluetoothDevice device, UUID characteristicUuid, byte[] value) {
this.device = device;
this.characteristicUuid = characteristicUuid;
this.bytes = new ArrayList<>();
this.bytes.add(value);
}
}
private List<InputBuffer> inputBuffers = new LinkedList<>();
private void handleInputFragment(BluetoothDevice device, UUID characteristicUuid, byte[] value) {
Log.d("SVC", "handling input Fragment");
for(InputBuffer buf : inputBuffers) {
if(buf.device.equals(device)) {
buf.bytes.add(value);
return;
}
}
inputBuffers.add(new InputBuffer(device, characteristicUuid, value));
}
handleInputMessage
的实现是特定于应用程序的。