Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android蓝牙低能量(ble)写特征延迟回调_Android_Bluetooth Lowenergy - Fatal编程技术网

Android蓝牙低能量(ble)写特征延迟回调

Android蓝牙低能量(ble)写特征延迟回调,android,bluetooth-lowenergy,Android,Bluetooth Lowenergy,我正在Android上使用BLEAPI(SDK 18)实现一个应用程序,我有一个问题,传输数据的过程非常缓慢。这是我的日志 03-12 16:20:05.121:D/BluetoothGatt(13578):writeCharacteristic()-uuid: 03-12 16:20:06.272:D/BluetoothGatt(13578):onCharacteristicWrite()-Device=。。。UUID=。。。状态=0 03-12 16:20:06.972:D/Bluetoot

我正在Android上使用BLEAPI(SDK 18)实现一个应用程序,我有一个问题,传输数据的过程非常缓慢。这是我的日志

03-12 16:20:05.121:D/BluetoothGatt(13578):writeCharacteristic()-uuid:

03-12 16:20:06.272:D/BluetoothGatt(13578):onCharacteristicWrite()-Device=。。。UUID=。。。状态=0

03-12 16:20:06.972:D/BluetoothGatt(13578):writeCharacteristic()-uuid:

03-12 16:20:08.254:D/BluetoothGatt(13578):onCharacteristicWrite()-Device=。。。UUID=。。。状态=0

03-12 16:20:10.055:D/BluetoothGatt(13578):writeCharacteristic()-uuid:

03-12 16:20:11.257:D/BluetoothGatt(13578):onCharacteristicWrite()-Device=。。。UUID=。。。状态=0

03-12 16:20:12.478:D/BluetoothGatt(13578):writeCharacteristic()-uuid:

03-12 16:20:14.250:D/BluetoothGatt(13578):onCharacteristicWrite()-Device=。。。UUID=。。。状态=0

03-12 16:20:14.960:D/BluetoothGatt(13578):writeCharacteristic()-uuid:

03-12 16:20:16.242:D/BluetoothGatt(13578):onCharacteristicWrite()-Device=。。。UUID=。。。状态=0

03-12 16:20:16.402:D/BluetoothGatt(13578):writeCharacteristic()-uuid:

03-12 16:20:20.225:D/BluetoothGatt(13578):onCharacteristicWrite()-Device=。。。UUID=。。。状态=0

03-12 16:20:20.526:D/BluetoothGatt(13578):writeCharacteristic()-uuid:

03-12 16:20:24.219:D/BluetoothGatt(13578):onCharacteristicWrite()-Device=。。。UUID=。。。状态=0

03-12 16:20:25.360:D/BluetoothGatt(13578):writeCharacteristic()-uuid:

03-12 16:20:27.222:D/BluetoothGatt(13578):onCharacteristicWrite()-Device=。。。UUID=。。。状态=0

关于更多信息,我发现每个传输过程只有在具有onCharacteristicWrite回调时才能完成,这意味着在接收onCharacteristicWrite回调之前的所有发送命令都将被忽略

这是我们必须遵循的Android流程,还是有办法设置它?跳过回调步骤以加快进度

我的代码是:

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
......
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    mSending = false;
}
};

private void writeCharacteristic() {
    .....

    mGattCharacSetIntensity.setValue(data);
    mGattCharacSetIntensity.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
    mBluetoothGatt.writeCharacteristic(mGattCharacSetIntensity);
    return;
}
编辑:我与iPhone(AppStore的BLE传输数据应用程序)进行了比较,BLE传输数据非常快(不到0.5秒),令人印象深刻。我们如何才能加快传输进度


编辑:如果我将BluetoothGattCharacteristic的WriteType设置为WRITE_TYPE_NO_响应,当我依次发送多个命令时,Android会将它们存储在队列中,并在收到writeCharacteristic回调后逐个发送到远程设备,这会导致一个问题,当您停止发送时,Android发送进度仍在继续,但会有延迟(有时会超过3秒)。

BLE链接的性能在很大程度上取决于所使用的连接间隔,如果您的连接间隔很高,您看到的性能可能不会那么不合理。根据核心规范,连接间隔可以在7.5 ms和4 s之间,因此具有相当大的灵活性


如果可能的话,我建议您尝试将正在通话的外围设备更改为使用较短的连接间隔,这将提高性能。您可以查看、解释BLE吞吐量,以及解释连接参数

我们也有同样的问题。它发生在几次写入之后,在重新连接Gatt时消失。我们要做的是测量writeCharacteristic()和onCharacteristicWrite()之间的延迟。如果超过某个阈值(1500ms),我们将断开()并关闭()gatt服务器,然后重新连接。在那之后的一段时间里,天气通常是晴朗的

执行此操作时,可能需要禁用整个蓝牙适配器并重新启用它。否则,与gatt服务器的第一次重新连接失败,需要大约15秒才能重新连接。有时它永远不会重新连接


禁用适配器时,可能会遇到以下问题:OnServicesDiscovery()中没有返回服务发现。我们目前没有解决方案

这对我来说似乎也是个问题。如果延迟调试器中的调用,则可以确认在写入BLE无线电时存在延迟。我将在我的应用程序中实现一个队列来处理延迟

以下是我对命令排队所做的操作:

public void sendWriteCommandToConnectedMachine(byte[] commandByte) {
    if(commandByte.length > 20)
        dissectAndSendCommandBytes(commandByte);
    else
        queueCommand(commandByte); //TODO - need to figure out if service is valid or not
}

private void queueCommand(byte[] command) {
    mCommandQueue.add(command);

    if(!mWaitingCommandResponse)
        dequeCommand();
}
下面是我对可撤销命令的出列操作

private void dequeCommand() {
    if(mCommandQueue.size() > 0) {
        byte[] command = mCommandQueue.get(0);
        mCommandQueue.remove(0);
        sendWriteCommand(command);
    }
}
这是我特有的写作方法

@Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        if(status != BluetoothGatt.GATT_SUCCESS)
            logMachineResponse(characteristic, status);     

        mWaitingCommandResponse = false;
        dequeCommand();
    }

您必须实现自己的流量控制,在为上一次写入发送OnCharacteristicWrite之前不要开始写入

这是模拟蓝牙的输出还是来自您设备的真实值?这些来自real Nexus 7,2013版,Android OS 4.3,安卓BLE堆栈实际上是同步工作的。所以它可以实现堆栈操作。谢谢用户370305,我同意你的观点,但是谷歌安卓系统没有任何文档可以证实这一点。所以我没有任何证据。如此悲伤。我们可以缩短延迟时间吗?你的写操作肯定不会花那么长时间。在KitKat设备上,我的性能测试表明,在关闭响应的情况下,我能够以超过7KB/s的速度写入数据,并能跨越10次。每次写入都是在收到蓝牙设备的响应后才完成的。谢谢@hlnd,我正在尝试你的建议。亲爱的hlnd,你的链接只是说连接质量取决于操作系统,但我们如何在Android(4.3)上更快,我希望不到1秒。正如我所说,在建立连接后,您可能必须更改外围设备上运行的代码,以尽快请求更短的连接间隔。具体如何做到这一点将在很大程度上取决于您在该设备上使用的芯片,因此我无法在此对此进行评论。亲爱的hind,不幸的是,外围设备超出了我的范围,这是客户提供的设备。我不能做任何优化这方面。在这种情况下,我真的没有任何建议。据我所知,,