如何将CBUUID转换为string

我找不到任何官方的方式来从一个CBUUID获取一个UUIDstring。 这些UUID可以是2或16个字节。

目标是将CBUUIDs作为string存储在某个文件中,然后使用[CBUUID UUIDWithString:]重新生成。这是迄今为止我所拥有的。

// returns a simple 4 byte string for 16bit uuids, 128 bit uuids are in standard 8-4-4-4-12 format // the resulting string can be passed into [CBUUID UUIDWithString:] +(NSString*)CBUUIDToString:(CBUUID*)cbuuid; { NSData* data = cbuuid.data; if ([data length] == 2) { const unsigned char *tokenBytes = [data bytes]; return [NSString stringWithFormat:@"%02x%02x", tokenBytes[0], tokenBytes[1]]; } else if ([data length] == 16) { NSUUID* nsuuid = [[NSUUID alloc] initWithUUIDBytes:[data bytes]]; return [nsuuid UUIDString]; } return [cbuuid description]; // an error? } 

我为了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 

对于这个input:

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

它会产生以下输出:

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

并保留16字节UUID的适当连字符,同时支持简单的2字节UUID。

对于所有那些认为CBUUID是免费桥接CFUUIDRef的人来说,事实并非如此。

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

这不是崩溃,但你得到垃圾。 这可能是唯一识别垃圾,但不能被往返。

PS:这不作为评论,因为SO评论奇怪不允许代码格式。

iOS 7.1(testing版,昨天,11/18/13)在CBUUID上引入了以下属性:

@property(nonatomic, readonly) NSString *UUIDString

UUID表示为一个string。 (只读)

从CBUUID类参考 。

值得注意的是,为了比较一个UUIDstring和一个CBUUID ,这个工作:

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

我知道这是7个月,因为它被问及答复,但… CBUUID是“免费桥接” CFUUID和最简单的方法是

  //CBUUID* uuid = descr.UUID; NSString* str = CFUUIDCreateString(nil, uuid); 

这是Brad Larson的答案的迅速延伸:

 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 } } 

从iOS 7.1 UUIDString属性是有的,但对于特定的iOS7,以上的扩展是很好的select。

布拉德的答案完成了它的工作,但是使用NSUUID类的解决scheme可能更简单(虽然可能不是更高效):

 // 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 

以下工作没有任何错误:

 NSString *str = [[NSString alloc] initWithFormat:@"%@", CFUUIDCreateString(nil, peripheral.UUID) ];