HomeiOS Developmentios - How one can parse BLE notifications with CoreBluetooth and completionHandler...

ios – How one can parse BLE notifications with CoreBluetooth and completionHandler in Swift?


I’ve so as to add a BLE system assist in my easy undertaking written in Swift UI.

Mannequin: Telephone sends a request (.withoutResponse) <-> BLE Machine solutions by BLE notification.

As I’m not knowledgeable Swift programmer my primary drawback is the best way to parse these notifications by way of code to execute one other perform (f.e. save system parameters to CoreData/UserDefaults if Response is OK).

func saveDevice(identifier: UUID, title: String, kind: Int) { 
        
        writeToIdentifier(identifier: identifier, knowledge: Request.deviceSave(title: title, kind: kind))

        //!
        CoreDataModel.shared.saveDevice(uuid: identifier, title: title, kind: Int16(kind)) // I would like to finish/or not this perform once we get notification from system
    }

func writeToIdentifier(identifier: UUID, knowledge: Information) {
        
        print("Writing to identifier: ", identifier)
        if let index = self.gadgets.firstIndex(the place: {$0.identifier == identifier}) {
            let peripheral = self.gadgets[index].peripheral
            let attribute = peripheral.providers?.first?.traits?[1]
            peripheral.writeValue(knowledge, for: attribute!, kind: .withoutResponse)
        }
    }
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor attribute: CBCharacteristic, error: Error?) {
        if let worth = attribute.worth {
            parseResponse(knowledge: worth, identifier: peripheral.identifier)
        }
    }
func parseResponse(knowledge: Information, identifier: UUID) {
        
        let technique: UInt8 = knowledge.first!
        
        swap (technique) {
        case 1:                             
            print("Methodology: DEVICE SAVE")
            if (knowledge[5] == 1) {              // Right here we get response and solely now we might like to save lots of the system to CoreData
                print("Is configured: true")
            } else {
                print("Is configured: false")
            }
        }
} 

I’ve already began to be taught swift asynchronous programming however I needed to ask you for assist.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments