我使用範圍和Altbeacon問題和測距的Android
監測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) {
}
}
現在問題是我們每次進入beacon範圍時都會調用Application類的didEnterRegion
方法而AbcService類的didEnterRegion
方法沒有調用一段時間,也沒有開始測距。我的代碼中有什麼問題?
在服務類中是否調用了'didDetermineStateForRegion'? 'onBeaconServiceConnect()'是否被調用? – davidgyoung
它正在調用一段時間...所以我不復制是什麼問題。 – aj0822ArpitJoshi
對不起,我可能需要更多信息纔能有所幫助。我建議你嘗試重複測試,也許10次,看看你的服務類中的哪些方法,onCreate,onBeaconServiceConnect和didDetermineState被每次測試運行調用。如果你做了這些測試,請注意你注意到哪些方法被調用的模式與被測試的條件。 – davidgyoung