ios 如何将CBUID转换为字符串

sg24os4d  于 2023-06-25  发布在  iOS
关注(0)|答案(9)|浏览(101)

我找不到任何正式的方法来从CBUID中获取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?
}
h9vpoimq

h9vpoimq1#

我为CBUID设置了以下类别:

@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。

pvabu6sv

pvabu6sv2#

对于所有说CBUUID是通过CFUUIDRef免费桥接的人来说,事实并非如此。

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

它没有坠毁,但你把垃圾弄出来了。它可能是唯一识别垃圾的,但它不可能是往返。
PS:这并没有作为注解工作,因为奇怪的是,注解不允许代码格式化。

mv1qrgav

mv1qrgav3#

iOS 7.1(昨天发布,11/18/13)在CBUUID上引入了以下属性:
@property(nonatomic, readonly) NSString *UUIDString

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

从CBUID类引用。
同样值得注意的是,对于将UUID字符串与CBUUID进行比较,这是可行的:

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

pod7payv4#

我知道已经有7个月没问过了,但是... CBUUID是“免费桥接”到CFUUID,最简单的转换方法是

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

ecr0jaav5#

以下是布拉德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,上述扩展是很好的选择。

iaqfqrcu

iaqfqrcu6#

Objective C和Swift都有原生方法,而且是非常直接的方法。

NSString *str = characteristic.UUID.UUIDstring;

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

1wnzp6jl

1wnzp6jl7#

Swift使CBUID示例的uuidString属性变得容易。

let x = "B5E4B1AA-A851-4148-BF91-03B9B342295C"
let x_uuid = CBUUID(string: x)        
print(x_uuid.uuidString)

https://developer.apple.com/documentation/corebluetooth/cbuuid/1518742-uuidstring

jjjwad0x

jjjwad0x8#

布拉德的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
xesrikrc

xesrikrc9#

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

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

相关问题