3

I am using Moko H2 beacon and using this library to get beacon list I am getting beacon list successfully

I am getting this type of response after scan

[
  {
    "mac": "F2:EE:97:63:1B:B6",
    "validDataHashMap": {
      "20": {
        "data": "20000bc117c0006673d201552aa1",
        "type": 2
      }
    },
    "rssi": -53
  },
  {
    "name": "BeaconX",
    "mac": "C4:7D:65:69:95:B7",
    "validDataHashMap": {
      "0afcc47d656995b7220164426561636f6e58": {
        "data": "0afcc47d656995b7220164426561636f6e58",
        "type": 4
      }
    },
    "rssi": -58
  },
  {
    "name": "BeaconX",
    "mac": "D0:24:EA:4E:1F:B3",
    "validDataHashMap": {
      "0afcd024ea4e1fb3220164426561636f6e58": {
        "data": "0afcd024ea4e1fb3220164426561636f6e58",
        "type": 4
      },
      "20": {
        "data": "20000bd91980005b4af60130014a",
        "type": 2
      }
    },
    "rssi": -59
  },
  {
    "mac": "D3:B9:AF:E1:D7:D3",
    "validDataHashMap": {
      "1000016d6f6b6f736d61727400": {
        "data": "1000016d6f6b6f736d61727400",
        "type": 1
      }
    },
    "rssi": -61
  },
  {
    "name": "BeaconX",
    "mac": "CB:4C:90:6C:E7:43",
    "validDataHashMap": {
      "1000016d6f6b6f736d61727400": {
        "data": "1000016d6f6b6f736d61727400",
        "type": 1
      },
      "0afccb4c906ce743220163426561636f6e58": {
        "data": "0afccb4c906ce743220163426561636f6e58",
        "type": 4
      }
    },
    "rssi": -61
  },
  {
    "mac": "CF:EB:82:02:6A:78",
    "validDataHashMap": {
      "20": {
        "data": "20000ba51b40006675dd0155316e",
        "type": 2
      }
    },
    "rssi": -61
  }
]

my requirement is how can I get distance from a distance

I have a rssi value and txPower calculation int txPower = Integer.parseInt(data.substring(2, 4), 16);

so how can I calculate distance using these 2 value rssi and txPower

I tried this but getting the wrong result

Adapter

holder.itemView.tvDistance.text = "${model.rssi.toInt().getDistance(model.measure_power.toInt()).convertMeterToFt().toRoundString()} ft" 

Extension function

fun Int.getDistance(txPower: Int): Double {
    return Math.pow(10.0, ((Math.abs(this) - Math.abs(txPower)) / (10 * 2)).toDouble())
}

fun Double.convertMeterToFt(): Double {
    return (3.281 * this) // to ft
}

fun Double.toRoundString(): String {
    return DecimalFormat("#0.##").format(this)
}

rssi@1m is -59 rssi@1m

calculate distance

fun calcDistbyRSSI(rssi: Int, measurePower: Int = -59): String? {
        val iRssi = Math.abs(rssi)
        val iMeasurePower = Math.abs(measurePower)
        val power:Double = (iRssi - iMeasurePower)/(10*2.0)
        // ft = m * 3.2808

        if (Math.pow(10.0,power) * 3.2808 < 1.0){
            return String.format("%.2f ft(Immediate)", Math.pow(10.0,power) * 3.2808)
        }else if (Math.pow(10.0,power) * 3.2808 > 1.0 && Math.pow(10.0,power) * 3.2808 < 10.0){
            return String.format("%.2f ft(Near)", Math.pow(10.0,power) * 3.2808)
        }else{
            return String.format("%.2f ft(Far)", Math.pow(10.0,power) * 3.2808)
        }
    }

Please help me Any Help Would be Highly Appreciated.

Chirag Patel
  • 437
  • 2
  • 7
  • 22

1 Answers1

7

You don't need to use txPower if you have RSSI value of beacons. You can calculate the distance information from RSSI value with this formula:

Distance = 10^((Measured Power - Instant RSSI)/(10*N)).

N is the constant for the environmental factor. It takes a value between 2-4. The measured power is the RSSI value at one meter.

So when you get a new beacon info. You can calculate the current distance. Let's assume your RSSI information is -61 and your N factor is 2.4. You measured RSSI value -44 at one meter.

When you get -61 RSSI. It corresponds to 10^((-44-(-61))/(10*2.4)) = 5.10 meter distance between rssi sender and receiver.

Zane Claes
  • 14,732
  • 15
  • 74
  • 131
bsgal
  • 313
  • 2
  • 13
  • 1
    i don't understand how to calculate Measured Power and N please put equation – Chirag Patel Dec 03 '20 at 11:42
  • 1
    You won't calculate measured power. You should observe it manually. Keep your beacon 1 meter away from receiver and observe the beacons rssi values. Also you don't need to calculate N factor. It's a factor that attenuation of signal. It's changing in different environments you can try different values until you find the value that gives the most accurate distance. Because this is about the air in the environment. You can read **Equation 3 explanations** from [here](https://www.mdpi.com/2076-3417/10/6/2003/pdf). – bsgal Dec 03 '20 at 12:03
  • so the measured power is rssi from 1 meter ridht? – Chirag Patel Dec 03 '20 at 12:44
  • i am geeting rssi like -65,-66,-70,-72,-73,-74 in 1 meter these are multiple beacons – Chirag Patel Dec 03 '20 at 12:53
  • Yes, right. If you read the article which I referenced you can see "**where the distance d0 is a unit distance (i.e., 1 m) and A represents the RSSI of the unit distance.**" line. And ^ is exponential number symbol. For 10^x, you can use Math.pow(10, x). And finally, you can use arithmetic mean of different RSSIs for 1 meter RSSI. If my answer is helpful you can upvoting so others can read it. – bsgal Dec 03 '20 at 12:58
  • Math.pow(10.0, ((-44 - model.rssi.toInt()) / 10 * 2.4)) like this? this is right? – Chirag Patel Dec 03 '20 at 13:16
  • Yes but I gave -44 as example number. You should use average rssi value of different beacons rssi at 1 meter. – bsgal Dec 03 '20 at 13:19
  • so every time Measured Power is different? – Chirag Patel Dec 03 '20 at 13:27
  • this is my final equation Math.pow(10.0, ((Math.abs(-65) - Math.abs(model.rssi.toInt())) / 10 * 2.0)) this is right? – Chirag Patel Dec 03 '20 at 13:28
  • It's so normal cause it's depended to environmental factors. Therefore, your measured power shouldn't be static. You can use the measured power value for hours. Because air behavior and conditions not changes very quickly. But when you observed your results getting bad you can check instant "measured power". If the difference between previous measured power and your instant measured power is very big, you should update your measured power. – bsgal Dec 03 '20 at 13:37
  • so how can i stable measured power or make dynamic – Chirag Patel Dec 03 '20 at 13:39
  • i get this result on 1 meter -65,-66,-70,-72,-73,-74 so i use 65 in measured power – Chirag Patel Dec 03 '20 at 13:40
  • still not getting accurate distance – Chirag Patel Dec 03 '20 at 13:54
  • can you help me remotely? – Chirag Patel Dec 03 '20 at 13:55
  • 1
    If the distance is not true you should check your parameters. Also note that: You can't get the exact distance with RSSI. Generally, distance values has error tolerance. RSSI values can be affect from anything easily. So distance info can be misleading. I'm suggesting you that should search similar works. – bsgal Dec 03 '20 at 14:18
  • hello what happens if Measured Power is lower then rssi? because in equation it shows masured power - rssi – Chirag Patel Jan 25 '21 at 09:13
  • let's remember that measured power is rssi at 1 meter. If your measured power is lower than instant rssi it means the distance between your beacon and signal receiver is less than 1 meter. The equation is "measured power - rssi" means that: let's assume that your measured power is -44 and your rssi is -30 in this case the distance would be: 10^((-44+30)/10*2.4) = 0.26 m is less than 1 meter. In a nutshell: if measured power is lower then rssi, the calculated distance will be less than 1 meter. – bsgal Jan 29 '21 at 11:39
  • i am doing like this is it right? https://github.com/MokoBeaconX/MokoBeaconX_Android/issues/13 – Chirag Patel Jan 29 '21 at 14:04
  • please check my updated question i added a function for getting distance and rssi@1m is -59 – Chirag Patel Jan 29 '21 at 14:08
  • @bsgal the equation, if copied directly into a compiler, did not produce the correct results. I've edited the response, Chirag; pseudo-code would be `pow(10, (measuredRssi - instantRssi) / (10.0 * N)`... plugging in -44, -61, and 2.4 should give the expected `5.1` meter result cited in the original answer. The equation is also expressed this way in this article, for reference: https://medium.com/beingcoders/convert-rssi-value-of-the-ble-bluetooth-low-energy-beacons-to-meters-63259f307283 – Zane Claes Jun 28 '23 at 22:23