为什么setMaxBatchSize()对RxAndroidBle中较大的MTU无效?

问题描述 投票:0回答:1

我想通过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 -> {

        });
    }
  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。

java android bluetooth-lowenergy android-bluetooth rxandroidble
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.