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

Why setMaxBatchSize() is not useful for larger MTU in RxAndroidBle?

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

现在的问题是,当我通过 setMaxBatchSize(ex 64) 修改 MTU 时,mRxBleConnection.getMtu() returns 默认 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 -> {

        });
    }
  1. 换个方式试试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。即使phone支持,与phone通信的设备也不支持。 设置 MTU 需要同时支持移动 phone 和设备。

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

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 时,您可以在日志中看到新值未被接受——您要通信的外围设备不允许它。