0

这已经让我醒了三天:我正在编写一个将 va BT 连接到 Arduino 的小应用程序。为了获得关于连接状态和传输数据的视觉反馈,我使用了一个允许我连接/断开连接以及向我显示状态和数据的视图:

        VStack {
            Text("Glove Training App")
                .font(.title)
            HStack {
                Button(action: { MyBluetoothManager.shared.scan() }) {
                    Text("Connect")
                        .padding(30)
                }
                Text(" | ")
                Button(action: { MyBluetoothManager.shared.disconnect()}) {
                    Text("Disconnect")
                    .padding(30)
                }
            }

            Text(manager.stateChange)
                .font(.subheadline)
                .padding(.bottom, 30)

            Text(peripheral.transmittedString)
            .font(.subheadline)
            .padding(.bottom, 30)
        }
    }

在一个单独的文件中,我拥有所有 BT 管理:

class MyBluetoothManager: NSObject, ObservableObject {
    @Published var stateChange: String = "Initializing..." {
        willSet { objectWillChange.send() }
    }
    static let shared = MyBluetoothManager()

    let central = CBCentralManager(delegate: MyCentralManagerDelegate.shared,
        queue: nil, options: [
        CBCentralManagerOptionRestoreIdentifierKey: restoreIdKey,
        ])
(...)
    func setConnected(peripheral: CBPeripheral) {
        (...)

        state = .connected(peripheral)
        self.stateChange = "Connected"
        print("Connected")
    }
}

class MyPeripheralDelegate: NSObject, ObservableObject, CBPeripheralDelegate {
    let objectWillChange = ObservableObjectPublisher()
    var transmittedString: String = "No data" {
        willSet { objectWillChange.send()
        }
    }
    func peripheral(_ peripheral: CBPeripheral,
            didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
        (...)
        let rxData = characteristic.value
        if let str = NSString(data: rxData!, encoding: String.Encoding.utf8.rawValue) as String? {
            print(str)
            self.transmittedString = str
            let measurement = str.components(separatedBy: "|")
            (...)
        } else {
            print("not a valid UTF-8 sequence")
        }
    }
}

这些值最初设置正确,但从未更新。在终端中,我可以看到打印的值,并且应用程序按预期工作。我正在使用最新版本的 XCode。我看了几个教程,这似乎很棘手。任何帮助将不胜感激。

干杯,基督徒

编辑:这是完整的 BluetoothManager 类(主要不是我的代码,但工作正常):

class MyBluetoothManager: NSObject, ObservableObject {
    @Published var stateChange: String = "Initializing..." {
        willSet { objectWillChange.send() }
    }
    static let shared = MyBluetoothManager()

    let central = CBCentralManager(delegate: MyCentralManagerDelegate.shared,
        queue: nil, options: [
        CBCentralManagerOptionRestoreIdentifierKey: restoreIdKey,
        ])

    var state = State.poweredOff
    enum State {
        case poweredOff
        case restoringConnectingPeripheral(CBPeripheral)
        case restoringConnectedPeripheral(CBPeripheral)
        case disconnected
        case scanning(Countdown)
        case connecting(CBPeripheral, Countdown)
        case discoveringServices(CBPeripheral, Countdown)
        case discoveringCharacteristics(CBPeripheral, Countdown)
        case connected(CBPeripheral)
        case outOfRange(CBPeripheral)

        var peripheral: CBPeripheral? {
            switch self {
            case .poweredOff: return nil
            case .restoringConnectingPeripheral(let p): return p
            case .restoringConnectedPeripheral(let p): return p
            case .disconnected: return nil
            case .scanning: return nil
            case .connecting(let p, _): return p
            case .discoveringServices(let p, _): return p
            case .discoveringCharacteristics(let p, _): return p
            case .connected(let p): return p
            case .outOfRange(let p): return p
            }
        }
    }

    func scan() {
        guard central.state == .poweredOn else {
            self.stateChange = "Cannot scan, BT is not powered on"
            print("Cannot scan, BT is not powered on")
            return
        }

        central.scanForPeripherals(withServices: [myDesiredServiceId], options: nil)
        state = .scanning(Countdown(seconds: 10, closure: {
            self.central.stopScan()
            self.state = .disconnected
            self.stateChange = "Scan timed out"
            print("Scan timed out")

        }))
    }

    func disconnect(forget: Bool = false) {
        if let peripheral = state.peripheral {
            central.cancelPeripheralConnection(peripheral)
        }
        if forget {
            UserDefaults.standard.removeObject(forKey: peripheralIdDefaultsKey)
            UserDefaults.standard.synchronize()
        }
        self.stateChange = "Disconnected"
        state = .disconnected
    }

    func connect(peripheral: CBPeripheral) {
        central.connect(peripheral, options: nil)
        state = .connecting(peripheral, Countdown(seconds: 10, closure: {
            self.central.cancelPeripheralConnection(peripheral)
            self.state = .disconnected
            self.stateChange = "Connect timed out"
            print("Connect timed out")
        }))
    }

    func discoverServices(peripheral: CBPeripheral) {
        peripheral.delegate = MyPeripheralDelegate.shared
        peripheral.discoverServices([myDesiredServiceId])
        state = .discoveringServices(peripheral, Countdown(seconds: 10, closure: {
            self.disconnect()
            self.stateChange = "Could not discover services"
            print("Could not discover services")
        }))
    }

    func discoverCharacteristics(peripheral: CBPeripheral) {
        guard let myDesiredService = peripheral.myDesiredService else {
            self.disconnect()
            return
        }
        peripheral.delegate = MyPeripheralDelegate.shared
        peripheral.discoverCharacteristics([myDesiredCharacteristicId],
            for: myDesiredService)
        state = .discoveringCharacteristics(peripheral, Countdown(seconds: 10,
            closure: {
            self.disconnect()
                self.stateChange = "Could not discover characteristics"
            print("Could not discover characteristics")
        }))
    }

    func setConnected(peripheral: CBPeripheral) {
        guard let myDesiredCharacteristic = peripheral.myDesiredCharacteristic
            else {
                self.stateChange = "Missing characteristic"
                print("Missing characteristic")
                disconnect()
            return
        }

        UserDefaults.standard.set(peripheral.identifier.uuidString,
            forKey: peripheralIdDefaultsKey)
        UserDefaults.standard.synchronize()

        peripheral.delegate = MyPeripheralDelegate.shared
        peripheral.setNotifyValue(true, for: myDesiredCharacteristic)

        state = .connected(peripheral)
        self.stateChange = "Connected"
        print("Connected")
    }
}

4

1 回答 1

0
           Button(action: { MyBluetoothManager.shared.scan() }) {
                Text("Connect")
                    .padding(30)
            }
            Text(" | ")
            Button(action: { MyBluetoothManager.shared.disconnect()}) {
                Text("Disconnect")
                .padding(30)
            }
        }

        Text(manager.stateChange) << why don't you use MyBluetoothManager.shared here ? is there a second instance? this might be the error...but unfortunately you just showed us a small piece of code...
于 2020-04-21T17:28:25.557 回答