use AltBeacon’s algorithm.
TX power ,用于确定你和beacon之间距离有多近。根据这个值不但可以获得粗略的信息(比如靠近/远离/不在范围内等),也可以获取精确到米的距离,TX power是距离设备1米测得的信号强度值
(1)计算1
1 public double calculateDistance(int txPower, double rssi) { 2 if (rssi == 0) { 3 return -1.0; // if we cannot determine accuracy, return -1. 4 } 5 double ratio = rssi*1.0/txPower; 6 if (ratio < 1.0) { 7 return Math.pow(ratio,10); 8 } 9 else { 10 double accuracy = (0.89976)*Math.pow(ratio,7.7095) + 0.111; 11 return accuracy; 12 } 13 }
(2)计算2
1 public double calculateAccuracy (int rssi) { 2 // d = 10^((abs(RSSI) - A) / (10 * n)) 3 4 /*** 5 * d - 计算所得距离 6 7 RSSI - 接收信号强度(负值) 8 9 A - 发射端和接收端相隔1米时的信号强度 10 11 n - 环境衰减因子 12 */ 13 14 int iRssi = Math.abs(rssi); 15 float power = (iRssi - mTxPower) / (10 * mEnvironmentFactor); 16 double dis = Math.pow(10, power); 17 18 return dis; 19 }
(3)定义区域
1 private String getDistance(accuracy) { 2 if (accuracy == -1.0) { 3 return "Unknown"; 4 } else if (accuracy < 1) { 5 return "Immediate"; 6 } else if (accuracy < 3) { 7 return "Near"; 8 } else { 9 return "Far"; 10 } 11 }
altbeacon内置设备模型以及用于beacon测距的算法中的系数
1 { 2 "models": 3 [ 4 { 5 "coefficient1": 0.42093, 6 "coefficient2": 6.9476, 7 "coefficient3": 0.54992, 8 "version":"4.4.2", 9 "build_number":"KOT49H", 10 "model":"Nexus 4", 11 "manufacturer":"LGE" 12 }, 13 { 14 "coefficient1": 0.42093, 15 "coefficient2": 6.9476, 16 "coefficient3": 0.54992, 17 "version":"4.4.2", 18 "build_number":"LPV79", 19 "model":"Nexus 5", 20 "manufacturer":"LGE", 21 "default": true 22 }, 23 { 24 "coefficient1": 0.9401940951, 25 "coefficient2": 6.170094565, 26 "coefficient3": 0.0, 27 "version":"5.0.2", 28 "build_number":"LXG22.67-7.1", 29 "model":"Moto X Pro", 30 "manufacturer":"XT1115", 31 "default": false 32 }, 33 { 34 "coefficient1": 0.1862616782, 35 "coefficient2": 8.235367435, 36 "coefficient3": -0.45324519, 37 "version":"6.0", 38 "build_number":"MPE24.49-18", 39 "model":"XT1092", 40 "manufacturer":"Motorola", 41 "default": false 42 } 43 ] 44 }
ALTBeacon中的测距方法
1 public double calculateDistance(int txPower, double rssi) { 2 if (rssi == 0) { 3 return -1.0; // if we cannot determine accuracy, return -1. 4 } 9 double ratio = rssi*1.0/txPower; 10 double distance; 11 if (ratio < 1.0) { 12 distance = Math.pow(ratio,10); 13 } 14 else { 15 distance = (mCoefficient1)*Math.pow(ratio,mCoefficient2) + mCoefficient3; 16 }18 return distance; 19 }
如果当前的设备没有在altbeacon内置设备模型列表中,beacon SDK 则选择Nexus 5系数进行距离计算,如下
1 public static double calculateAccuracy(int txPower, double rssi) { 2 if (rssi == 0) { 3 return -1.0; // if we cannot determine accuracy, return -1. 4 } 5 double ratio = rssi * 1.0 / txPower; 6 if (ratio < 1.0) { 7 return Math.pow(ratio, 10); 8 } else { 9 double accuracy = (0.42093) * Math.pow(ratio, 6.9476) + 0.54992; 10 return accuracy; 11 } 12 }