Calculate the distance between two bluetooth devices using RSSI value, in swift?

I get the RSSI value from the searched bluetooth device in list.
But didn't know how to Calculate the distance.
We need txPower to calculate that but from where I get this txPower.

I'm using this code but don't know it's correct.
 func calculateNewDistance(txCalibratedPower: Int, rssi: Int) -> Double{
      if rssi == 0{ return -1 }

      let ratio = Double(exactly:rssi)!/Double(txCalibratedPower)
      if ratio < 1.0{
        return pow(10.0, ratio)
      }else{
        let accuracy = 0.89976 * pow(ratio, 7.7095) + 0.111
        return accuracy
      }
    }
There are many references on the web for this (seems you inspired from https://gist.github.com/eklimcz/446b56c0cb9cfe61d575)function calculateDistance(rssi) {

Code Block
var txPower = -59 //hard coded power value. Usually ranges between -59 to -65
if (rssi == 0) {
return -1.0;
}
var ratio = rssi*1.0/txPower;
if (ratio < 1.0) {
return Math.pow(ratio,10);
}
else {
var distance = (0.89976)*Math.pow(ratio,7.7095) + 0.111;
return distance;
}
}

As explained
Code Block
var txPower = -59 //hard coded power value. Usually ranges between -59 to -65

txPower (you called it txCalibratedPower) is the transmission power of the beacon or device. Its range is between -59 and -65. For the real value you need to get directly.
So, just do as done here, set at -59.
Then you can try to calibrate:
Modify in calculateNewDistance to loop through different values for txCalibratedPower, from - 56 to - 70
  • turn device BT on

  • install in free space from the measuring point

  • measure the distance and compare with the list of computed values

  • the closest to real distance will give you an estimate of txPower for THIS device

@Claude Thanks for your answer. I'm done with distance but now the distance which I get is not current.

For Android they use the Kalman filter to remove the noise and get accuracy so similar to that how can i use the Kalman filter in iOS (swift).
Thanksss
Calculate the distance between two bluetooth devices using RSSI value, in swift?
 
 
Q