ios - 如何监控多个 iBeacon 并根据每个信标更改 UILabel?

标签 ios swift ibeacon beacon

我在同时监控多个信标时遇到问题。我的代码只用一个就可以正常工作,但我似乎无法弄清楚如何监视多个信标并更新 UILabel。

当我打开信标时,它们都在被监控和识别,但我的手机无法在 View 中显示正确的标签。它不断地在 distanceReading.text 中显示“UNKOWN”,除非它是函数 (Estimote) 中的最后一个信标。

我还遇到了一些更新信标名称的其他问题。我不确定如何调用信标标识符,这将是理想的方式(类似于 beacon.identifier)。我已经尝试创建另一个变量并通过每次信标扫描更新名称,但它只扫描最后一个并且不会改变。我希望它会在检测到它时对其进行扫描,从而允许我在检测到新信标时更改变量。

我已经尝试将所有信标放在一个 startScanning() 函数中,为每个唯一的 UUID 分配变量,并为每个单独的信标使用 locationManager.startMonitoring() 和 locationManager.startRangingBeacon()。然后我尝试使用每个 UUID、主要、次要和标识符的参数创建一个 startScanning() 函数,然后为每个信标调用该函数。

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet var distanceReading: UILabel!
    @IBOutlet var nameLabel: UILabel!
    var locationManager: CLLocationManager?
    var beaconDict: [String: String]?
    var labelName: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager = CLLocationManager()
        locationManager?.delegate = self
        locationManager?.requestAlwaysAuthorization()

        alertShown = false

        view.backgroundColor = .gray  // default is in "unknown mode"

    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedAlways {
            // Can we monitor beacons or not?
            if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self) {
                //  Can we detect the distance of a beacon?
                if CLLocationManager.isRangingAvailable() {
                    startScanning(uuid: UUID(uuidString: "5A4BCFCE-174E-4BAC-A814-092E77F6B7E5")!, major: 123, minor: 456, identifier: "Apple Beacon", name: "Apple")
                    startScanning(uuid: UUID(uuidString: "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6")!, major: 123, minor: 456, identifier: "Radius Beacon", name: "Radius")
                    startScanning(uuid: UUID(uuidString: "5AFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF")!, major: 123, minor: 456, identifier: "Red Bear Beacon", name: "Red Bear")
                    startScanning(uuid: UUID(uuidString: "B9407F30-F5F8-466E-AFF9-25556B57FE6D")!, major: 123, minor: 456, identifier: "Estimote", name: "Estimote")
                }
            }
        }
    }

    func startScanning(uuid: UUID, major: UInt16, minor: UInt16, identifier: String, name: String) {
        let uuidApple = uuid
        let beaconRegion1 = CLBeaconRegion(proximityUUID: uuidApple, major: major, minor: minor, identifier: identifier)
        locationManager?.startMonitoring(for: beaconRegion1)
        locationManager?.startRangingBeacons(in: beaconRegion1)
        labelName = name
    }

    func update(distance: CLProximity) {
        UIView.animate(withDuration: 1) {
            switch distance {
            case .far:
                self.view.backgroundColor = .blue
                self.distanceReading.text = "FAR"
            case .near:
                self.view.backgroundColor = .orange
                self.distanceReading.text = "NEAR"
            case .immediate:
                self.view.backgroundColor = .red
                self.distanceReading.text = "RIGHT HERE"
            default:
                self.view.backgroundColor = .gray
                self.distanceReading.text = "UNKNOWN"
            }
        }
    }

    func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
        if let beacon = beacons.first {
            nameLabel.text = labelName
            update(distance: beacon.proximity)
        } else {
            update(distance: .unknown)
        }
    }

我希望每次检测到新信标时,我的标签都会相应更改。这适用于名为 LAST 的信标,但不适用于前三个。颜色改变了,但标签拒绝改变。我还想找出一种方法来调用我在 CLBeaconRegion 中设置的信标标识符。

最佳答案

尝试像 for beacons in beacons 中那样遍历所有远程信标,而不是像 beacons.first 中那样只处理数组中的第一个信标。

关于ios - 如何监控多个 iBeacon 并根据每个信标更改 UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56976585/

相关文章:

ios swift 确实选择了行来检查字符串是否相等

swift - 在 Swift 中从不同的类调用相同的属性?

ios - 在 iOS 7.1 中强制退出后,核心 BLE 是否在后台运行?或者只是 iBeacon?

ios - 信标区域监控应用程序的 CLLocationManager 的生命周期

iphone - 在 iOS 应用程序中显示服务器端内容 - 需要的方法

ios - 子类化 UITableViewCell 不允许多个单元格标签

ios - 无法在自定义单元格中使用 UISwitch 以移动到新 Controller

ios - 谷歌登录后使用电子邮件登录Firebase出现错误: The password is invalid or the user does not have a password

ios - 使用 iOS 中的照片框架将视频保存到自定义相册

ios - 是否真的需要locationManager.startUpdatingLocation()来监视或范围信标?