public class IBeaconLocation { //计算手机与蓝牙基站的距离 public static double calculateAccuracy(iBeaconClass.iBeacon beacon) { int txPower = beacon.txPower; double rssi = beacon.rssi; if (rssi == 0) { return -1.0; // if we cannot determine accuracy, return -1. } double ratio = rssi * 1.0 / txPower; if (ratio < 1.0) { return Math.pow(ratio, 10); } else { double accuracy = (0.89976) * Math.pow(ratio, 7.7095) + 0.111; return accuracy; } } //三边测量法 // 通过三点坐标和到三点的距离,返回第4点位置 public double[] calcPhonePosition(double x1, double y1, double d1, double x2, double y2, double d2, double x3, double y3, double d3) { double[] d = {0.0, 0.0}; double a11 = 2 * (x1 - x3); double a12 = 2 * (y1 - y3); double b1 = Math.pow(x1, 2) - Math.pow(x3, 2) + Math.pow(y1, 2) - Math.pow(y3, 2) + Math.pow(d3, 2) - Math.pow(d1, 2); double a21 = 2 * (x2 - x3); double a22 = 2 * (y2 - y3); double b2 = Math.pow(x2, 2) - Math.pow(x3, 2) + Math.pow(y2, 2) - Math.pow(y3, 2) + Math.pow(d3, 2) - Math.pow(d2, 2); d[0] = (b1 * a22 - a12 * b2) / (a11 * a22 - a12 * a21); d[1] = (a11 * b2 - b1 * a21) / (a11 * a22 - a12 * a21); return d; } //double 四舍五入 public int doubleRound(double num) { BigDecimal b = new BigDecimal(num); num = b.setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue(); return (int) num; } }