ios - 如何检测新的iBeacon?

标签 ios swift ibeacon

我对信标进行测距并将其显示在我的 TableView 中。我需要检测我的应用程序何时检测到新的信标。我尝试这样做,但出了问题

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
}

最佳答案

迭代两个数组并不容易,因为如果您同时看到两个信标,您会错误地认为它们是“新的”,因为其中一个与另一个不同。

我通常使用 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)
    }
  }
}

关于ios - 如何检测新的iBeacon?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43789902/

相关文章:

ios - 如何让我的应用程序图标进入 iOS 8 锁屏的左下角?

ios - 多个 UIActivityViewController 占位符项目?

ios - 在代码中使用静态单元格重新排序 UITableview

ios - AVAudioEngine.start() 中的崩溃,即使它被包裹在 do/catch 中

javascript - Node.js 卡尔曼滤波器一维

android - Android 上如何覆盖 Minor/Major/txPower 等 iBeacon 属性?

multithreading - 从 UIView 制作 UIImage 但不在主线程中

ios - ARC : returning allocated object from a method called by performSelector, 可以吗?

ios - UITextfield 中的位置 "Clear Button"

Swift Hashing 算法使用位移来避免冲突