如何在didRangeBeaconsInRegion中使用范围

时间:2015-04-10 12:49:22

标签: ibeacon ibeacon-android altbeacon

-------编辑2 --------

仍在使用Davidgyoung的帖子和这些评论,现在我有一个FatalException:

  

E / AndroidRuntime:致命异常:   IntentService [BeaconIntentProcessor]       过程:databerries.beaconapp,PID:19180       显示java.lang.NullPointerException               at databerries.beaconapp.MyApplicationName.didEnterRegion(MyApplicationName.java:76)               at org.altbeacon.beacon.BeaconIntentProcessor.onHandleIntent(BeaconIntentProcessor.java:83)               在android.app.IntentService $ ServiceHandler.handleMessage(IntentService.java:65)               在android.os.Handler.dispatchMessage(Handler.java:102)               在android.os.Looper.loop(Looper.java:136)               在android.os.HandlerThread.run(HandlerThread.java:61)

此错误是由于调用了setRangeNotifier()?

------- -------- EDIT

在Davidgyoung的帖子和这些评论之后,我尝试了这种方法,但仍然无法正常工作:

public class MyApplicationName extends Application implements BootstrapNotifier {
private static final String TAG = ".MyApplicationName";
private RegionBootstrap regionBootstrap;
private BeaconManager beaconManager;

List region_list = new ArrayList();

@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "App started up");
    // wake up the app when any beacon is seen (you can specify specific id filers in the parameters below)
    List region_list = myRegionList();
    regionBootstrap = new RegionBootstrap(this, region_list);

    BeaconManager 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"));
    beaconManager.setBackgroundScanPeriod(3000l);
    beaconManager.setBackgroundBetweenScanPeriod(5000l);
}

@Override
public void didDetermineStateForRegion(int arg0, Region arg1) {
    // Don't care
}

@Override
public void didEnterRegion(Region region) {
    Log.d(TAG, "Got a didEnterRegion call");
    // This call to disable will make it so the activity below only gets launched the first time a beacon is seen (until the next time the app is launched)
    // if you want the Activity to launch every single time beacons come into view, remove this call.
    regionBootstrap.disable();
    Intent intent = new Intent(this, MyActivity.class);
    // IMPORTANT: in the AndroidManifest.xml definition of this activity, you must set android:launchMode="singleInstance" or you will get two instances
    // created when a user launches the activity manually and it gets launched from here.
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    this.startActivity(intent);

    String zone = region.toString();
    Log.d(TAG, "Enter in region");
    String text = "Enter in " + zone;
    Log.d(TAG, text);
    String uuid = "UUID : " + region.getId1();
    Log.d(TAG, uuid);
    //This part is not working
    beaconManager.setRangeNotifier(this);
    beaconManager.startRangingBeaconsInRegion(region);
}

@Override
public void didExitRegion(Region arg0) {
    // Don't care
}

错误与setRangeNotifier中的输入和startRangingBeaconsInRegion

的例外有关

这不是我的主要课程,我的主要课程:

public class MyActivity extends Activity{
public final static String EXTRA_MESSAGE = "com.example.myapp.MESSAGE";
/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.d("myActivity","onCreate");

}

}

我想要区域内信标的所有ID。如果我明白这个方法,应用程序在她检测到一个区域时会在后台唤醒,通常是" startRangingBeaconsInRegion"可以给我一个信标列表,我可以拿这个信号。

------- --------原始

我想知道我周围的所有灯塔。我知道这个信标的UUID,我可以使用' region.toString();'。但是,我需要信标的其他ID。并且,我没有" Beacon"在didRangeBeaconsInRegion。

如何了解该地区的信标?

最后一个问题,可以在后台制作吗?

由于

1 个答案:

答案 0 :(得分:1)

您可以在"测距示例代码"中找到信标测距的示例。部分:http://altbeacon.github.io/android-beacon-library/samples.html

这将允许您通过查看回调中Beacon中返回的每个Collection<Beacon> beacons对象来读取所有标识符。像这样:

        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            for (Beacon beacon: beacons) {
                Log.i(TAG, "This beacon has identifiers:"+beacon.getId1()+", "+beacon.getId2()+", "+beacon.getId3());        
            }
        }

一旦你开始测距,它将在后台继续这样做,只要你不退出开始测距的活动。在库的某些用途下,范围在后台减慢,但只有在使用BackgroundPowerSaver类时才会发生这种情况。如果您不想在后台放慢速度,请不要在图书馆中启用后台省电功能。