Swift通过rssi找到最近的信标 [英] Swift find closest Beacon by rssi

查看:121
本文介绍了Swift通过rssi找到最近的信标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想成为iBeacon,我从Youtube中学到了一些东西.这是按距离找到最近的信标,但是它不是很准确.因此,我想通过rssi找到最接近的一个.我应该如何修改代码? rssi的值始终为负,这是否意味着rssi越大,就越接近?

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]
    }
}

推荐答案

RSSI的幅度越低,信号越强.换句话说,-49 dBm的RSSI表示比-59 dBm的信号更强的信号(通常是更近的信标).

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.

您当然可以使用RSSI而不是使用CLBeacon精度属性来计算最接近的信标,但是您将无法获得更一致的结果.这是因为在iOS上,仅在1秒钟内对RSSI进行平均(使用该时段内检测到的数据包的所有测量值),而CLBeacon精度属性则在20秒内进行平均.这意味着准确性属性(以米为单位衡量的估计距离)更加稳定.

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.

修复了两个版本中的错误

因此您可以尝试以下操作:

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
  }
}

这篇关于Swift通过rssi找到最近的信标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆