【问题标题】:setupNotification returns "Error already connected" whereas no connection request is sendsetupNotification 返回“已连接错误”,而未发送连接请求
【发布时间】:2019-04-25 17:50:05
【问题描述】:

我正在使用 RxAndroidBLE API 制作一个带有一些 BLE 交互的 Android 应用程序。我遵循了来自https://github.com/Polidea/RxAndroidBle的示例指南和示例

我与指定设备建立了 BLE 连接,稍后在连接时我读取和写入特性没有问题,但是当我尝试为电池电量特性设置通知时,我收到以下可抛出的错误消息:已经连接到设备MAC 地址 XX:XX..."

我真的不明白这种情况下的错误,因为我可以毫无问题地读写特征。

我想在出于特定目的初始读取其值后为此特征设置通知。

这是重现我的问题的示例代码:

private lateinit var device: RxBleDevice
private var connectionObservable: Observable<RxBleConnection>? = null
private var rxBleConnection: RxBleConnection? = null

private val connectionDisposable = CompositeDisposable()
private val connectionStateDisposable = CompositeDisposable()
private var notifyValueChangeSubscription = CompositeDisposable()


var enableBatteryNotificationRunnable: Runnable = Runnable {
  enableBatteryNotification()
}
private var myHandler = Handler()
val DELAY_BEFORE_ENABLE_NOTIFICATION: Long = 100


private fun connect() {
  connectionObservable = device.establishConnection(false)
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())

  connectionObservable?.let {
    connectionDisposable.add(it.subscribe(
      { rxBleConnection ->
        this.rxBleConnection = rxBleConnection
      },
      { _ ->
        Log.e("connect", "connexion error")                       
      })
    )
  }

  val state = device.observeConnectionStateChanges().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
  connectionStateDisposable.add(
    state.subscribe(
      { connectionState ->
        Log.i("connect", "connexion state :$connectionState")
        if(connectionState == RxBleConnection.RxBleConnectionState.CONNECTED) {
            myHandler.postDelayed(enableBatteryNotificationRunnable, DELAY_BEFORE_ENABLE_NOTIFICATION);
        }
      }
    )
    { _ ->
      Log.e("connection listener", "connexion state error")
    }
  )
}

private fun enableBatteryNotification () {
  connectionObservable?.let {
    var observableToReturn =  it
      .flatMap { it.setupNotification(UUID_BATTERY_LEVEL) }
      .doOnNext {
        Log.i("NOTIFICATION", "doOnNext")
      }
      .flatMap { it }
      .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())

    notifyValueChangeSubscription.add(observableToReturn.subscribe({ bytes ->
        var strBytes = String(bytes)
        Log.i("NOTIFICATION", "value change: $strBytes")
      }, 
      { throwable ->
        Log.e("NOTIFICATION", "Error in notification process: " + throwable.message)
      })
    )
  }
}

提前感谢您的帮助:)

【问题讨论】:

    标签: android kotlin bluetooth-lowenergy rxandroidble


    【解决方案1】:

    setupNotification 返回“已连接错误”,而没有发送连接请求

    实际上发出了两个连接请求——因此出现了错误。来自RxBleDevice.establishConnection()Javadoc:

         * Establishes connection with a given BLE device. {@link RxBleConnection} is a handle, used to process BLE operations with a connected
         * device.
    

    在您的代码中有两个订阅establishConnection()Observable

    private lateinit var device: RxBleDevice
    private var connectionObservable: Observable<RxBleConnection>? = null
    private var rxBleConnection: RxBleConnection? = null
    
    private val connectionDisposable = CompositeDisposable()
    private val connectionStateDisposable = CompositeDisposable()
    private var notifyValueChangeSubscription = CompositeDisposable()
    
    
    var enableBatteryNotificationRunnable: Runnable = Runnable {
      enableBatteryNotification()
    }
    private var myHandler = Handler()
    val DELAY_BEFORE_ENABLE_NOTIFICATION: Long = 100
    
    
    private fun connect() {
      connectionObservable = device.establishConnection(false)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
    
      connectionObservable?.let {
        connectionDisposable.add(it.subscribe( // << Here is the first subscription
          { rxBleConnection ->
            this.rxBleConnection = rxBleConnection
          },
          { _ ->
            Log.e("connect", "connexion error")                       
          })
        )
      }
    
      val state = device.observeConnectionStateChanges().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
      connectionStateDisposable.add(
        state.subscribe(
          { connectionState ->
            Log.i("connect", "connexion state :$connectionState")
            if(connectionState == RxBleConnection.RxBleConnectionState.CONNECTED) {
                myHandler.postDelayed(enableBatteryNotificationRunnable, DELAY_BEFORE_ENABLE_NOTIFICATION);
            }
          }
        )
        { _ ->
          Log.e("connection listener", "connexion state error")
        }
      )
    }
    
    private fun enableBatteryNotification () {
      connectionObservable?.let {
        var observableToReturn =  it
          .flatMap { it.setupNotification(UUID_BATTERY_LEVEL) }
          .doOnNext {
            Log.i("NOTIFICATION", "doOnNext")
          }
          .flatMap { it }
          .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
    
        notifyValueChangeSubscription.add(observableToReturn.subscribe({ bytes -> // << Here is the second subscription
            var strBytes = String(bytes)
            Log.i("NOTIFICATION", "value change: $strBytes")
          }, 
          { throwable ->
            Log.e("NOTIFICATION", "Error in notification process: " + throwable.message)
          })
        )
      }
    }
    

    这种情况对于学习RxJava 的人来说是一个常见的困惑来源。有three paths 可以解决您的问题。从最少到最多的工作量:

    分享establishConnection Observable

    可以与RxReplayingShare 共享一个RxBleConnection。改变这个:

      connectionObservable = device.establishConnection(false)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
    

    到这里:

      connectionObservable = device.establishConnection(false)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(ReplayingShare.instance())
    

    使用rxBleConnection: RxBleConnection? 属性

    代替:

      connectionObservable?.let {
        var observableToReturn =  it
          .flatMap { it.setupNotification(UUID_BATTERY_LEVEL) }
          .doOnNext {
            Log.i("NOTIFICATION", "doOnNext")
          }
          .flatMap { it }
          .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
    
        notifyValueChangeSubscription.add(observableToReturn.subscribe({ bytes -> // << Here is the second subscription
            var strBytes = String(bytes)
            Log.i("NOTIFICATION", "value change: $strBytes")
          }, 
          { throwable ->
            Log.e("NOTIFICATION", "Error in notification process: " + throwable.message)
          })
        )
      }
    

    制作:

      rxBleConnection?.let {
        var observableToReturn = rxBleConnection.setupNotification(UUID_BATTERY_LEVEL)
          .doOnNext {
            Log.i("NOTIFICATION", "doOnNext")
          }
          .flatMap { it }
          .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
    
        notifyValueChangeSubscription.add(observableToReturn.subscribe({ bytes -> // << Here is the second subscription
            var strBytes = String(bytes)
            Log.i("NOTIFICATION", "value change: $strBytes")
          }, 
          { throwable ->
            Log.e("NOTIFICATION", "Error in notification process: " + throwable.message)
          })
        )
      }
    

    不鼓励这样做,因为您可能会得到一个不再有效的 RxBleConnection,因为它可能在调用 enableBatteryNotification() 之前已断开连接

    更改代码流以使用单个 .subscribe()

    这是针对您的具体用例量身定制的定制解决方案。不幸的是,您添加的信息不足以创建插入式代码替换,但它可能看起来像这样:

    device.establishConnection(false)
        .flatMap { connection ->
            Observable.merge(
                connection.readCharacteristic(uuid0).map { ReadResult(uuid0, it) }.toObservable(),
                connection.setupNotification(uuid1).flatMap { it }.map { NotifyResult(uuid1, it) }.delaySubscription(100, TimeUnit.MILLISECONDS)
            )
        }
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
            { /* handle ReadResult/NotifyResult */ },
            { /* handle potential errors */ }
        )
    

    其中ReadResultNotifyResult 将是data class,即UUIDByteArray

    【讨论】:

    • 非常感谢!您的答案非常清楚,并揭示了每种解决方案的好处!我选择了 RxReplayingShare,现在一切正常 :)
    猜你喜欢
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 2020-06-04
    • 2018-06-02
    相关资源
    最近更新 更多