1

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]
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Guan Nan Yao
  • 151
  • 1
  • 1
  • 9

1 Answers1

2

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
  }
}
davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • Thanks, that's very helpful – Guan Nan Yao Aug 09 '15 at 14:18
  • May i ask if i want have an array that contain all the beacons in ascending order by the accuracy, do I have to write a sort by myself in Swift1.2? I know in Swift2.0 has Array.sort, but this seem does not woking in IOS 8. – Guan Nan Yao Aug 10 '15 at 18:20
  • Actually, you don't even need to do a sort, because iOS automatically returns beacons in the ranging callback in ascending order of accuracy. – davidgyoung Aug 10 '15 at 19:34
  • i have an array now, so i want use the beacon.minor to pair the array and display what in array on the tableview, but it does no work. Xcode said Could not find an overload for 'subscript' that accepts the supplied arguments. cell.textLabel?.text = self.Array[self.beaconArray[indexPath.row].minor] – Guan Nan Yao Aug 12 '15 at 04:29
  • Since this is a different problem, please open a new question rather than asking in comments. You can link to the new question from comments . – davidgyoung Aug 12 '15 at 11:50