Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android “通知返回”;“已连接错误”;而不发送任何连接请求_Android_Kotlin_Bluetooth Lowenergy_Rxandroidble - Fatal编程技术网

Android “通知返回”;“已连接错误”;而不发送任何连接请求

Android “通知返回”;“已连接错误”;而不发送任何连接请求,android,kotlin,bluetooth-lowenergy,rxandroidble,Android,Kotlin,Bluetooth Lowenergy,Rxandroidble,我正在使用RxAndroidBLE API制作一个Android应用程序,其中包含一些可中断的操作。我遵循指南中的示例和样本 我与指定的设备建立了一个BLE连接,稍后在连接时,我读写特性没有问题,但当我尝试设置电池电量特性的通知时,我收到以下可丢弃的错误消息:已连接到MAC地址为XX:XX的设备…” 我真的不理解这种情况下的错误,因为我可以毫无问题地读写 我想在为特定目的初始读取其值后设置此特性的通知 下面是重现我的问题的示例代码: private lateinit var device: Rx

我正在使用RxAndroidBLE API制作一个Android应用程序,其中包含一些可中断的操作。我遵循指南中的示例和样本

我与指定的设备建立了一个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)
      })
    )
  }
}
专用lateinit var设备:RxBleDevice
私有变量连接可观察:可观察?=null
专用变量rxBleConnection:rxBleConnection?=null
private val connectionDisposable=CompositeDisposable()
private val connectionStateDisposable=CompositeDisposable()
私有变量notifyValueChangeSubscription=CompositeDisposable()
var enableBatteryNotificationRunnable:Runnable=Runnable{
enableBatteryNotification()
}
私有变量myHandler=Handler()
启用通知之前的val延迟:长=100
私人娱乐连线(){
connectionObservable=设备。建立连接(false)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
可观察到的连接?让我们{
connectionDisposable.add(it.subscribe)(
{rxbeconnection->
this.rxbeconnection=rxbeconnection
},
{ _ ->
Log.e(“连接”、“连接错误”)
})
)
}
val state=device.observeConnectionStateChanges().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
connectionstate.add(
state.subscribe(
{连接状态->
Log.i(“连接”,“连接状态:$connectionState”)
if(connectionState==RxBleConnection.RxBleConnectionState.CONNECTED){
myHandler.postDelayed(启用电池通知Runnable,在启用通知之前延迟);
}
}
)
{ _ ->
Log.e(“连接侦听器”、“连接状态错误”)
}
)
}
私人娱乐使能电池通知(){
可观察到的连接?让我们{
var observableToReturn=it
.flatMap{it.setupNotification(UUID_电池水平)}
doOnNext先生{
Log.i(“通知”、“doOnNext”)
}
.flatMap{it}
.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
notifyValueChangeSubscription.add(observableToReturn.subscribe({字节->
var strBytes=字符串(字节)
Log.i(“通知”,“值更改:$strBytes”)
}, 
{可丢弃->
Log.e(“通知”,“通知过程中的错误:”+throwable.message)
})
)
}
}
提前感谢您的帮助:)

setupNotification返回“错误已连接”,但未发送任何连接请求

实际上发出了两个连接请求-因此出现了错误。从
rxblevice.buildConnection()
Javadoc:

     * Establishes connection with a given BLE device. {@link RxBleConnection} is a handle, used to process BLE operations with a connected
     * device.
在您的代码中,有两个对
建立连接()

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)
      })
    )
  }
}
为此:

  connectionObservable = device.establishConnection(false)
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .compose(ReplayingShare.instance())
使用
rxbeconnection:rxbeconnection?
属性 而不是:

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

其中,
ReadResult
NotifyResult
将是采用
UUID
ByteArray

数据类,非常感谢您!您的答案非常清楚,并揭示了每种解决方案的好处!我使用了RxReplayShare,现在一切正常:)
  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)
      })
    )
  }
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 */ }
    )