0
votes

The following...

    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
        print(characteristic)
    }

..outputs...

<CBCharacteristic: 0x1700b8180, UUID = FFE1, properties = 0x10, value = <01>, notifying = YES>

I want the "value" part "01".

3
characteristic.value will give a NSData object <01>, but it's up to you to transform that hex into the value you want (string encoding? int value?, etc.). - Larme

3 Answers

1
votes

According to the documentation you can access it by calling: characteristic.value, this will be an object of type Data. Then you can transform this object to string. Like this:

let data = characteristic.value
var dataString = String(data: data, encoding: String.Encoding.utf8)
0
votes

I want to thank OOPer on the Apple Developer Forums for this answer.

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {  
    guard let data = characteristic.value else {  
        return  
    }  
    if data.elementsEqual([0x01]) { //<- You can directly compare a Data to an Array of bytes.  
        //do something  
    }
}
-1
votes

swift: Get value from characteristic while update value.

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {

        let value = characteristic.value

        print(value)
}