SwiftUI:如何处理 BLE 响应并显示新值
SwiftUI: How to process BLE response and display new values
我有一个 swift 应用程序可以与小型 BLE 设备通信。我可以发送请求并从设备获得答案,但我很难更新 swiftui 视图中显示的值。
这是我尝试过的:
- 有回调:
在 BleConnection.swift 文件中,实现所有 BLE 内容,我声明了一个回调 var onResponse: (([UInt8]) -> Void)? = nil
当从设备收到响应时,数据将通过回调推送到视图:
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
...
if self.onResponse != nil {
self.onResponse!(characteristic.value!.bytes)
}
}
}
}
在 ReaderInformations.swift 文件中,那里有 swiftui 视图,我实现了回调并尝试用 @State var
更新组件显示值但是它没有成功。回调中的 print() 很好地打印在控制台中,但组件未更新。然后我读到只有视图的内部方法可以更新状态变量。
- 结合:
我更新了外围设备(didUpdateValueFor)并发送了 BLE 设备响应,如下所示:
let passThroughSubjectPublisher = PassthroughSubject<[UInt8], Never>()
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
...
passThroughSubjectPublisher.send(characteristic.value!.bytes)
}
}
并且在视图中:
struct ReaderInformations: View {
var ble: BleConnection
@State private var status: String = "status"
private var cancelSet: Set<AnyCancellable> = []
init(bleInstance: BleConnection) {
passThroughSubjectPublisher.sink(receiveValue: { response in. // Escaping closure captures mutating 'self' parameter
switch response[0] {
self.status = "TEST". // This error because of the self
...
}
}).store(in: &cancelSet)
}
我也没有工作,因为我在 init 中尝试访问一个甚至还没有实例化的成员。
所以我不知道该怎么做。你们会如何处理这个问题?
改为在正文中附加发布者的观察者,就像下面的伪代码
struct ReaderInformations: View {
var ble: BleConnection
@State private var status: String = "status"
var body: some View {
VStack { // .. any your view
}
.onReceive(ble.passThroughSubjectPublisher) { response in // << here !!
switch response[0] {
self.status = "TEST"
///...
}
}
}
}
我有一个 swift 应用程序可以与小型 BLE 设备通信。我可以发送请求并从设备获得答案,但我很难更新 swiftui 视图中显示的值。
这是我尝试过的:
- 有回调:
在 BleConnection.swift 文件中,实现所有 BLE 内容,我声明了一个回调 var onResponse: (([UInt8]) -> Void)? = nil
当从设备收到响应时,数据将通过回调推送到视图:
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
...
if self.onResponse != nil {
self.onResponse!(characteristic.value!.bytes)
}
}
}
}
在 ReaderInformations.swift 文件中,那里有 swiftui 视图,我实现了回调并尝试用 @State var
更新组件显示值但是它没有成功。回调中的 print() 很好地打印在控制台中,但组件未更新。然后我读到只有视图的内部方法可以更新状态变量。
- 结合: 我更新了外围设备(didUpdateValueFor)并发送了 BLE 设备响应,如下所示:
let passThroughSubjectPublisher = PassthroughSubject<[UInt8], Never>()
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
...
passThroughSubjectPublisher.send(characteristic.value!.bytes)
}
}
并且在视图中:
struct ReaderInformations: View {
var ble: BleConnection
@State private var status: String = "status"
private var cancelSet: Set<AnyCancellable> = []
init(bleInstance: BleConnection) {
passThroughSubjectPublisher.sink(receiveValue: { response in. // Escaping closure captures mutating 'self' parameter
switch response[0] {
self.status = "TEST". // This error because of the self
...
}
}).store(in: &cancelSet)
}
我也没有工作,因为我在 init 中尝试访问一个甚至还没有实例化的成员。
所以我不知道该怎么做。你们会如何处理这个问题?
改为在正文中附加发布者的观察者,就像下面的伪代码
struct ReaderInformations: View {
var ble: BleConnection
@State private var status: String = "status"
var body: some View {
VStack { // .. any your view
}
.onReceive(ble.passThroughSubjectPublisher) { response in // << here !!
switch response[0] {
self.status = "TEST"
///...
}
}
}
}