我试图创建一个可观察的序列来指示设备上蓝牙的状态。我正在使用
连接回调委托到RxSwift可观察域的适当方式是什么?
ReplaySubject<CBManagerState>
,但是我想知道是否有更好的方法,因为我听说使用onNext()
会有一些问题。连接回调委托到RxSwift可观察域的适当方式是什么?
class BluetoothStatusMonitor: NSObject, CBPeripheralManagerDelegate {
let bluetoothStatusSequence = ReplaySubject<CBManagerState>.create(bufferSize: 1)
var bluetoothPeripheralManager: CBPeripheralManager?
func checkBluetoothStatus()
{
//silently check permissions, without alert
let options = [CBCentralManagerOptionShowPowerAlertKey:0]
bluetoothPeripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: options)
}
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
bluetoothStatusSequence.onNext(peripheral.state)
}
}