以更少的延迟在蓝牙 Ble 中发送连续数据流

Sending continues data stream in bluetooth Ble with less delay

背景

我正在开发一个 android 应用程序,它可以与 Nordic 蓝牙 4 设备通信, 我可以发送和接收来自北欧的数据。

问题是每当我想发送批量数据时,我必须将数据分成几个 20 字节的数据并以 50 毫秒的延迟发送

如下代码所示

private boolean sendBytes(byte[] iBytes){
    sendResetBytes();
    byte[] arr=new byte[20];
    for(int i=0;i<iBytes.length;i++){
        if(i!=0&&i%20==0){
            if(!mBluetoothGeneric.send(arr))return false;
            arr=new byte[20];
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        arr[i%20]=iBytes[i];
    }
    if(arr.length!=0)
        if(!mBluetoothGeneric.send(arr))return false;
    return true;
}

为了发送字节,我使用了 Nordic 提供的 uartService 库

send() 我实现了简单调用 writeRxCharacteristics() fn

 public boolean writeRXCharacteristic(byte[] value)
{


    BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
    showMessage("mBluetoothGatt null"+ mBluetoothGatt);
    if (RxService == null) {
        showMessage("Rx service not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return false;
    }
    BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(RX_CHAR_UUID);
    if (RxChar == null) {
        showMessage("Rx charateristic not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return false;
    }
    RxChar.setValue(value);
    boolean status = mBluetoothGatt.writeCharacteristic(RxChar);
    return status;
}

我的问题,有什么方法可以让我以尽可能短的延迟向 nordic 发送批量数据

首先调用 setWriteType(WRITE_TYPE_NO_RESPONSE) 特性,以便能够在一个连接事件中发送多个数据包。

然后您需要一次写入每个块并在发送下一个之前等待 onCharacteristicWrite,因为在 Android 的 BLE 堆栈中一次只能有一个未完成的 GATT 操作。