java - 为什么 setMaxBatchSize() 对于 RxAndroidBle 中较大的 MTU 没有用?

标签 java android bluetooth-lowenergy android-bluetooth rxandroidble

我想通过BLE向设备传输数据。每次传输的字节数为 64。

现在的问题是,当我通过 setMaxBatchSize(ex 64) 修改 MTU 时,mRxBleConnection.getMtu() 返回默认的 MTU (23)。

    private void connect(RxBleDevice rxBleDevice){

         connectionObservable = rxBleDevice.establishConnection(false)
                .subscribe(rxBleConnection -> {

                    mRxBleConnection = rxBleConnection;
                    rxBleConnection.setupNotification(MainActivity.MY_UUID);
                    longWrite();

                });


    }
    private void longWrite(){
           mRxBleConnection.setupNotification(MainActivity.MY_UUID)
                .flatMap(ob -> ob.merge(
                        mRxBleConnection.createNewLongWriteBuilder()
                                .setCharacteristicUuid(MainActivity.MY_UUID)
                                .setBytes(HexString.hexToBytes(writeData))
                                .setMaxBatchSize(64)
                                .build(),ob)
                )

        .subscribe(bytes -> {
            Log.i(TAG,mRxBleConnection.getMtu());

            doResponse(HexString.bytesToHex(bytes));

        },throwable -> {

        });
    }
  • 尝试另一种方式RxBleConnection.requestMtu(int)
  • Disposable writeSubscription = mRxBleConnection.requestMtu(176)
                    .subscribe(
                            integer -> {
                                Log.i(TAG, "longWrite: "+integer);
                            },
                            throwable ->{
    
                            }
    
                    );
    

    日志

    04-22 16:30:58.895 9435-9494/com.example.write D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
    04-22 16:30:59.642 9435-9494/com.example.write D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=7 device=50:8C:B1:6A:F9:34
    04-22 16:30:59.671 9435-9532/com.example.write D/BluetoothGatt: configureMTU() - device: 50:8C:B1:6A:F9:34 mtu: 176
    04-22 16:31:00.035 9435-9494/com.example.write D/BluetoothGatt: onConnectionUpdated() - Device=50:8C:B1:6A:F9:34 interval=6 latency=0 timeout=500 status=0
    04-22 16:31:00.347 9435-9494/com.example.write D/BluetoothGatt: onConfigureMTU() - Device=50:8C:B1:6A:F9:34 mtu=23 status=0
    

    mtu 始终为 23。

    已爱上----------已关闭

    我发现蓝牙设备的BLE版本是4.0,这个版本太低,不支持设置MTU。即使手机支持,与手机通信的设备也不支持。 设置MTU 需要手机和设备同时支持。

    最佳答案

    回答主题中的问题 - 见下文

    LongWriteOperationBuilder.setMaxBatchSize() Javadoc:

    /**
    * Setter for a maximum size of a byte array that may be write at once
    * If this is not specified - the default value of the connection's MTU is used
    *
    * @param maxBatchSize the maximum size of a byte array to write at once
    * @return the LongWriteOperationBuilder
    */
    

    如果未指定.setMaxBatchSize(),则使用从 MTU 派生的值。这是一种单向暗示。它并没有说设置此属性会更改 MTU。

    此外,当您尝试设置不同的 MTU 时,您可以在日志中看到新值未被接受 - 您想要通信的外设不允许这样做。

    关于java - 为什么 setMaxBatchSize() 对于 RxAndroidBle 中较大的 MTU 没有用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55788578/

    相关文章:

    java - Android:工具栏后退按钮是透明的

    android - Dagger 2.11 注入(inject)不适用于 fragment

    android - BluetoothAdapter 死对象异常

    android - 我们可以像配对经典蓝牙设备一样配对 BLE 设备吗?

    java - 使用 Polygonshape 创建夹具时 Box2D 抛出错误

    java - JButtons计算后如何清除

    java - 插入或替换为空时如何发送消息。 - JAVA/MYSQL

    java - java中的反转和重复字符串单词

    java - 如何检测空白区域的点击?

    java - Android BLE 设备无法相互检测