0

我试图让引导程序从 iBeacons 收集 id2 和 id3 并与它们开始活动。问题是应用程序不会从意图开始,我一直看到 D/BeaconService:调用测距回调 D/Callback:尝试通过意图回调: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.
4

1 回答 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接口。

于 2014-11-26T10:49:57.953 回答