iOS iBeacon:如何以编程方式获取所有的proximityUUID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21163290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS iBeacon: How to get all of proximityUUID programmatically?
提问by zono
I want to see all of proximityUUID of advertising packets programmatically. Some articles say that it is impossible on iOS but Android is possible. But I cannot believe it because I found the fantastic app "BLExplr" has the feature. I need to implement the function into my app. Does anyone knows how to do it or good examples? Any help will be appreciated.
我想以编程方式查看广告数据包的所有接近UUID。有些文章说在 iOS 上是不可能的,但在 Android 上是可能的。但我不敢相信,因为我发现出色的应用程序“BLExplr”具有该功能。我需要在我的应用程序中实现该功能。有谁知道怎么做或很好的例子?任何帮助将不胜感激。
(UPDATE 2014/1/17)
(更新 2014/1/17)
I believe @davidgyoung answer is right. Estimote beacon's proximityUUID is "B9407F30-F5F8-466E-AFF9-25556B57FE6D" but displayed my Estimote beacon's UUID on BLExplr app is another ID.
我相信@davidgyoung 的回答是正确的。Estimote信标的proximityUUID是“B9407F30-F5F8-466E-AFF9-25556B57FE6D”但在BLExplr应用程序上显示我的Estimote信标的UUID是另一个ID。
回答by davidgyoung
Unfortunately, you cannot to this on iOS. When you say that BLExplr and LightBlue can do this, you are confusing the Bluetooth service UUID with the iBeacon Proximity UUID. These are two very different things.
不幸的是,你不能在 iOS 上做到这一点。当您说 BLExplr 和 LightBlue 可以做到这一点时,您将蓝牙服务 UUID 与 iBeacon Proximity UUID 混淆了。这是两件非常不同的事情。
The Bluetooth service UUID is visible to iOS, but has nothing to do with an iBeacon's identifiers, and is useless for working with iBeacons. The service UUID is generated by iOS each time a bluetooth device is seen, and stays the same only for the duration of time the bluetooth device is in range. If you take a bluetooth device away and bring it back later, it will have a different service UUID.
蓝牙服务 UUID 对 iOS 可见,但与 iBeacon 的标识符无关,对 iBeacon 工作无用。每次看到蓝牙设备时,服务 UUID 都会由 iOS 生成,并且仅在蓝牙设备在范围内的持续时间内保持不变。如果您将蓝牙设备拿走并稍后再带回来,它将具有不同的服务 UUID。
An iBeacon's identifiers (ProximityUUID, Major, Minor) are embedded inside the body of the Bluetooth advertisement. The problem on iOS devices is that Apple's CoreBluetooth APIs disallow access to the raw advertisement body, so no third-party app is able to read these identifiers. Apple only allows access to these identifiers using the special iBeacon CoreLocation APIs, but these APIs require you to know the Proximity UUID up front.
iBeacon 的标识符(ProximityUUID、Major、Minor)嵌入在蓝牙广告的正文中。iOS 设备上的问题是 Apple 的 CoreBluetooth API 不允许访问原始广告正文,因此没有第三方应用程序能够读取这些标识符。Apple 仅允许使用特殊的 iBeacon CoreLocation API 访问这些标识符,但这些 API 要求您预先知道 Proximity UUID。
Sorry, I know this is not the answer you want to hear! (I'm sorry about it, too!) For what it's worth, you can do this on Android, on OSX Mavericks and Linux.
对不起,我知道这不是你想听到的答案!(我也很抱歉!)不管怎样,您可以在 Android、OSX Mavericks 和 Linux 上执行此操作。
See details here.
在此处查看详细信息。
回答by Nate Symer
davidgyoungis partially wrong about not being able to get iBeacon info. You actually canget the proximity UUID on OS X, but not iOS.
davidgyoung关于无法获得 iBeacon 信息的部分错误。您实际上可以在 OS X 上获得接近度 UUID,但不能在 iOS 上获得。
In a CBPeripheral
's advertisingData
, there should be a key called kCBAdvDataManufacturerData
; It is an NSData
representing the iBeacon advertising information. This key is only available on OS X.
在CBPeripheral
的advertisingData
,应该有一个叫做关键kCBAdvDataManufacturerData
; 它是一个NSData
代表 iBeacon 的广告信息。此密钥仅在 OS X 上可用。
Check that the 2nd byte is equal to 0x02
, the 1st two bytes are equal to 0x004c
(76
in decimal), and the 4th byte (in decimal) + 4 equals the data's length (should be 25).
检查第二个字节是否等于0x02
,第一个两个字节等于0x004c
(76
十进制),第四个字节(十进制)+ 4 等于数据的长度(应为 25)。
NSRanges (sorry for Mac syntax)
Proximity UUID: NSMakeRange(4, 16)
Major: NSMakeRange(20,2)
Minor: NSMakeRange(22,2)
NSRanges(抱歉 Mac 语法)
Proximity UUID: NSMakeRange(4, 16)
Major: NSMakeRange(20,2)
Minor:NSMakeRange(22,2)
To make sure you're doing it right, you can log the values as hex (use the format string %x
) and make sure they match the description
of the NSData
from whence they came.
为了确保你正在做的是正确的,你可以登录值十六进制(使用格式字符串%x
),并确保它们匹配description
的NSData
从那里他们来到。
回答by Jeanette Müller
NSRange uuidRange = NSMakeRange(4, 16);
NSRange majorRange = NSMakeRange(20, 2);
NSRange minorRange = NSMakeRange(22, 2);
NSRange powerRange = NSMakeRange(24, 1);
Byte uuidBytes[16];
[data getBytes:&uuidBytes range:uuidRange];
NSUUID *uuid = [[NSUUID alloc] initWithUUIDBytes:uuidBytes];
int16_t majorBytes;
[data getBytes:&majorBytes range:majorRange];
int16_t majorBytesBig = (majorBytes >> 8) | (majorBytes << 8);
int16_t minorBytes;
[data getBytes:&minorBytes range:minorRange];
int16_t minorBytesBig = (minorBytes >> 8) | (minorBytes << 8);
int8_t powerByte;
[data getBytes:&powerByte range:powerRange];
return @{ @"uuid" : uuid,
@"major" : @(majorBytesBig),
@"minor" : @(minorBytesBig),
@"power" : @(powerByte)
};
but the uuid is the DeviceUUID, not the ProximityUUID
但 uuid 是 DeviceUUID,而不是 ProximityUUID