Android 成为 RxKotlin
Android BLE RxKotlin
我正在尝试创建一个 BLE 服务来扫描设备,并使用 rxKotlin 创建一个可观察对象,允许另一个 class 观察何时找到设备。我对如何创建允许另一个 class 订阅的可观察对象感到困惑,而且到处都是教程。有人可以指导我如何操作或提供好的教程。
蓝牙服务class 发现设备的回调
var foundDeviceObservable: Observable<BluetoothDevice> = Observable.create { }
private val scanCallback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
with(result.device) {
var foundName = if (name == null) "N/A" else name
foundDevice = BluetoothDevice(
foundName,
address,
address,
result.device.type.toString()
)
foundDeviceObservable.subscribe {
//Update Observable value?
}
}
}
}
class DeviceListViewModel(application: Application) : AndroidViewModel(application) {
private val bluetoothService = BLEService()
//Where I am trying to do logic with device
fun getDeviceObservable(){
bluetoothService.getDeviceObservable().subscribe{ it ->
}
}
解决方案
看了user4097210的回复找到了解决办法。只需要将找到的设备更改为
var foundDeviceObservable: BehaviorSubject<BluetoothDevice> = BehaviorSubject.create()
然后在回调中调用下一个方法
private val scanCallback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
with(result.device) {
var foundName = if (name == null) "N/A" else name
foundDevice = BluetoothDevice(
foundName,
address,
address,
result.device.type.toString()
)
foundDeviceObservable.onNext(foundDevice)
}
}
}
使用 BehaviorSubject
// create a BehaviorSubject
var foundDeviceObservable: BehaviorSubject<BluetoothDevice> = BehaviorSubject()
// call onNext() to send new found device
foundDeviceObservable.onNext(foundDevice)
// do your logic use foundDeviceObservable
foundDeviceObservable.subscribe(...)
我正在尝试创建一个 BLE 服务来扫描设备,并使用 rxKotlin 创建一个可观察对象,允许另一个 class 观察何时找到设备。我对如何创建允许另一个 class 订阅的可观察对象感到困惑,而且到处都是教程。有人可以指导我如何操作或提供好的教程。
蓝牙服务class 发现设备的回调
var foundDeviceObservable: Observable<BluetoothDevice> = Observable.create { }
private val scanCallback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
with(result.device) {
var foundName = if (name == null) "N/A" else name
foundDevice = BluetoothDevice(
foundName,
address,
address,
result.device.type.toString()
)
foundDeviceObservable.subscribe {
//Update Observable value?
}
}
}
}
class DeviceListViewModel(application: Application) : AndroidViewModel(application) {
private val bluetoothService = BLEService()
//Where I am trying to do logic with device
fun getDeviceObservable(){
bluetoothService.getDeviceObservable().subscribe{ it ->
}
}
解决方案
看了user4097210的回复找到了解决办法。只需要将找到的设备更改为
var foundDeviceObservable: BehaviorSubject<BluetoothDevice> = BehaviorSubject.create()
然后在回调中调用下一个方法
private val scanCallback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
with(result.device) {
var foundName = if (name == null) "N/A" else name
foundDevice = BluetoothDevice(
foundName,
address,
address,
result.device.type.toString()
)
foundDeviceObservable.onNext(foundDevice)
}
}
}
使用 BehaviorSubject
// create a BehaviorSubject
var foundDeviceObservable: BehaviorSubject<BluetoothDevice> = BehaviorSubject()
// call onNext() to send new found device
foundDeviceObservable.onNext(foundDevice)
// do your logic use foundDeviceObservable
foundDeviceObservable.subscribe(...)