Ios 如何将CBUUID转换为字符串

Ios 如何将CBUUID转换为字符串,ios,core-bluetooth,bluetooth-lowenergy,Ios,Core Bluetooth,Bluetooth Lowenergy,我找不到任何官方方法从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中获取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?
}

我为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
它产生以下输出:

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

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

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

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

以下内容使我工作顺利,没有任何错误:

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

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

CBUUID * foo = [CBUUID UUIDWithString:CBUUIDCharacteristicExtendedPropertiesString];
CFStringRef fooBar = CFUUIDCreateString(NULL, (__bridge CFUUIDRef)foo);
if (![CBUUIDCharacteristicExtendedPropertiesString isEqualToString:(__bridge NSString *)fooBar])
    NSLog(@"fubar!");
它没有崩溃,但你正在把垃圾弄出去。它可能是唯一标识垃圾的方法,但它不能被循环

PS:这不能用作注释,因为注释奇怪地不允许代码格式化。

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

@属性(非原子,只读)NSString*UUIString

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

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

if ([cbuuidInQuestion isEqual:[CBUUID UUIDWithString:@"1234-5678-9012-1234"]]) {
    // isEqual tests for "the same UUID"
    // == tests for "the same CBUUID object"
}
Brad's完成了它的工作,但是使用
nsuid
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
//CBUUID+ToString.h
#进口
@接口CBUUID(ToString)
-(NSString*)toString;
@结束
//CBUUID+ToString.m
#导入“CBUUID+ToString.h”
@实现CBUUID(ToString)
-(NSString*)toString{
if([self respondsToSelector:@selector(uuistring)]){
return[self uuistring];//自iOS 7.1起可用
}否则{
返回[[[nsuid alloc]initWithUUIDBytes:[[self data]字节]]UUIDString];//iOS 6.0+
}
}
@结束

以下是布拉德·拉森答案的快速扩展:

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
    }
}
导入核心蓝牙
扩展CBUUID{
func representativeString()->字符串{
让data=self.data
let bytesToConvert=data.length
设uuidBytes=UnsafePointer(data.bytes)
var outputString=String()

对于0..中的currentByteIndex,目标C和Swift中有本地方法,这是一种非常直接的方法

NSString *str = characteristic.UUID.UUIDstring;
同样的事情也发生在敏捷的语言上 链接到库->

喜欢它。它有点复杂,但它可以处理其他长度。但是CBUUID也被记录为16位或128位。工作得很好!我只是复制并粘贴到我的.m文件中@interface之前,还有viola!这对两个字节长的CBUUID有效吗?我不知道怎么做,因为它们只是结构。它们不是t免费桥接。CFUUID是简单的结构,而CBUUID是一个NSObject。它们之间有很大的区别。我很高兴你自己找到了它。对我们其他人来说,它只是工作。有人能指出这是在哪里记录的吗?Cocoa基础指南文档中既没有CBUUID头,也没有免费桥接表?我想你会发现它不是免费桥接的。请看我的答案。这是因为外围UUID不是CBUUID。CBUUID不是CFUUID。当递给CBUUID时,它会爆炸。请查看Biofe的回复。不确定为什么这一个被否决-马克,你使用的是Brad类别的解决方案吗?苹果刚刚让所有这些答案过时了iOS 7.1。请看下面我的答案。有人知道UUIDString方法是否是7.0中未记录的方法吗?UUIDString属性已从文档中删除!@thefinestist,没有。它尚未合并到公共文档中。我包含的链接是开发人员预览;您需要一个Apple开发人员帐户才能查看。或者,如果您只是想查看相信我的话,
UUIDString
将在不久的将来成为一个属性。如果你的目标是iOS 7.0而不是7.1,你可以通过
[[nsuid alloc]initWithUUIDBytes:cbuuid.data.bytes]UUIDString]获得字符串。
。我想指出这一点——在MacOSX上,这仍然很困难(10.9),@0xced的解决方案对meOSX 10.10(约塞米蒂)真的很有帮助,而且还有
uuiString
可用
initWithuuiBytes:
需要8个字节的数据。对于16位CBUUID,这是未定义的行为。“CBUUID类的实例代表128位通用唯一标识符,”因此,代码是有效的。此答案不再有效,data.bytes已被弃用
NSString *str = characteristic.UUID.UUIDstring;