如何使用android studio计算ibeacon的距离

时间:2017-08-05 17:12:14

标签: android distance ibeacon ibeacon-android android-ibeacon

我尝试使用android studio计算我的ibeacon距离,我按照http://www.software7.com/blog/creating-a-beacon-app-for-android-in-less-than-10-minutes-from-scratch/中的视频教程,我做了与他完全相同的事情,但他没有使用android studio而不是IntelliJ IDEA。结果应该出现在android monitor - logcat中,但是我没有

import android.os.RemoteException;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.Identifier;
import org.altbeacon.beacon.MonitorNotifier;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;
import java.util.Collection;

public class MainActivity extends AppCompatActivity implements 
BeaconConsumer {
public static final String TAG = "BeaconsEverywhere";
private BeaconManager beaconManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.getBeaconParsers().add(new BeaconParser()
            .setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-
    24,d:25-25"));
    beaconManager.bind(this);

}

@Override
public void onBeaconServiceConnect() {
    final Region region = new Region("myBeaons", Identifier.parse("2173E519-
    9155-4862-AB64-7953AB146156"),null,null);
    beaconManager.addMonitorNotifier(new MonitorNotifier() {
        @Override
        public void didEnterRegion(Region region) {
            try {
                Log.d(TAG,"didEnterRegion");
                beaconManager.startRangingBeaconsInRegion(region);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void didExitRegion(Region region) {
            try {
                Log.d(TAG,"didExitRegion");
                beaconManager.stopRangingBeaconsInRegion(region);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void didDetermineStateForRegion(int i, Region region) {

        }
    });
    beaconManager.addRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, 
        Region region) {
            for(Beacon oneBeacon : beacons){

                Log.d(TAG, "distance: " + oneBeacon.getDistance() + " id:" + 
                oneBeacon.getId1() + "/" + oneBeacon.getId2() + "/" + 
                oneBeacon.getId3());
            }
        }
    });
    try {
        beaconManager.startMonitoringBeaconsInRegion(region);
    }catch (RemoteException e){
        e.printStackTrace();
    }

}

@Override
protected void onDestroy() {
    super.onDestroy();
    beaconManager.unbind(this);
}
}

The first Image is my expectation , second is my result

你会帮我弄清楚问题是什么吗?

1 个答案:

答案 0 :(得分:0)

我会在每个方法中添加新的调试行,例如Log.d(TAG,"onCreate");Log.d(TAG,"onBeaconServiceConnect");,这样您就可以知道发生了什么,然后在运行应用时查找这些行。这将告诉您程序在此过程中的进度。

我还怀疑您需要使用与此处列出的不同的信标解析器表达式:

beaconManager.getBeaconParsers().add(new BeaconParser()
        .setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-
24,d:25-25"));

如果您使用的是现成的iBeacon,则需要将该字符串替换为iBeacon布局,如下所示:

https://beaconlayout.wordpress.com/

不要担心Android Studio与IntelliJ IDEA。显示的代码应该可以正常使用IDE。