简体   繁体   English

如何检测新的iBeacon?

[英]How to detect a new iBeacon?

I range beacons and display them in my TableView. 我设置信标并在TableView中显示它们。 I need to detect when my app detects a new beacon. 我需要检测我的应用何时检测到新信标。 I try to do it in this way, but something goes wrong 我尝试以此方式进行操作,但是出了点问题

var oldBeacons: [CLBeacon] = []

func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
  for beacon in beacons {
    for oldBeacon in oldBeacons {
      if beacon.minor != oldBeacon.minor, beacon.major != oldBeacon.major {
        print("New Beacon")
      } else {
        print("Old Beacon")
      }
    }
  }
  oldBeacons = beacons
}

Iterating through two arrays won't easily work because if you ever see two beacons at the same time, you'll incorrectly think they are "new" because one is not the same as the other. 遍历两个数组将很难进行工作,因为如果同时看到两个信标,您会错误地认为它们是“新的”,因为一个与另一个不相同。

I typically use a Set to do this: 我通常使用Set来做到这一点:

var detectedBeacons: Set<String>

func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
  for beacon in beacons {
    let key = "\(beacon.proximityUUID) \(beacon.major) \(beacon.minor)"
    if detectedBeacons.contains(key) {
      print("Old Beacon")
    }
    else {
      print("New Beacon")
      detectedBeacons.insert(key)
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM