如何把CBUUID成字符串(How to turn CBUUID into string)

2019-07-03 15:53发布

我找不到任何正式的方式来获得一个UUID字符串返回了CBUUID的。 这些的UUID可以是2首或16个字节长。

我们的目标是存储CBUUIDs在某处作为一个字符串文件,然后用[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?
}

Answer 1:

我七拼八凑以下类别为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

对于这个输入:

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。



Answer 2:

所有那些说CBUUID是免费电话与CFUUIDRef桥,事实并非如此。

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

它不崩溃,但你得到垃圾出。 它可能是唯一标识垃圾,但它不能往返式操作。

PS:这并没有为注释工作,因为SO评论奇怪不允许代码格式化。



Answer 3:

的iOS 7.1(测试版昨日公布,13年11月18日)介绍了以下属性CBUUID

@property(nonatomic, readonly) NSString *UUIDString

的UUID表示为一个字符串。 (只读)

从CBUUID类参考 。

另外值得一提的是,用于比较的UUID字符串用CBUUID ,这个工程:

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


Answer 4:

我知道这是已有7月起有人问和回答,但是...... CBUUID是“免费电话桥接”到CFUUID和转换的最简单的方法是

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


Answer 5:

这是布拉德·拉尔森的回答迅速扩展:

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,上面的扩展名是不错的选择。



Answer 6:

有一个在客观C和斯威夫特本地方法,这是很简单的方法。

NSString *str = characteristic.UUID.UUIDstring;

雨燕与语言链接同样的事情库- > https://developer.apple.com/documentation/corebluetooth/cbuuid/1518742-uuidstring?language=objc



Answer 7:

布拉德的答案做的工作,但解决方案可能是简单的(尽管可能不是更有效)使用NSUUID类:

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


Answer 8:

下面曾为我没有任何错误:

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


文章来源: How to turn CBUUID into string