Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- import CoreBluetooth
- struct Peripheral: Identifiable {
- let id: Int
- let name: String
- let rssi: Int
- }
- class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate {
- var myCentral: CBCentralManager!
- @Published var serviceUUID = CBUUID(string: "----redacted for privacy----")
- @Published var characteristicUUID = CBUUID(string: "----redacted for privacy----")
- @Published var isSwitchedOn = false
- @Published var peripherals = [CBPeripheral]()
- @Published var isScanning = false
- @Published var settingCharacteristic: CBCharacteristic?
- var recievedData = Data()
- @Published var settingData: NetworkSettings? {
- willSet{
- objectWillChange.send()
- }
- }
- override init() {
- super.init()
- myCentral = CBCentralManager(delegate: self, queue: nil)
- myCentral.delegate = self
- isScanning = myCentral.isScanning
- }
- // Ran if state of CBCM updates
- func centralManagerDidUpdateState(_ central: CBCentralManager) {
- if central.state == .poweredOn {
- isSwitchedOn = true
- }
- else {
- isSwitchedOn = false
- }
- }
- // Ran if devices discovered
- func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
- //print(advertisementData)
- if(peripherals.contains(where: {peri in peri == peripheral})){
- let index = peripherals.firstIndex(where: {peri in peri == peripheral})
- peripherals[index!] = peripheral
- }else{
- peripherals.append(peripheral)
- }
- }
- func startScanning() {
- print("startScanning")
- myCentral.scanForPeripherals(withServices: [serviceUUID], options: [CBCentralManagerScanOptionAllowDuplicatesKey: true, CBCentralManagerScanOptionSolicitedServiceUUIDsKey: [serviceUUID]])
- isScanning = myCentral.isScanning
- }
- func stopScanning() {
- print("stopScanning")
- myCentral.stopScan()
- isScanning = myCentral.isScanning
- }
- // Connect to device after connected
- func connect(peripheral: CBPeripheral){
- myCentral.connect(peripheral, options: nil)
- }
- func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
- print("Failed to connect to pi.")
- }
- }
- extension BLEManager: CBPeripheralDelegate{
- // .... here is some BLE stuff - not needed for error info
- // Got setting data
- func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
- // Deal with errors (if any)
- if let error = error {
- print("Error discovering characteristics: " + error.localizedDescription)
- return
- }
- guard let characteristicData = characteristic.value else {return}
- print("Received bytes: \(characteristicData.count)")
- print(String(decoding: characteristicData, as: UTF8.self))
- // HERE has to happen the magic: The value is updated from nil to a NetworkSettings struct / instance - but nothing re-renders in SwiftUI!
- self.settingData = getNetworkSettingsFromString(data: String(decoding: characteristicData, as: UTF8.self))
- // Already treid using this - no change
- // objectWillChange.send()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement