RxAndroidBLE 已连接,无法向物联网设备发送数据

RxAndroidBLE already connected and can't send data to IoT device

我有一台带 BLE 的物联网设备,还有一部支持 BLE 协议的智能手机。 我正在使用 RxAndroidBle:com.polidea.rxandroidble2:rxandroidble:1.11.1 问题是互相沟通。我已建立连接:

    @OnClick(R.id.connectButton)
    void onConnectButton() {

        if (rxBleDevice == null) {
            if (myViewModel.getMacAddress().getValue() != null) {
                if (!myViewModel.getMacAddress().getValue().isEmpty()) {
                    // get BLE device
                    rxBleDevice = SampleApplication.getRxBleClient(this.getActivity())
                            .getBleDevice(myViewModel.getMacAddress().getValue());

                    // establish connection
                    connectionObservable = rxBleDevice.establishConnection(false)
                            .takeUntil(disconnectTriggerSubject);
//                            .compose(ReplayingShare.instance());
                    /*
                    reason: no instance(s) of type variable(s) T exist so that ReplayingShare<T> conforms to
                    ObservableTransformer<? super RxBleConnection, ? extends R
                     */

                    statusTextView.setText(R.string.connected);
                }

            }
        } else {
            triggerDisconnect();
            statusTextView.setText(R.string.disconnected);
        }
    }

然后我只使用 connectionObservable 发送这样的数据:

                if (rxBleDevice != null) {
//                    if (isConnected()) {
                    final Disposable disposable = connectionObservable
                            .firstOrError()
                            .flatMap(rxBleConnection -> rxBleConnection.writeCharacteristic(uuid, HexString.hexToBytes(data)))
                            .subscribe(
                                    bytes -> onWriteSuccess(bytes),
                                    throwable -> onWriteFailure(throwable)
                            );
                    compositeDisposable.add(disposable);
//                    }
                }

我总是得到的错误是: Already connected to device with MAC address EA:A5:34:E6:28:2E,但如果我尝试 isConnected() 总是说它们没有连接。有没有办法每 300 毫秒向物联网设备发送一次数据? 下面是完整的堆栈跟踪。

I/VideoFragment: Write error: 
      com.polidea.rxandroidble2.exceptions.BleAlreadyConnectedException: Already connected to device with MAC address EA:A5:34:E6:28:2E
        at com.polidea.rxandroidble2.internal.RxBleDeviceImpl.call(RxBleDeviceImpl.java:84)
        at com.polidea.rxandroidble2.internal.RxBleDeviceImpl.call(RxBleDeviceImpl.java:72)
        at io.reactivex.internal.operators.observable.ObservableDefer.subscribeActual(ObservableDefer.java:33)
        at io.reactivex.Observable.subscribe(Observable.java:12284)
        at io.reactivex.internal.operators.observable.ObservableTakeUntil.subscribeActual(ObservableTakeUntil.java:38)
        at io.reactivex.Observable.subscribe(Observable.java:12284)
        at io.reactivex.internal.operators.observable.ObservableElementAtSingle.subscribeActual(ObservableElementAtSingle.java:37)
        at io.reactivex.Single.subscribe(Single.java:3666)
        at io.reactivex.internal.operators.single.SingleFlatMap.subscribeActual(SingleFlatMap.java:36)
        at io.reactivex.Single.subscribe(Single.java:3666)
        at io.reactivex.Single.subscribe(Single.java:3652)
        at com.example.automotive.Fragments.VideoFragment.onMove(VideoFragment.java:275)
        at io.github.controlwear.virtual.joystick.android.JoystickView.run(JoystickView.java:860)
        at android.os.Handler.handleCallback(Handler.java:914)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7560)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

isConnected 方法:

    private boolean isConnected() {
        return rxBleDevice.getConnectionState() == RxBleConnection.RxBleConnectionState.CONNECTED;
    }

Is there a way to send data every 300 ms to IoT device?

当然有。如果没有要发送的数据的外部来源,可以使用类似于以下的代码:

bleDevice.establishConnection(false)
    .flatMap(rxBleConnection -> 
        Observable.interval(300, TimeUnit.MILLISECONDS)
            .flatMap(ignored -> rxBleConnection.writeCharacteristic(uuid, HexString.hexToBytes(data)))
    )
    .subscribe(
        ignored -> {},
        error -> { /* log or something */ }
    );

以上假设没有其他人同时订阅 bleDevice.establishConnection(false)

我想你想问的是为什么你会得到这个异常以及如何忍受它。引入此异常是为了防止用户从代码中的多个位置调用有状态 BLE 传输并将其搞乱。有个wiki page about it.

您可以使用 RxReplayingShare 来分享 Observable<RxBleConnection。那你就得不到BleAlreadyConnectedException。您已经尝试过,但显然注释掉了该行,因为编译器无法找出它将 replay/share 的对象类型。也许用 ReplayingShare.<RxBleConnection>instance() 指定它会有帮助?