我找不到任何正式的方法来从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?
}
9条答案
按热度按时间h9vpoimq1#
我为CBUID设置了以下类别:
对于此输入:
它产生以下输出:
并为16字节UUID保留适当的断字,同时支持简单的2字节UUID。
pvabu6sv2#
对于所有说CBUUID是通过CFUUIDRef免费桥接的人来说,事实并非如此。
它没有坠毁,但你把垃圾弄出来了。它可能是唯一识别垃圾的,但它不可能是往返。
PS:这并没有作为注解工作,因为奇怪的是,注解不允许代码格式化。
mv1qrgav3#
iOS 7.1(昨天发布,11/18/13)在
CBUUID
上引入了以下属性:@property(nonatomic, readonly) NSString *UUIDString
从CBUID类引用。
同样值得注意的是,对于将UUID字符串与
CBUUID
进行比较,这是可行的:pod7payv4#
我知道已经有7个月没问过了,但是...
CBUUID
是“免费桥接”到CFUUID
,最简单的转换方法是ecr0jaav5#
以下是布拉德Larson的回答:
从iOS 7.1
UUIDString
属性是存在的,但对于特定的iOS7,上述扩展是很好的选择。iaqfqrcu6#
Objective C和Swift都有原生方法,而且是非常直接的方法。
Swift语言的链接到库-> https://developer.apple.com/documentation/corebluetooth/cbuuid/1518742-uuidstring?language=objc
1wnzp6jl7#
Swift使CBUID示例的
uuidString
属性变得容易。https://developer.apple.com/documentation/corebluetooth/cbuuid/1518742-uuidstring
jjjwad0x8#
布拉德的answer完成了它的工作,但是使用
NSUUID
类的解决方案可以更简单(尽管可能不会更高效):xesrikrc9#
以下是我工作没有任何错误: