用RXAndroidBlE观察连接状态
我正在尝试侦听我的应用程序是否连接到蓝牙设备。我试图打印用RXAndroidBlE观察连接状态,android,kotlin,bluetooth-lowenergy,rx-android,rxandroidble,Android,Kotlin,Bluetooth Lowenergy,Rx Android,Rxandroidble,我正在尝试侦听我的应用程序是否连接到蓝牙设备。我试图打印connectionState结果,但应用程序甚至没有到达第一个println,因此我无法检查它们可能是什么。我想列举可能的连接状态,然后根据响应调整UI。我该怎么做 val rxBleClient = RxBleClient.create(this.context!!) val bleDevice = rxBleClient.getBleDevice("34:81:F4:3C:2D:7B") val disposable = bleDe
connectionState
结果,但应用程序甚至没有到达第一个println
,因此我无法检查它们可能是什么。我想列举可能的连接状态,然后根据响应调整UI。我该怎么做
val rxBleClient = RxBleClient.create(this.context!!)
val bleDevice = rxBleClient.getBleDevice("34:81:F4:3C:2D:7B")
val disposable = bleDevice.establishConnection(true) // <-- autoConnect flag
.subscribe({
rxBleConnection ->
// All GATT operations are done through the rxBleConnection.
bleDevice.observeConnectionStateChanges()
.subscribe({
connectionState ->
println("Connection State: $connectionState")
if (connectionState != null) {
enableBluetooth.setBackgroundResource(R.drawable.bluetooth_on) // Change image
deviceConnected.setText(R.string.connected_to_hooplight) // Changed text
} else {
enableBluetooth.setBackgroundResource(R.drawable.bluetooth_off) // Change image
deviceConnected.setText(R.string.connect_to_hooplight) // Changed text
}
}, {
throwable ->
Log.d("Error: ", throwable.toString())
})
}, {
throwable ->
// Handle an error here.
Log.d("Error: ", throwable.toString())
})
// When done... dispose and forget about connection teardown :)
disposable.dispose()
val rxBleClient=rxBleClient.create(this.context!!)
val bleDevice=rxBleClient.getBleDevice(“34:81:F4:3C:2D:7B”)
val一次性=bleDevice.establishConnection(真)//
//所有关贸总协定的运作都是通过rxBleConnection完成的。
bleDevice.observeConnectionStateChanges()
.订阅({
连接状态->
println(“连接状态:$connectionState”)
if(connectionState!=null){
enableBluetooth.setBackgroundResource(R.drawable.bluetooth\u打开)//更改图像
deviceConnected.setText(R.string.connected_to_hooplight)//更改的文本
}否则{
enableBluetooth.setBackgroundResource(R.drawable.bluetooth\u关闭)//更改图像
deviceConnected.setText(R.string.connect\u to\u hooplight)//更改的文本
}
}, {
可丢弃->
Log.d(“错误:,throwable.toString())
})
}, {
可丢弃->
//在这里处理错误。
Log.d(“错误:,throwable.toString())
})
//完成后。。。处理并忘记连接拆卸:)
一次性处置
上述代码有两个方面:
println
也不会出现的原因bleDevice.establishConnection()
和bleDevice.observeConnectionStateChanges()
在功能上互不依赖。不必建立连接来观察变化。即使在连接打开后开始观察更改,也只会在连接关闭时获得信息(因为这是自那时以来的第一次更改)val observingConnectionStateDisposable = bleDevice.observeConnectionStateChanges()
.subscribe(
{ connectionState ->
Log.d("Connection State: $connectionState")
if (connectionState == RxBleConnectionState.CONNECTED) { // fixed the check
enableBluetooth.setBackgroundResource(R.drawable.bluetooth_on) // Change image
deviceConnected.setText(R.string.connected_to_hooplight) // Changed text
} else {
enableBluetooth.setBackgroundResource(R.drawable.bluetooth_off) // Change image
deviceConnected.setText(R.string.connect_to_hooplight) // Changed text
}
},
{ throwable -> Log.d("Error: ", throwable.toString()) }
)
val connectionDisposable = bleDevice.establishConnection(false)
.subscribe(
{ Log.d("connection established") }, // do your thing with the connection
{ throwable -> Log.d("Error on connection: ${throwable}") }
)
上述代码有两个方面:
println
也不会出现的原因bleDevice.establishConnection()
和bleDevice.observeConnectionStateChanges()
在功能上互不依赖。不必建立连接来观察变化。即使在连接打开后开始观察更改,也只会在连接关闭时获得信息(因为这是自那时以来的第一次更改)val observingConnectionStateDisposable = bleDevice.observeConnectionStateChanges()
.subscribe(
{ connectionState ->
Log.d("Connection State: $connectionState")
if (connectionState == RxBleConnectionState.CONNECTED) { // fixed the check
enableBluetooth.setBackgroundResource(R.drawable.bluetooth_on) // Change image
deviceConnected.setText(R.string.connected_to_hooplight) // Changed text
} else {
enableBluetooth.setBackgroundResource(R.drawable.bluetooth_off) // Change image
deviceConnected.setText(R.string.connect_to_hooplight) // Changed text
}
},
{ throwable -> Log.d("Error: ", throwable.toString()) }
)
val connectionDisposable = bleDevice.establishConnection(false)
.subscribe(
{ Log.d("connection established") }, // do your thing with the connection
{ throwable -> Log.d("Error on connection: ${throwable}") }
)