我在Altbeacon中使用此代码进行测距和监控
class Appclass extends Application implements BootstrapNotifier {
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
private boolean haveDetectedBeaconsSinceBoot = false;
public static Region region1;
private static boolean activityVisible;
public static Appclass instances;
public static BeaconManager beaconManager;
public void onCreate() {
super.onCreate();
instances = this;beaconManager=org.altbeacon.beacon.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.setForegroundScanPeriod(1100l);
beaconManager.setForegroundBetweenScanPeriod(0l);
beaconManager.setAndroidLScanningDisabled(true);
beaconManager.setBackgroundBetweenScanPeriod(01);
beaconManager.setBackgroundScanPeriod(1100l);
try {
beaconManager.updateScanPeriods();
} catch (Exception e) {
}
Log.d("", "setting up background monitoring for beacons and power saving");
// wake up the app when a beacon is seen
region1 = new Region("backgroundRegion",
null, null, null);
regionBootstrap = new RegionBootstrap(this, region1);
backgroundPowerSaver = new BackgroundPowerSaver(this);
}
@Override
public void didEnterRegion(Region region) {
Log.d("ABC", "Enter");
Log.d("ABC value", region.getId2() + " " + region.getId3());
try { Intent i = new Intent(getApplicationContext(), AbcService.class);
startService(i);
} catch (Exception e) {
}
}
@Override
public void didExitRegion(Region region) {
Log.d("ABC", "exit");
}
@Override
public void didDetermineStateForRegion(int i, Region region) {
}
然后在我的AbcService类代码中:
class AbcService extends Service implements BeaconConsumer, MonitorNotifier, RangeNotifier {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Appclass.beaconManager.bind(this);
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
} catch (Exception e) {
}
return START_STICKY;
}
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {
Log.d("ABC", "range");
}
@Override
public void onBeaconServiceConnect() {
Appclass.beaconManager.setMonitorNotifier(this);
Appclass.beaconManager.setRangeNotifier(this);
}
@Override
public void didEnterRegion(Region region) {
try {
Log.d("ABC", "didEnterRegion");
Log.d("ABC", "" + region.getId2() + region.getId3());
Appclass.beaconManager.startRangingBeaconsInRegion(Appclass.region1);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void didExitRegion(Region region) {
try {
Log.d("ABC", "didExitRegion");
Appclass.beaconManager.stopRangingBeaconsInRegion(Appclass.region1);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void didDetermineStateForRegion(int i, Region region) {
}
}
现在的问题是每当我们输入信标范围时,每次调用Application类的didEnterRegion
方法,但AbcService类的didEnterRegion
方法没有调用一段时间而且也没有开始测距。我的代码中有什么问题?
答案 0 :(得分:0)
一些提示:
在开始监控的同时开始测距。退出该区域时,没有理由等待输入的区域回调或停止测距。
将记录添加到didDetermineStateFirRegion
。当应用程序启动后出现信标时,即使设备已经处于先前运行的区域,也会调用此方法。 didEnterRegion
只有在与该区域匹配的信标实际消失15秒以上然后重新出现时才会被调用。