1

我正在 MacOS 上尝试一个非常简单的 AppKit 应用程序,它显示附近的 iBeacons。

不过,我的系统上似乎从来没有可用的区域扫描。难道我做错了什么?

仅供参考:我的系统是运行 Catalina (10.15.7) 的 MacBook Pro(16 英寸,2019 年)

目标在“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)")
    }

}
4

1 回答 1

2

我不相信带有 CoreLocation 的 Beacon 监控适用于任何 MacOS 版本。

我查看了类文档以确认这一点,并惊讶地发现CLBeacon类文档确实说它在 MacOS 10.15+ 上受支持。我不知道这是否是一个错误,或者只是表明 MacOS 10.15+ 的 SDK 支持这些类,即使您不能真正使用它们。

我上一次在 MacOS 上使用这些 API 是在 2018 年,当时 MacOS 2.14 是新的。那时,我不得不使用 CoreBluetooth 在 MacOS 上进行原始 BLE 广告扫描并自己解析信标。我怀疑从那以后有什么改变。我不记得听说过有关向 MacOS 添加信标支持的任何消息,现在只是快速搜索一下,我找不到从 WWDC 2019 开始的关于此的讨论。我怀疑我错过了这样的变化,但我会很高兴得知这一点我做到了!

于 2021-07-15T16:50:49.807 回答