Android 在蓝牙Ble中继续发送数据流,延迟更小 背景

Android 在蓝牙Ble中继续发送数据流,延迟更小 背景,android,bluetooth,android-bluetooth,bluetooth-lowenergy,Android,Bluetooth,Android Bluetooth,Bluetooth Lowenergy,我正在开发一个android应用程序,它可以与北欧的Bluetooth 4设备进行通信, 我可以从北欧发送和接收数据 问题是,每当我想发送批量数据时,我必须将数据分成几个20字节的数据,并以50毫秒的延迟发送 如下面代码所示 private boolean sendBytes(byte[] iBytes){ sendResetBytes(); byte[] arr=new byte[20]; for(int i=0;i<iBytes.length;i++){

我正在开发一个android应用程序,它可以与北欧的Bluetooth 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;
}

我的问题是,是否有任何方法可以将批量数据以最小可能延迟第一次调用
setWriteType(WRITE\u TYPE\u NO\u RESPONSE)
发送到nordic,以便能够在一个连接事件中发送多个数据包


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

您好,我参考了您的答案,onCharacteristicWrite在WriteCharacter()之后调用但是我怎样才能在调用回调之前暂停执行,而不让它休眠呢?你可以发布一些代码片段吗?嗯,在回调到来之前,你不能暂停执行并休眠。您必须在回调中发送下一个块。使用一些状态变量来了解您发送了多远以及接下来会出现什么数据。
 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;
}