How to establish connection and then read characteristics in one activity?

有些话、适合烂在心里 提交于 2020-01-25 16:35:31

问题


Currently modifying the sample code and I am encountering an error when try to establish a connection and then read a characteristic in one activity.

I am getting BleAlreadyConnectedException when attempting to read from the device. I am first connecting on onCreate.

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_device);

    // How to listen for connection state changes
    bleDevice.observeConnectionStateChanges()
            .compose(bindUntilEvent(DESTROY))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(this::onConnectionStateChange);

    if (isConnected()) {
        triggerDisconnect();
    } else {
        connectionSubscription = bleDevice.establishConnection(this, true)
                .compose(bindUntilEvent(PAUSE))
                .observeOn(AndroidSchedulers.mainThread())
                .doOnUnsubscribe(this::clearSubscription)
                .subscribe(this::onConnectionReceived, this::onConnectionFailure);
    }
}

and then trying to read a characteristic in onConnectionReceived...

    private void onConnectionReceived(RxBleConnection connection) { 
    Log.d(TAG, "onConnectionReceived");
    connectionObservable = bleDevice
            .establishConnection(this, false)
            .takeUntil(disconnectTriggerSubject)
            .compose(bindUntilEvent(PAUSE))
            .doOnUnsubscribe(this::clearSubscription)
            .compose(new ConnectionSharingAdapter());

    if (isConnected()) {
        connectionObservable
                .flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(uuid))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(bytes -> {
                    Log.d(TAG, "onConnectionReceived:" + bytes);
                }, this::onReadFailure);
    }
}

How do you work around the double .establishConnection(this, false) with ConnectionSharingAdapter?


回答1:


The thing is that you are connecting twice. You should rather:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_device);

  // How to listen for connection state changes
  bleDevice.observeConnectionStateChanges()
    .compose(bindUntilEvent(DESTROY))
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(this::onConnectionStateChange);

  connectionObservable = bleDevice.establishConnection(this, true)
    .compose(bindUntilEvent(PAUSE))
    .takeUntil(disconnectTriggerSubject)
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .doOnUnsubscribe(this::clearSubscription)
    .compose(new ConnectionSharingAdapter());

  if (isConnected()) {
      triggerDisconnect();
  } else {
      connectionObservable.subscribe(this::onConnectionReceived, this::onConnectionFailure);
  }
}

private void onConnectionReceived(RxBleConnection connection) { 
  Log.d(TAG, "onConnectionReceived");
  connection
    .flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(uuid))
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(bytes -> {
        Log.d(TAG, "onValueReceived:" + bytes);
    }, this::onReadFailure);
}

Full example is here.



来源:https://stackoverflow.com/questions/38523715/how-to-establish-connection-and-then-read-characteristics-in-one-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!