无法在 macOS 上扫描 iBeacons

Posted

技术标签:

【中文标题】无法在 macOS 上扫描 iBeacons【英文标题】:Unable to scan for iBeacons on macOS 【发布时间】:2021-07-15 15:54:38 【问题描述】:

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

不过,我的系统上似乎从来没有可用的区域扫描功能。我做错了吗?

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

目标在“应用沙盒”功能部分启用了“蓝牙”和“应用位置”,并在“强化运行时”下启用了“位置”。

下面的代码总是给出以下内容:

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)")
    


【问题讨论】:

【参考方案1】:

我认为使用 CoreLocation 进行 Beacon 监控无法在任何 MacOS 版本上运行。

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

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

【讨论】:

非常感谢您的澄清。当文档暗示它可以在 macOS 上运行时,这令人沮丧,但事实并非如此。

以上是关于无法在 macOS 上扫描 iBeacons的主要内容,如果未能解决你的问题,请参考以下文章