如何在RxSwift中使用RxBluetooth将多个订阅连接到Disposable?

时间:2018-10-16 16:02:09

标签: bluetooth-lowenergy publish-subscribe rx-swift cbperipheral rxbluetooth

使用 RxBluetooth ,连接到CBP外围设备从未如此简单:

disposable = peripheral.establishConnection()
        .flatMap { $0.discoverServices([serviceId]) }.asObservable()
        .flatMap { Observable.from($0) }
        .flatMap { $0.discoverCharacteristics(nil)}.asObservable()
        .flatMap { Observable.from($0) }
        .flatMap { $0.readValue() }
        .subscribe(onNext: { characteristic in
            // At this point we have connected to the peripheral
            // Discovered the service with the id 'serviceId'
            // Discovered all the characteristics of the service
            // and this print will be triggered after reading each value

            print(Value read: characteristic.value)

        })

我想将操作串联到外围设备,所以当订阅触发时,我知道我有一个经过验证的外围设备。

一次性用品将具有串联的动作,如果动作设置成功或出错,将返回true

类似这样的东西:

disposable = peripheral.establishConnection()

        // Action 1: Let's validate the advertismentData, if it doesn't have the correct advertisement data, we trigger an error

        .flatMap { self.validateAdvertisementData($0) }
        // If there is no error we continue
        .flatMap { $0.discoverServices([serviceId]) }.asObservable()
        .flatMap { Observable.from($0) }
        .flatMap { $0.discoverCharacteristics(nil)}.asObservable()
        .flatMap { Observable.from($0) } 
        .flatMap { $0.readValue() }

        // Action 2: Let's validate the characteristics values, if a characteristic is missing a value we trigger an error

        .flatMap { self.validateInitialCharacteristics($0) }

        // If there is no error we continue by discovering the rest of the services of the peripheral

        // Action 3: We keep discovering services as this is a validated peripheral

        .flatMap { peripheral.discoverServices([healthServiceId, communicationServiceId]) }.asObservable()
        .flatMap { Observable.from(peripheral) }
        .flatMap { $0.discoverCharacteristics(nil)}.asObservable()
        .flatMap { Observable.from($0) } 
        .flatMap { $0.readValue() }

        //Action 4: Let's validate that we read the values and send an initialization packet to the peripheral   

        .flatMap { self.validateSubCharacteristics($0) }

        //Action 5: The values are valid, let's initialize the Peripheral

        .flatMap { self.initialize(peripheral) }

        //If we get a response, then it calls onNext.

        .subscribe(onNext: { Bool in
            // At this point we have connected to the peripheral
            // Discovered the service with the id 'serviceId'
            // Discover all the characteristics of the service
            // Read all values of these characteristics
            // Validated all the values
            // Made another discover for other services
            // Read the characteristics for those
            // Validated the values
            // Write to the peripheral
            // and this print will be triggered after the writing 

            print("Peripheral ready")

        }, onError: { Error in 

            print("Peripheral initialization failed")
        })

因此,主要思想是使用RxSwift 连接不同的操作,并且在所有操作成功完成后只能得到一个响应,即使没有得到一个错误也是如此。

也许我可以使用不同的主题,也可以使用多个订阅(只有一个一次性对象)来断开连接并将其连接起来?

0 个答案:

没有答案