Search code examples
swiftuuidcore-bluetooth

Core bluetooth create CBUUID with data


So I'm pretty new at the whole bluetooth stuff and I've been making a project and everything worked out fine so far, but when I got the UUIDS that we are supposed to use I got something like this:

0xD7, 0x36, 0x95, 0x0A, 0x4D, 0x6E, 0x12, 0x26, 0x80, 0x3A, 0x00, 0x50, 0xE4, 0xC0, 0x00, 0x67

when I've been using strings so far, I assume that it should be done with the data constructor for CBUUID but no idea how to actually do it, anyone knows a way to either convert those values to a string uuid or just use them as is to create a CBUUID?


Solution

  • You just need to initialize a new Data object with your bytes and pass the data object to the CBUUID data initializer:

    let bytes: [UInt8] = [0xD7, 0x36, 0x95, 0x0A, 0x4D, 0x6E, 0x12, 0x26, 0x80, 0x3A, 0x00, 0x50, 0xE4, 0xC0, 0x00, 0x67]
    let data = Data(bytes)
    let cbuuid = CBUUID(data: data)
    

    or simply:

    let cbuuid = CBUUID(data: Data([0xD7, 0x36, 0x95, 0x0A, 0x4D, 0x6E, 0x12, 0x26, 0x80, 0x3A, 0x00, 0x50, 0xE4, 0xC0, 0x00, 0x67]))