Swift find closest Beacon by rssi

梦想与她 提交于 2019-12-20 06:13:42

问题


I am trying to is iBeacon, I learned something from Youtube. This is find the closest Beacon by proximity, but it's not very accurate. So I want to find the closest one by rssi. How should I modify the code? The value of rssi always negative, does it mean the the greater the rssi is, the closest?

 func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
    //print(beacons)
    let knownBeacons = beacons.filter{ $0.proximity != CLProximity.Unknown }

    //print(knownBeacons)
    if(knownBeacons.count>0){
        let closestBeacon = knownBeacons[0] as CLBeacon
        self.showName.text = self.name[closestBeacon.minor.integerValue]
    }
}

回答1:


The lower the magnitude of the RSSI the stronger the signal. In other words, an RSSI of of -49 dBm represents a stronger signal (and usually a closer beacon) than one of -59 dBm.

You can certainly calculate the closest beacon using RSSI instead of using the CLBeacon accuracy property, but you won't get more consistent results. This is because on iOS the RSSI is averaged over only 1 second (using all measurements from packets detected over that period), whereas the CLBeacon accuracy property is averaged over 20 seconds. This means the accuracy property (which measures an estimated distance in meters) is much more stable.

EDIT: Fixed bug in both versions

So you can try this:

var closestBeacon: CLBeacon? = nil
for beacon in beacons {
  if closesBeacon ==nil || (beacon.rssi < 0 && beacon.rssi > closestBeacon!.rssi) {
    closestBeacon = beacon as? CLBeacon
  }
}

But you will get more stable results with this:

var closestBeacon: CLBeacon? = nil
for beacon in beacons {
  if closesBeacon == nil || (beacon.accuracy > 0 && beacon.accuracy < closestBeacon!.accuracy) {
    closestBeacon = beacon as? CLBeacon
  }
}


来源:https://stackoverflow.com/questions/31834641/swift-find-closest-beacon-by-rssi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!