【发布时间】:2019-03-08 23:43:24
【问题描述】:
我正在尝试听我的应用是否连接到蓝牙设备。我正在尝试打印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()
【问题讨论】:
标签: android kotlin bluetooth-lowenergy rx-android rxandroidble