首页 文章

如何解析Altbeacon引导程序中的iBeacon标识符并使用它们启动应用程序?

提问于
浏览
0

我正在尝试从iBeacons收集id2和id3的引导程序,并用它们开始一个活动 . 问题是应用程序不会从intent开始,我一直看到D / BeaconService:调用范围回调D /回调:尝试通过intent回调:ComponentInfo {com.rp_ds.chequeplease / org.altbeacon.beacon.BeaconIntentProcessor}

以下是我的代码:

@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "App started up");
    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.setDebug(true);
    // Add AltBeacons Parser for iBeacon
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
    // wake up the app when any beacon is seen (you can specify specific id filers in the parameters below)
    region = new Region("com.rp_ds.chequeplease.bootstrapRegion", Identifier.parse("F8EFB5C2-9FFF-47AE-8C8D-D23C417882D1"), null, null);
    regionBootstrap = new RegionBootstrap(this, region);
    backgroundPowerSaver = new BackgroundPowerSaver(this);
    _appPrefs = new AppPreferences(this);
}

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



@Override
public void didEnterRegion(Region arg0) {
    Log.d(TAG, "Got a didEnterRegion call");
    try {
        beaconManager.startRangingBeaconsInRegion(arg0);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
    // 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.
}

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

@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
    Log.d(TAG, "Got a didRangeBeaconsInRegion call");
    for(Beacon beacon:beacons) {
            if(null!=beacon.getId2()&&null!=beacon.getId3()) {
                Intent intent = new Intent(this, MainActivity.class);
                _appPrefs.setRestaurantID(beacon.getId2().toInt());
                _appPrefs.setTableNumber(beacon.getId3().toInt());
                // 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);
                Log.i(TAG,"Application started");
                try {
                    beaconManager.stopRangingBeaconsInRegion(region);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
    }
}

}

我确实得到了一个didEnterRegion调用,但没有一个didRangeBeaconsInRegion调用 . 信标也被认可 .

D/BeaconParser﹕ This is a recognized beacon advertisement -- 0215 seen
D/BeaconIntentProcessor﹕ got an intent to process
D/RangingData﹕ parsing RangingData
D/RangingData﹕ parsing RangingData
D/BeaconIntentProcessor﹕ got ranging data
D/BeaconIntentProcessor﹕ but ranging notifier is null, so we're dropping it.

1 回答

  • 0

    范围通知程序需要像这样设置:

    @Override
    public void didEnterRegion(Region arg0) {
        Log.d(TAG, "Got a didEnterRegion call");
        try {
            beaconManager.startRangingBeaconsInRegion(arg0);
            beaconManager.setRangeNotifier(this);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    

    确保包含的类实现 RangeNotifier 接口 .

相关问题