com*_*tyr 2 bluetooth-lowenergy ibeacon ibeacon-android beacon
我想使用 Apple 和 Google 的新 API 来支持此API 文档中所描述的Covid 联系人跟踪。但是当我尝试在 XCode 中使用这些 API 时,找不到这些类:
let request = CTSelfTracingInfoRequest()
Run Code Online (Sandbox Code Playgroud)
如何启用这些 API?
iOS 的 API 受到限制。 虽然您可以使用 XCode 11.5 和 iOS 13.5 针对 ExposureNotifcation 框架编写代码,但如果没有 Apple 授予您具有com.apple.developer.exposure-notification权利的配置文件,您甚至无法在模拟器中运行代码。Apple 仅在手动批准程序后才向与政府卫生机构相关的开发人员授予该权利。
以下是有关无需 Apple 特殊许可您可以做什么的更多信息。
13.5 之前的 iOS 版本不允许传输规范中的曝光通知服务信标蓝牙广告格式。从 13.5 开始,广告只能通过操作系统进行——第 3 方应用程序无法在不使用更高级别 API 的情况下发出该广告。
从 iOS 13.5 开始,Apple 还阻止第三方应用程序直接检测这种信标格式,迫使它们使用更高级别的 API。早期版本的 iOS 确实允许检测这种信标格式。
然而,Android 是另一回事。
虽然谷歌也同样限制使用在谷歌Play服务这些API与来自谷歌授予特殊权限的API密钥,Android的版本5.0及更高版本允许第三方应用程序来发送和检测曝光通知服务信广告是蓝牙规范的设想:
使用免费和开源的Android Beacon Library 2.17+,你可以像这样传输这个信标:
String uuidString = "01020304-0506-0708-090a-0b0c0d0e0f10";
Beacon beacon = new Beacon.Builder()
.setId1(uuidString)
.build();
// This beacon layout is for the Exposure Notification Service Bluetooth Spec
BeaconParser contactDetectionBeaconParser = new BeaconParser()
.setBeaconLayout("s:0-1=fd6f,p:-:-59,i:2-17");
BeaconTransmitter beaconTransmitter = new
BeaconTransmitter(getApplicationContext(), contactDetectionBeaconParser);
beaconTransmitter.startAdvertising(beacon
Run Code Online (Sandbox Code Playgroud)
并像这样扫描它:
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("s:0-1=fd6f,p:-:-59,i:2-17"));
...
beaconManager.startRangingBeaconsInRegion(new Region("All Exposure Notification Service beacons", null));
...
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
for (Beacon beacon: beacons) {
Log.i(TAG, "I see an Exposure Notification Service beacon with rolling proximity identifier "+beacon.getId1());
}
}
Run Code Online (Sandbox Code Playgroud)
在Android上,即使在后台也可以进行上述传输和检测。有关详细信息,请参阅库文档。
BeaconScope Android 应用程序内置了发送和接收暴露通知服务信标的功能。您可以将其用作帮助测试您构建的任何应用程序的工具。
您可以在我的博客文章中阅读更多内容,该文章向您展示了如何构建自己的应用程序来执行此操作。
对于 iOS,虽然在撰写本文时无法进行传输,但您可以使用以下代码在 iOS 13.4.x 及更早版本上扫描这些信标:
let exposureNotificationServiceUuid = CBUUID(string: "FD6F")
centralManager?.scanForPeripherals(withServices: [exposureNotificationServiceUuid], options: [CBCentralManagerScanOptionAllowDuplicatesKey: true])
...
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
if let advDatas = advertisementData[CBAdvertisementDataServiceDataKey] as? NSDictionary {
if let advData = advDatas.object(forKey: CBUUID(string: "FD6F")) as? Data {
let hexString = advData.map { String(format: "%02hhx", $0) }.joined()
let proximityId = String(hexString.prefix(32))
let metadata = hexString.suffix(8)
NSLog("Discovered Exposure Notification Service Beacon with Proximity ID\(proximityId), metadata \(metadata) and RSSI \(RSSI)")
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是请注意,Apple 从 iOS 13.5 beta 2 开始阻止了此功能。didDiscover对于具有曝光通知服务 UUID 的广告,永远不会调用上述方法。
完全披露:我是 Android Beacon 库开源项目的首席开发人员,也是基于该库构建的 BeaconScope 应用程序的作者。
编辑 2020 年 4 月 26 日:更新了上面的答案以链接到修订后的 1.1 版暴露通知服务蓝牙规范,从该更改中更新命名约定,并修改代码示例以显示元数据。
编辑 2020 年 4 月 30 日:根据 Apple 发布的 iOS 13.5 beta 2 和 XCode 11.5 beta 更新答案,以及 Apple 现在阻止 3rd 方应用程序检测曝光通知服务信标的事实。
编辑 2020 年 6 月 2 日:根据 Apple 最终发布的 iOS 13.5 和 Google 发布的 Google Play 服务更新了答案。
| 归档时间: |
|
| 查看次数: |
1093 次 |
| 最近记录: |