簡體   English   中英

如何將CBUUID轉換為字符串

[英]How to turn CBUUID into string

我找不到從CBUUID中取回UUID字符串的任何官方方法。 這些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?
}

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

對於所有說CBUUID是與CFUUIDRef橋接的免費電話的人來說,並非如此。

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

它沒有崩潰,但您正在浪費垃圾。 它可能唯一地標識了垃圾,但不能往返。

PS:這不能用作注釋,因為SO注釋奇怪地不允許代碼格式化。

iOS 7.1(昨天發布的Beta,11/18/13)在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"
}

我知道CBUUID要求和回答已經7個月了,但是... CBUUID是“免費橋接”到CFUUID ,最簡單的轉換方法是

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

這是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,可以使用上述擴展名。

目標C和Swift中都有本機方法,這是相當簡單的方法。

NSString *str = characteristic.UUID.UUIDstring;

Swift語言也是如此鏈接到庫-> https://developer.apple.com/documentation/corebluetooth/cbuuid/1518742-uuidstring?language=objc

布拉德的答案可以完成工作,但是使用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

以下工作對我沒有任何錯誤:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM