无法在 macOS 上扫描 iBeacons
Unable to scan for iBeacons on macOS
我正在 MacOS 上尝试一个非常简单的 AppKit 应用程序,它显示附近的 iBeacons。
虽然我的系统上似乎从来没有可用的区域扫描。我做错了什么吗?
仅供参考:我的系统是 MacBook Pro(16 英寸,2019 年)运行 Catalina (10.15.7)
目标在 'App Sandbox' 功能部分启用了 'Bluetooth' 和 'App Location',在 'Hardened Runtime' 下启用了 'Location'。
下面的代码总是给出以下内容:
LM Auth Status is now: Authorised Always
Beacon monitoring is not available
starting scanning
region monitoring failed: Error Domain=kCLErrorDomain Code=5 "(null)"
// ViewController.swift
// Beacons
import Cocoa
import CoreLocation
class ViewController: NSViewController, CLLocationManagerDelegate {
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.requestAlwaysAuthorization()
locationManager.delegate = self
}
@IBAction func buttonTapped(_ sender: NSButton) {
self.startScanning()
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
func startScanning() {
print("starting scanning")
//UUID Obscured for forum post...
guard let uuid = UUID(uuidString: "xxxxxxxx-E4A1-4720-9034-xxxxxxxxxxxx") else {
print("Couldn't make UUID")
return
}
let beaconID = "com.monkeyfood.myBeaconRegion"
let beaconRegion = CLBeaconRegion(uuid: uuid, identifier: beaconID)
// let beaconRegion = CLBeaconRegion(uuid: UUID(), identifier: beaconID)
self.locationManager.startMonitoring(for: beaconRegion)
}
//. CLLocationManagerDelegate Methods
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
var newStatus = ""
switch status {
case .notDetermined:
newStatus = "Not determined"
case .restricted:
newStatus = "Restricted"
case .denied:
newStatus = "Denied"
case .authorizedAlways:
newStatus = "Authorised Always"
case .authorized:
newStatus = "Authorised"
default:
newStatus = "What?"
}
print("LM Auth Status is now: \(newStatus)")
// Check for iBeacon monitoring status
let regionMonitoringIsAvailable = CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self)
let not = regionMonitoringIsAvailable ? "" : "not "
print("Beacon monitoring is \(not)available")
}
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
if beacons.count > 0 {
// updateDistance(beacons[0].proximity)
print("\(beacons.count) beacons found.")
print("\(beacons[0].proximity) dist.")
} else {
// updateDistance(.unknown)
}
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("region entered... \(region)")
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
print("region exited... \(region)")
}
func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
print("determined region state")
}
func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
print("region monitoring failed: \(error)")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("LM failed \(error)")
}
}
我不认为使用 CoreLocation 进行 Beacon 监控可以在任何 MacOS 版本上运行。
我查看了 class 文档以确认这一点,并且惊讶地发现 CLBeacon class 文档确实说它在 MacOS 10.15+ 上受支持。我不知道这是不是一个错误,或者只是表明适用于 MacOS 10.15+ 的 SDK 支持这些 classes,即使您不能真正使用它们。
我上一次在 MacOS 上使用这些 API 是在 2018 年,当时 MacOS 2.14 是新的。那时,我必须使用 CoreBluetooth 在 MacOS 上进行原始 BLE 广告扫描并自己解析信标。我怀疑从那以后有什么改变。我不记得听说过任何关于将信标支持添加到 MacOS 的消息,现在只是快速搜索,我找不到从 WWDC 2019 开始的任何讨论。我怀疑我错过了这样的变化,但我会很高兴得知我做到了!
我正在 MacOS 上尝试一个非常简单的 AppKit 应用程序,它显示附近的 iBeacons。
虽然我的系统上似乎从来没有可用的区域扫描。我做错了什么吗?
仅供参考:我的系统是 MacBook Pro(16 英寸,2019 年)运行 Catalina (10.15.7)
目标在 'App Sandbox' 功能部分启用了 'Bluetooth' 和 'App Location',在 'Hardened Runtime' 下启用了 'Location'。
下面的代码总是给出以下内容:
LM Auth Status is now: Authorised Always
Beacon monitoring is not available
starting scanning
region monitoring failed: Error Domain=kCLErrorDomain Code=5 "(null)"
// ViewController.swift
// Beacons
import Cocoa
import CoreLocation
class ViewController: NSViewController, CLLocationManagerDelegate {
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.requestAlwaysAuthorization()
locationManager.delegate = self
}
@IBAction func buttonTapped(_ sender: NSButton) {
self.startScanning()
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
func startScanning() {
print("starting scanning")
//UUID Obscured for forum post...
guard let uuid = UUID(uuidString: "xxxxxxxx-E4A1-4720-9034-xxxxxxxxxxxx") else {
print("Couldn't make UUID")
return
}
let beaconID = "com.monkeyfood.myBeaconRegion"
let beaconRegion = CLBeaconRegion(uuid: uuid, identifier: beaconID)
// let beaconRegion = CLBeaconRegion(uuid: UUID(), identifier: beaconID)
self.locationManager.startMonitoring(for: beaconRegion)
}
//. CLLocationManagerDelegate Methods
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
var newStatus = ""
switch status {
case .notDetermined:
newStatus = "Not determined"
case .restricted:
newStatus = "Restricted"
case .denied:
newStatus = "Denied"
case .authorizedAlways:
newStatus = "Authorised Always"
case .authorized:
newStatus = "Authorised"
default:
newStatus = "What?"
}
print("LM Auth Status is now: \(newStatus)")
// Check for iBeacon monitoring status
let regionMonitoringIsAvailable = CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self)
let not = regionMonitoringIsAvailable ? "" : "not "
print("Beacon monitoring is \(not)available")
}
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
if beacons.count > 0 {
// updateDistance(beacons[0].proximity)
print("\(beacons.count) beacons found.")
print("\(beacons[0].proximity) dist.")
} else {
// updateDistance(.unknown)
}
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("region entered... \(region)")
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
print("region exited... \(region)")
}
func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
print("determined region state")
}
func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
print("region monitoring failed: \(error)")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("LM failed \(error)")
}
}
我不认为使用 CoreLocation 进行 Beacon 监控可以在任何 MacOS 版本上运行。
我查看了 class 文档以确认这一点,并且惊讶地发现 CLBeacon class 文档确实说它在 MacOS 10.15+ 上受支持。我不知道这是不是一个错误,或者只是表明适用于 MacOS 10.15+ 的 SDK 支持这些 classes,即使您不能真正使用它们。
我上一次在 MacOS 上使用这些 API 是在 2018 年,当时 MacOS 2.14 是新的。那时,我必须使用 CoreBluetooth 在 MacOS 上进行原始 BLE 广告扫描并自己解析信标。我怀疑从那以后有什么改变。我不记得听说过任何关于将信标支持添加到 MacOS 的消息,现在只是快速搜索,我找不到从 WWDC 2019 开始的任何讨论。我怀疑我错过了这样的变化,但我会很高兴得知我做到了!