如何使用RXBluetoothKit for RxSwift组合写入特性和特性通知

如何使用RXBluetoothKit for RxSwift组合写入特性和特性通知,swift,rxiosble,Swift,Rxiosble,我正在尝试使用适用于swift的RXBluetoothKit连接一个BLE设备。设备的所有数据命令都遵循以下顺序 1.写入命令(writeWithResponse) 2.从通知中读取响应(基于不同的特性) 通知数据包的数量(通知数据包中最多20字节)将取决于命令。这将是一个固定的数字,或基本上使用notif值中的数据位结尾指示 这可以使用writeValue()和monitorValueUpdate()组合实现吗?//命令/结果的抽象 // Abstraction of your command

我正在尝试使用适用于swift的RXBluetoothKit连接一个BLE设备。设备的所有数据命令都遵循以下顺序 1.写入命令(writeWithResponse) 2.从通知中读取响应(基于不同的特性)

通知数据包的数量(通知数据包中最多20字节)将取决于命令。这将是一个固定的数字,或基本上使用notif值中的数据位结尾指示

这可以使用writeValue()和monitorValueUpdate()组合实现吗?

//命令/结果的抽象
// Abstraction of your commands / results
enum Command {
    case Command1(arg: Float)
    case Command2(arg: Int, arg2: Int)
}

struct CommandResult {
    let command: Command
    let data: NSData
}

extension Command {
    func toByteCommand() -> NSData {
        return NSData()
    }
}

// Make sure to setup notifications before subscribing to returned observable!!!
func processCommand(notifyCharacteristic: Characteristic,
                    _ writeCharacteristic: Characteristic,
                    _ command: Command) -> Observable<CommandResult> {

    // This observable will take care of accumulating data received from notifications
    let result = notifyCharacteristic.monitorValueUpdate()
        .takeWhile { characteristic in
            // Your logic which know when to stop reading notifications.
            return true
        }
        .reduce(NSMutableData(), accumulator: { (data, characteristic) -> NSMutableData in
            // Your custom code to append data?
            if let packetData = characteristic.value {
                data.appendData(packetData)
            }
            return data
        })

    // Your code for sending commands, flatmap with more commands if needed or do something similar
    let query = writeCharacteristic.writeValue(command.toByteCommand(), type: .WithResponse)

    return Observable.zip(result, query, resultSelector: { (result: NSMutableData, query: Characteristic) -> CommandResult in
        // This block will be called after query is executed and correct result is collected.
        // You can now return some command specific result.

        return CommandResult(command: command, data: result)
    })
}

// If you would like to serialize multiple commands, you can do for example:
func processMultipleCommands(notifyCharacteristic: Characteristic,
                             writeCharacteristic: Characteristic,
                             commands: [Command]) -> Observable<()> {
    return Observable.from(Observable.just(commands))
        // concatMap would be more appropriate, because in theory we should wait for 
        // flatmap result before processing next command. It's not available in RxSwift yet.
        .flatMap { command in
            return processCommand(notifyCharacteristic, writeCharacteristic, command)
        }
        .map { result in
            return ()
        }
}
枚举命令{ 案例命令1(参数:浮动) 案例命令2(arg:Int,arg2:Int) } 结构命令结果{ 让命令:命令 let data:NSData } 扩展命令{ func toByteCommand()->NSData{ 返回NSData() } } //请确保在订阅返回的observable之前设置通知!!! func processCommand(notifyCharacteristic:Characteristic, _写作特点:特点, _命令:命令)->可观察{ //此可观察对象将负责累积从通知接收到的数据 让结果=notifyCharacteristic.monitorValueUpdate() .takeWhile{中的特征 //您的逻辑知道何时停止读取通知。 返回真值 } .reduce(NSMutableData(),累加器:{(数据,特征)->中的NSMutableData //要附加数据的自定义代码? 如果let packetData=characteristic.value{ data.appendData(packetData) } 返回数据 }) //用于发送命令的代码,如果需要,可以使用带有更多命令的flatmap或执行类似操作 让query=writeCharacteristic.writeValue(command.tobytecond(),类型:。WithResponse) return Observable.zip(result,query,resultSelector:{(result:NSMutableData,query:Characteristic)->CommandResult in //执行查询并收集正确结果后,将调用此块。 //现在可以返回一些特定于命令的结果。 返回CommandResult(命令:命令,数据:结果) }) } //如果要序列化多个命令,可以执行以下操作,例如: func processmultipleCommand(notifyCharacteristic:Characteristic, 写作特点:特点, 命令:[命令]->可观察{ 返回Observable.from(Observable.just(命令)) //concatMap更合适,因为理论上我们应该等待 //在处理下一个命令之前生成flatmap结果。它在RxSwift中还不可用。 .flatMap{中的命令 返回processCommand(notifyCharacteristic、writeCharacteristic、command) } .map{导致 返回() } }
你可以试试上面的。这只是一个你如何处理它的想法。我试着评论最重要的事情。让我知道它是否适合你