How to turn CBUUID into string

只谈情不闲聊 提交于 2019-11-30 03:46:38

I rigged up the following category to do this for CBUUID:

@interface CBUUID (StringExtraction)

- (NSString *)representativeString;

@end

@implementation CBUUID (StringExtraction)

- (NSString *)representativeString;
{
    NSData *data = [self data];

    NSUInteger bytesToConvert = [data length];
    const unsigned char *uuidBytes = [data bytes];
    NSMutableString *outputString = [NSMutableString stringWithCapacity:16];

    for (NSUInteger currentByteIndex = 0; currentByteIndex < bytesToConvert; currentByteIndex++)
    {
        switch (currentByteIndex)
        {
            case 3:
            case 5:
            case 7:
            case 9:[outputString appendFormat:@"%02x-", uuidBytes[currentByteIndex]]; break;
            default:[outputString appendFormat:@"%02x", uuidBytes[currentByteIndex]];
        }

    }

    return outputString;
}

@end

For this input:

NSLog(@"UUID string: %@", [[CBUUID UUIDWithString:@"0bd51666-e7cb-469b-8e4d-2742f1ba77cc"] representativeString]);
NSLog(@"UUID string2: %@", [[CBUUID UUIDWithString:@"1800"] representativeString]);

it produces the following output:

UUID string: 0bd51666-e7cb-469b-8e4d-2742f1ba77cc
UUID string2: 1800

and preserves the appropriate hyphenation for the 16 byte UUIDs, while supporting the simple 2-byte UUIDs.

To all those saying that CBUUID is toll-free bridged with CFUUIDRef, it's not.

CBUUID * foo = [CBUUID UUIDWithString:CBUUIDCharacteristicExtendedPropertiesString];
CFStringRef fooBar = CFUUIDCreateString(NULL, (__bridge CFUUIDRef)foo);
if (![CBUUIDCharacteristicExtendedPropertiesString isEqualToString:(__bridge NSString *)fooBar])
    NSLog(@"fubar!");

It's not crashing but you're getting garbage out. It's probably uniquely identifying garbage, but it can't be round-tripped.

PS: This didn't work as a comment because SO comments oddly don't allow code formatting.

iOS 7.1 (beta released yesterday, 11/18/13) introduced the following property on CBUUID:

@property(nonatomic, readonly) NSString *UUIDString

The UUID represented as a string. (read-only)

From CBUUID Class Reference.

It's also worth noting that for comparing a UUID string with a CBUUID, this works:

if ([cbuuidInQuestion isEqual:[CBUUID UUIDWithString:@"1234-5678-9012-1234"]]) {
    // isEqual tests for "the same UUID"
    // == tests for "the same CBUUID object"
}

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);

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.

Jevgenij Kononov

There is native method in objective C and Swift and it is quite straight forward method.

NSString *str = characteristic.UUID.UUIDstring;

Same thing with Swift language Link to library-> https://developer.apple.com/documentation/corebluetooth/cbuuid/1518742-uuidstring?language=objc

werediver

Brad's answer does its work, but the solution could be simpler (though probably not more efficient) using NSUUID class:

// CBUUID+ToString.h

#import <CoreBluetooth/CoreBluetooth.h>

@interface CBUUID (ToString)

- (NSString *)toString;

@end

// CBUUID+ToString.m

#import "CBUUID+ToString.h"

@implementation CBUUID (ToString)

- (NSString *)toString {
    if ([self respondsToSelector:@selector(UUIDString)]) {
        return [self UUIDString]; // Available since iOS 7.1
    } else {
        return [[[NSUUID alloc] initWithUUIDBytes:[[self data] bytes]] UUIDString]; // iOS 6.0+
    }
}

@end

The following Worked me without any error:

NSString *str = [[NSString alloc] initWithFormat:@"%@",  CFUUIDCreateString(nil, peripheral.UUID) ];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!