我正在Android中编写一个简单的beacon应用程序
如果在某个地区之外开始工作(这是我的自定义日志):
onCreate - In
onCreate - Out
onBeaconServiceConnect - In
onBeaconServiceConnect - Out
didDetermineStateForRegion (INSIDE)
didEnterRegion - In
dER - startRangingBeaconsInRegion OK
didEnterRegion - Out
didRangeBeaconsInRegion - 1 beacons
...... omitted .....
didRangeBeaconsInRegion - 0 beacons
didDetermineStateForRegion (OUTSIDE)
didExitRegion - In
dXR - stopRangingBeaconsInRegion OK
didExitRegion - Out
didRangeBeaconsInRegion - 0 beacons
如果在区域内启动,则不会调用bootstrapNotifier
(即monitorNotifier
)方法
谁能看到我在哪里出错?
package it.test.test01;
import android.app.Application;
import android.os.RemoteException;
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.MonitorNotifier;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.powersave.BackgroundPowerSaver;
import org.altbeacon.beacon.startup.RegionBootstrap;
import org.altbeacon.beacon.startup.BootstrapNotifier;
import java.util.Collection;
public class testApplication extends Application
implements BootstrapNotifier, BeaconConsumer, RangeNotifier {
private final String TAG = "Application ";
protected static final Region beaconRegion = new Region("test01", null, null, null);
protected BeaconManager beaconManager = null;
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
protected static String sLog = "";
@Override
public void onCreate() {
super.onCreate();
logIt(TAG, "onCreate - In");
beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().clear();
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"));//iBeacon (tutti)
//--- wake up the app when a beacon is seen
regionBootstrap = new RegionBootstrap(this, beaconRegion);
//--- activate power saver
backgroundPowerSaver = new BackgroundPowerSaver(this);
beaconManager.bind(this);
logIt(TAG, "onCreate - Out");
}
private void logIt (String TAG, String msg) {
sLog += TAG + msg + "\n";
Log.w(TAG, msg);
}
//-------------------------//
//--- BootstrapNotifier ---//
//-------------------------//
@Override
public void didDetermineStateForRegion(int state, Region region) {
String msg = "didDetermineStateForRegion ";
switch(state) {
case MonitorNotifier.INSIDE:
msg +="(INSIDE)";
break;
case MonitorNotifier.OUTSIDE:
msg +="(OUTSIDE)";
break;
default:
msg +="(state=" +state +")";
break;
}
logIt(TAG, msg);
}
@Override
public void didEnterRegion(Region arg0) {
logIt(TAG, "didEnterRegion - In");
try {
beaconManager.startRangingBeaconsInRegion(beaconRegion);
logIt(TAG,"dER - startRangingBeaconsInRegion OK");
} catch (RemoteException e) {
logIt(TAG, "dER - startRangingBeaconsInRegion Err " +e);
}
logIt(TAG, "didEnterRegion - Out");
}
@Override
public void didExitRegion(Region region) {
logIt(TAG, "didExitRegion - In");
try {
beaconManager.stopRangingBeaconsInRegion(beaconRegion);
logIt(TAG,"dXR - stopRangingBeaconsInRegion OK");
} catch (RemoteException e) {
logIt(TAG, "dXR - stopRangingBeaconsInRegion Err " +e);
}
logIt(TAG, "didExitRegion - Out");
}
//----------------------//
//--- BeaconConsumer ---//
//----------------------//
@Override
public void onBeaconServiceConnect() {
logIt(TAG, "onBeaconServiceConnect - In");
beaconManager.setRangeNotifier(this);
logIt(TAG, "onBeaconServiceConnect - Out");
}
//---------------------//
//--- RangeNotifier ---//
//---------------------//
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
logIt(TAG, "didRangeBeaconsInRegion - " +beacons.size() +" beacons");
}
}
答案 0 :(得分:0)
如果您在应用启动时已进入该区域,则无法获得进入事件。这是设计的。
编辑:图书馆2.9有两个修改,以便在启动时更容易确定区域状态:
一个应用程序开始监控,保证调用didDetermineStateForRegion
回调,告诉您该区域的状态。 (除非实际发生更改,否则didEnterRegion
和didExitRegion
回调可能无法触发。)
如果要禁用区域状态持久性,可以使用beaconManager.setRegionStatePeristenceEnabled(false);
这样做。这将保证在应用启动后第一次看到信标时回调到didEnterRegion
,即使在应用程序重启之前几秒钟就可以看到信标。
如果您使用的是2.9+,请不要按照以下说明操作,因为该解决方案已被弃用,将来可能无法使用。
如果您确实希望在每次应用启动时首次检测到额外的条目事件,则可以添加以下代码行: MonitoringStatus.getInstanceForApplication(本).stopStatusPreservationOnProcessDestruction();
我们正致力于在下一版本中更轻松地配置此行为。