在Android设备中获取设备到iBeacon LE的距离

时间:2015-12-30 22:28:26

标签: android ibeacon ibeacon-android altbeacon

我正试图从我的移动设备(s6-android 5.1)到硬件IBeacon的距离正确。

首先以这种形式开始:

  protected void onCreate(Bundle savedInstanceState) {
  ...
  beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager
            .getBeaconParsers()
            .add(new BeaconParser()
                    .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
  beaconManager.bind(this);
  ...
 }

然后重写:

  @Override
public void onBeaconServiceConnect() {
    beaconManager.setRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {

            Log.i(TAG + "region", region.toString());
            Log.i(TAG + " Size ", String.valueOf(beacons.size()));

            for (Beacon beacon : beacons) {
                Log.i(TAG, " Distance :" + beacon.getId1() + ", " + beacon.getId2() + ", " + beacon.getId3());
            }

            if (beacons.size() > 0) {
                Beacon firstBeacon = beacons.iterator().next();
                logToDisplay("The first beacon " + firstBeacon.toString() + " is about " + firstBeacon.getDistance() + " meters away.");
                Log.i(TAG + " Distance ", beacons.iterator().next().getDistance() + "");
            }
        }
    });

    try {
        beaconManager.startRangingBeaconsInRegion(new Region("RadBeacon",
                Identifier.parse("2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6"),
                Identifier.parse("1"), Identifier.parse("1")));


    } catch (RemoteException e) {
        Log.i(TAG + "Error", e.toString());
    }
}

在top方法中,我可以记录 region正确返回:

  id1: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6 id2: 1 id3: 1

和Log中的其他信息:

   onScanResult() - ScanResult{mDevice=....., mScanRecord=ScanRecord [mAdvertiseFlags=6, mServiceUuids=null, mManufacturerSpecificData={76=[2, 21, -30, -59, 109, -75, -33, -5, 72, -46, -80, 96, -48, -11, -89, 16, -106, -32, 0, 2, 0, 6, -59]}, mServiceData={00005153-0000-1000-8000-00805f9b34fb=[67]}, mTxPowerLevel=0, mDeviceName=null], mRssi=-60, mTimestampNanos=431625881888515}

但我的问题在于Collection<Beacon> beacons,并且总是返回 0

我可以Monitor通过以下方式进入/退出区域:

  beaconManager.setMonitorNotifier(new MonitorNotifier() {...}

但是却无法获得移动和信标硬件之间的距离;

所以经过更多搜索后找到方法并尝试:

  protected static double calculateAccuracy(int txPower, double 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 {
        return (0.89976) * Math.pow(ratio, 7.7095) + 0.111;
    }
}

对于rssi,设置在Log和txPower中设置静态int的mRssi,但return value不正确。

现在我有两个问题:

  1. 为什么Collection<Beacon> beacons,始终返回0值?
  2. 如何在移动设备和Ibeacon硬件之间实现正确和正确的Distance
  3. 在我尝试这个库和资源之前:

    https://forums.estimote.com/t/get-distance-of-beacons/1986

    Measure distance to iBeacon from Android device

    https://github.com/AltBeacon/android-beacon-library/issues/43

    https://github.com/NickdeDycker/EstimoteIndoorAndroid

    http://altbeacon.github.io/android-beacon-library/samples.html

    http://developer.estimote.com/android/tutorial/part-3-ranging-beacons/

    还有很多其他链接!

    Thank you for your attention and help

    EDIT1

    显示在Logcat

       W/ModelSpecificDistanceCalculator﹕ Cannot find match for this device.  Using default
    

    @davidgyoung

1 个答案:

答案 0 :(得分:0)

一些提示:

  1. 确保信标使用现成的扫描应用程序Locate.传输您认为的标识符。如果标识符不匹配,您可以在Region定义。

  2. onBeaconServiceConnect的顶部添加调试行以确保调用它。

  3. 确认您大约每秒都在didRangeBeaconsInRegion中始终看到调试行。