How to turn CBUUID into string

前端 未结 8 1002
太阳男子
太阳男子 2020-12-28 20:11

I can\'t find any official way to get a UUID string back out of a CBUUID. These UUIDs can be 2 or 16 bytes long.

The goal is to store CBUUIDs in a file somewhere as

相关标签:
8条回答
  • 2020-12-28 20:56

    Here is swift extension of Brad Larson's answer :

    import CoreBluetooth
    
    extension CBUUID {
    
        func representativeString() -> String {
            let data = self.data
    
            let bytesToConvert = data.length
            let uuidBytes = UnsafePointer<CUnsignedChar>(data.bytes)
            var outputString = String()
    
            for currentByteIndex in 0..<bytesToConvert {
                switch currentByteIndex {
                case 3,5,7,9:
                    outputString += String(format: "%02x-",uuidBytes[currentByteIndex])
                default:
                    outputString += String(format: "%02x",uuidBytes[currentByteIndex])
                }
            }
    
            return outputString
        }
    }
    

    From iOS 7.1 UUIDString property is there but for specific iOS7, above extension is good option.

    0 讨论(0)
  • 2020-12-28 20:59

    I know it's been 7 month since it was asked and answered, but... CBUUID is “toll-free bridged” to CFUUID and the easiest way to convert is

     //CBUUID* uuid = descr.UUID;
     NSString* str = CFUUIDCreateString(nil, uuid);
    
    0 讨论(0)
提交回复
热议问题