0

現在サンプル コードを変更していますが、接続を確立して 1 つのアクティビティで特性を読み取ろうとすると、エラーが発生します。

BleAlreadyConnectedExceptionデバイスから読み込もうとすると取得します。最初に 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);
    }
}

次に、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);
    }
}

ConnectionSharingAdapter で二重の . EstablishConnection(this, false) を回避するにはどうすればよいですか?

4

1 に答える 1

1

問題は、2回接続していることです。むしろ:

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

完全な例はこちらです。

于 2016-07-22T10:19:06.100 に答える