Android:查找用户当前位置和信标之间的距离

时间:2017-05-09 08:53:37

标签: android beacon eddystone

我正在尝试找到Eddy Stone Beacon和用户在Android设备中的当前距离。 我到目前为止所做的是,我已经编写了代码来检测最近的灯塔并且它有效。为了进一步,我需要一些建议,我怎么能实现这一点。 使用一些公式?要使用地图?请提供我能做些什么..

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.Region;
import com.estimote.sdk.SystemRequirementsChecker;

import java.util.List;

public class MainActivity extends AppCompatActivity {
private final String TAG = MainActivity.class.getSimpleName();

private BeaconManager mBeaconManager;
private Region mRegion;


@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mBeaconManager = new BeaconManager(this);

    mBeaconManager.setRangingListener(new BeaconManager.RangingListener() {
        @Override
        public void onBeaconsDiscovered(Region region, List<Beacon> list) {

            if (!list.isEmpty()) {


                Log.d(TAG, "onBeaconsDiscovered: " + list.size());
                Beacon nearestBeacon = list.get(0);
                Log.d(TAG, "UUID: " + nearestBeacon.getProximityUUID());

                Log.d(TAG, "Strength: " + nearestBeacon.getRssi());
            }
        }
    });
    mRegion = new Region("Ranged Region", null, null, null);
}

@Override
protected void onResume() {
    super.onResume();
    SystemRequirementsChecker.checkWithDefaultDialogs(this);
    mBeaconManager.connect(new BeaconManager.ServiceReadyCallback() {
        @Override
        public void onServiceReady() {
            mBeaconManager.startRanging(mRegion);
        }
    });
}


@Override
protected void onPause() {
    mBeaconManager.stopRanging(mRegion);
    super.onPause();

}

}

编辑MainActivity

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mBeaconManager = new BeaconManager(this);

    mBeaconManager.setRangingListener(new BeaconManager.RangingListener() {
        @Override
        public void onBeaconsDiscovered(Region region, List<Beacon> list) {

            if (!list.isEmpty()) {


                Log.d(TAG, "onBeaconsDiscovered: " + list.size());
                Beacon nearestBeacon = list.get(0);

                UUID beaconId = nearestBeacon.getProximityUUID();
                int strength = nearestBeacon.getRssi();
                int minNumber = nearestBeacon.getMinor();
                int majorNumber = nearestBeacon.getMajor();
                int txPower = nearestBeacon.getMeasuredPower();

                Log.d(TAG, "UUID: " + nearestBeacon.getProximityUUID());
                Log.d(TAG, "Strength: " + nearestBeacon.getRssi());

                Log.d(TAG, "MinNum " + nearestBeacon.getMinor());
                Log.d(TAG, "MaxNum: " + nearestBeacon.getMajor());
                Log.d(TAG, "Tx: " + nearestBeacon.getMeasuredPower());

                double distance;

                if (strength == 0) {
                    distance = -1.0; 
                    Log.d(TAG, "dis1: " + distance);
                }

                double ratio = strength * 1.0 / txPower;
                if (ratio < 1.0) {
                    distance = Math.pow(ratio, 10);
                    Log.d(TAG, "dis2: " + distance);

                } else {
                    double accuracy = (0.89976) * Math.pow(ratio, 7.7095) + 
                     0.111;
                    distance = accuracy;
                    Log.d(TAG, "dis3: " + distance);

                }


            }
        }
    });

    mRegion = new Region("Ranged Region", null, null, null);
}

@Override
protected void onResume() {
    super.onResume();
    SystemRequirementsChecker.checkWithDefaultDialogs(this);
    mBeaconManager.connect(new BeaconManager.ServiceReadyCallback() {
        @Override
        public void onServiceReady() {
            mBeaconManager.startRanging(mRegion);
        }
    });
}


@Override
protected void onPause() {
    mBeaconManager.stopRanging(mRegion);
    super.onPause();
}

}

1 个答案:

答案 0 :(得分:0)

Estimote API提供Utils.computeAccuracy方法来计算到信标的距离。在API的1.0.0版本中,此工具移至RegionUtils.computeAccuracy

mBeaconManager.setRangingListener(new BeaconManager.RangingListener() {
    @Override
    public void onBeaconsDiscovered(Region region, List<Beacon> list) {
        if (!list.isEmpty()) {
            Beacon nearestBeacon = list.get(0);
            double distance = com.estimote.sdk.Utils.computeAccuracy(nearestBeacon);
        }
    }
});

我相信这是基于信号理论通过比较数据包中传输的接收信号强度和预期信号强度来计算的。信号越低,信号越远。正如@davidgyoung所提到的,这只是粗略的距离估计。不同的设备(电话和信标)模型将具有很少不同的无线电并因此具有不同的测量。电话和信标之间的环境也会产生差异。我建议您理解这篇文章:https://altbeacon.github.io/android-beacon-library/distance-calculations.html