Advertisement
jannis6023

Untitled

Dec 10th, 2021 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.55 KB | None | 0 0
  1.  
  2. import Foundation
  3. import CoreBluetooth
  4.  
  5. struct Peripheral: Identifiable {
  6.     let id: Int
  7.     let name: String
  8.     let rssi: Int
  9. }
  10.  
  11. class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate {
  12.  
  13.     var myCentral: CBCentralManager!
  14.    
  15.     @Published var serviceUUID = CBUUID(string: "----redacted for privacy----")
  16.     @Published var characteristicUUID = CBUUID(string: "----redacted for privacy----")
  17.  
  18.     @Published var isSwitchedOn = false
  19.     @Published var peripherals = [CBPeripheral]()
  20.     @Published var isScanning = false
  21.     @Published var settingCharacteristic: CBCharacteristic?
  22.    
  23.     var recievedData = Data()
  24.    
  25.     @Published var settingData: NetworkSettings? {
  26.         willSet{
  27.             objectWillChange.send()
  28.         }
  29.     }
  30.    
  31.  
  32.     override init() {
  33.         super.init()
  34.  
  35.         myCentral = CBCentralManager(delegate: self, queue: nil)
  36.         myCentral.delegate = self
  37.         isScanning = myCentral.isScanning
  38.     }
  39.  
  40.     // Ran if state of CBCM updates
  41.     func centralManagerDidUpdateState(_ central: CBCentralManager) {
  42.         if central.state == .poweredOn {
  43.             isSwitchedOn = true
  44.         }
  45.         else {
  46.             isSwitchedOn = false
  47.         }
  48.     }
  49.    
  50.    
  51.     // Ran if devices discovered
  52.     func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
  53.        
  54.         //print(advertisementData)
  55.        
  56.         if(peripherals.contains(where: {peri in peri == peripheral})){
  57.             let index = peripherals.firstIndex(where: {peri in peri == peripheral})
  58.             peripherals[index!] = peripheral
  59.         }else{
  60.             peripherals.append(peripheral)
  61.         }
  62.        
  63.     }
  64.    
  65.     func startScanning() {
  66.         print("startScanning")
  67.         myCentral.scanForPeripherals(withServices: [serviceUUID], options: [CBCentralManagerScanOptionAllowDuplicatesKey: true, CBCentralManagerScanOptionSolicitedServiceUUIDsKey: [serviceUUID]])
  68.         isScanning = myCentral.isScanning
  69.     }
  70.    
  71.     func stopScanning() {
  72.         print("stopScanning")
  73.         myCentral.stopScan()
  74.         isScanning = myCentral.isScanning
  75.     }
  76.    
  77.     // Connect to device after connected
  78.     func connect(peripheral: CBPeripheral){
  79.         myCentral.connect(peripheral, options: nil)
  80.     }
  81.    
  82.     func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
  83.         print("Failed to connect to pi.")
  84.     }
  85. }
  86.  
  87. extension BLEManager: CBPeripheralDelegate{
  88.    
  89.     // .... here is some BLE stuff - not needed for error info
  90.    
  91.     // Got setting data
  92.     func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
  93.         // Deal with errors (if any)
  94.         if let error = error {
  95.             print("Error discovering characteristics: " + error.localizedDescription)
  96.             return
  97.         }
  98.        
  99.         guard let characteristicData = characteristic.value else {return}
  100.        
  101.         print("Received bytes: \(characteristicData.count)")
  102.         print(String(decoding: characteristicData, as: UTF8.self))
  103.        
  104.         // HERE has to happen the magic: The value is updated from nil to a NetworkSettings struct / instance - but nothing re-renders in SwiftUI!
  105.         self.settingData = getNetworkSettingsFromString(data: String(decoding: characteristicData, as: UTF8.self))
  106.         // Already treid using this - no change
  107.         // objectWillChange.send()
  108.        
  109.     }
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement