12

我找不到任何从 CBUUID 中获取 UUID 字符串的官方方法。这些 UUID 可以是 2 或 16 字节长。

目标是将 CBUUID 作为字符串存储在某个文件中的某个位置,然后使用 [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?
}
4

8 回答 8

33

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

于 2012-11-07T23:49:03.667 回答
7

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

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

它没有崩溃,但你正在把垃圾拿出来。它可能是唯一标识垃圾,但不能往返。

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

于 2013-08-19T01:12:03.857 回答
7

iOS 7.1(昨天发布的测试版,2013 年 11 月 18 日)引入了以下属性CBUUID

@property(nonatomic, readonly) NSString *UUIDString

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

来自CBUUID 类参考

还值得注意的是,将 UUID 字符串与 a 进行比较CBUUID,这是有效的:

if ([cbuuidInQuestion isEqual:[CBUUID UUIDWithString:@"1234-5678-9012-1234"]]) {
    // isEqual tests for "the same UUID"
    // == tests for "the same CBUUID object"
}
于 2013-11-20T00:21:41.213 回答
4

我知道它被问和回答已经 7 个月了,但是......CBUUID是“免费桥接”的CFUUID,最简单的转换方法是

 //CBUUID* uuid = descr.UUID;
 NSString* str = CFUUIDCreateString(nil, uuid);
于 2013-02-16T01:14:18.837 回答
2

这是 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.1UUIDString的属性是存在的,但对于特定的 iOS7,上述扩展是不错的选择。

于 2016-06-03T06:21:04.280 回答
0

Objective C 和 Swift 中有本地方法,它是非常直接的方法。

NSString *str = characteristic.UUID.UUIDstring;

与 Swift 语言链接到库-> https://developer.apple.com/documentation/corebluetooth/cbuuid/1518742-uuidstring?language=objc相同

于 2018-04-11T10:33:56.287 回答
-1

布拉德的回答确实有效,但使用类的解决方案可能更简单(尽管可能效率不高)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
于 2014-11-27T10:09:18.927 回答
-3

以下为我工作,没有任何错误:

NSString *str = [[NSString alloc] initWithFormat:@"%@",  CFUUIDCreateString(nil, peripheral.UUID) ];
于 2013-07-16T08:27:47.920 回答