Skip to content

Instantly share code, notes, and snippets.

@JoostKiens
Last active October 26, 2022 17:40
Show Gist options
  • Save JoostKiens/d834d8acd3a6c78324c9 to your computer and use it in GitHub Desktop.
Save JoostKiens/d834d8acd3a6c78324c9 to your computer and use it in GitHub Desktop.
iBeacon calculate distance in meters
// Based on http://stackoverflow.com/a/20434019
function calculateAccuracy(txPower, rssi) {
if (rssi === 0) {
return -1; // if we cannot determine accuracy, return -1.
}
var ratio = rssi * 1 / txPower;
if (ratio < 1.0) {
return Math.pow(ratio, 10);
} else {
return (0.89976) * Math.pow(ratio, 7.7095) + 0.111;
}
}
@besi
Copy link

besi commented Jun 6, 2017

Could you maybe add an example of how to exactly use your function?

I have an iBeacon that is 1m away. I have a txPower of 4dbm and an RSSI of -55dbm. But when I put these values into your function I get these results:

calculateAccuracy(4,-55)
// 241561090582.98172

So instead of approximately 1m i get 0.02502742130697 mili-lightyears

@philipgiuliani
Copy link

philipgiuliani commented Jun 14, 2017

@besi The TX power is not what you think it is. Its the dbm value at a distance of 1m (for calibration). You can also read it from an iBeacon when its correctly calibrated.

So in your case you have to call calculateAccuracy(-55,-55) and you will get 1,01m
So in your case you have to call calculateAccuracy(-55,-80) and you will get 16,27m

@CaferCruz
Copy link

Why do you multiply for 1?

@navyseai
Copy link

navyseai commented May 7, 2018

@philipgiuliani how did you get to txPower of -55 ?

@lisuwang
Copy link

@hadpro24
Copy link

This formula for iOS devices ? How is for android ?

@JoostKiens
Copy link
Author

This formula for iOS devices ? How is for android ?

@hadpro24 sorry I have no idea, I have used this function once in 2015 and have forgotten the context 🤷‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment