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