如何将CBUUID转换为字符串

时间:2012-11-07 18:28:07

标签: 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 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?
}

8 个答案:

答案 0 :(得分: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。

答案 1 :(得分:7)

对于那些说CBUUID与CFUUIDRef免费联网的人,不是。

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

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

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

答案 2 :(得分:7)

iOS 7.1(昨天发布的测试版,11/18/13)在CBUUID上引入了以下属性:

  

@property(nonatomic, readonly) NSString *UUIDString

     

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

来自CBUUID Class Reference

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

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

答案 3 :(得分:4)

我知道自被询问和回答以来已经过了7个月,但是...... CBUUID被“免费桥接”到CFUUID并且最简单的转换方式是

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

答案 4 :(得分: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.1 UUIDString属性可以,但对于特定的iOS7,上面的扩展是很好的选择。

答案 5 :(得分:0)

目标C和Swift中有本机方法,这是一种非常直接的方法。

NSString *str = characteristic.UUID.UUIDstring;

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

答案 6 :(得分:-1)

Brad的answer做了它的工作,但使用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

答案 7 :(得分:-3)

以下工作没有任何错误:

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