当我启动应用程序时,扫描开始并在2-3秒内触发onBeaconServiceConnect()函数。我需要增加它。我也使用背景信标扫描另一个类。我使用另一个beaconManager对象。虽然我使用setForegroundBetweenScanPeriod()函数来执行此操作,但它无法正常工作。 onBeaconServiceConnect()函数在2-3秒内被触发。我该如何解决?我的功能如下:
------------------------我的主要类检测前景上的信标--------------- -------
private void generateBeaconOptions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("This app needs location acces");
builder.setMessage("Please grat location acces so this app can detect beacons");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
}
});
}
}
beaconManager = 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"));
region = new Region("com.example.myapp.boostrapRegion", null, null, null);
beaconManager.setForegroundScanPeriod(1100l);
beaconManager.setForegroundBetweenScanPeriod(30000l);
beaconManager.bind(this);
}
@Override
public void onBeaconServiceConnect() {
if(!ConfigFile.isDemo()) {
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
UniPromClient.setIsInAnyBeaconRegion(true);
for (Beacon beacon : beacons) {
logToDisplay("Detected " + beacon.toString() + " with UUID " + beacon.getId1().toString() + " and Major ID " + beacon.getId2().toString() + " and Minor ID " + beacon.getId3().toString());
UniPromClient.setUniPromBeacon(new UniPromBeacon(beacon.getId1().toString(),
beacon.getId2().toString(),
beacon.getId3().toString()));
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
updateUserScoreAsUserStaying();
}
});
break;
}
} else {
UniPromClient.setIsInAnyBeaconRegion(false);
UniPromClient.setUniPromBeacon(null);
logToDisplay("No iBeacons detected");
}
if (UniPromClient.isPrevIsAnyBeaconInRegion() != UniPromClient.isInAnyBeaconRegion()) {
if (UniPromClient.isInAnyBeaconRegion()) {
showMainActivityAlert("Uni Promotion alanına girdiniz :)");
} else if (!UniPromClient.isInAnyBeaconRegion() && UniPromClient.isPrevIsAnyBeaconInRegion()) {
showMainActivityAlert("Uni Promotion alanından çıktınız :(");
}
UniPromClient.setPrevIsAnyBeaconInRegion(UniPromClient.isInAnyBeaconRegion());
generateDrawerList();
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
}
}
}
--------------------------我的Backgorund类检测背景上的信标------------ < / p>
@Override
public void onCreate() {
super.onCreate();
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
//BeaconManager.setsManifestCheckingDisabled(true);
//beaconManager = 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"));
beaconManager.setBackgroundScanPeriod(1000l);
beaconManager.setBackgroundBetweenScanPeriod(5000l);
Region region = new Region("com.example.myapp.boostrapRegion", null, null, null);
regionBootstrap = new RegionBootstrap(this, region);
//backgroundPowerSaver = new BackgroundPowerSaver(this);
}
答案 0 :(得分:1)
如果更改扫描周期时已开始扫描,则必须致电beaconManager.updateScanPeriods()
以使更改生效。
另外,请注意tgat会自动切换前景/后台扫描周期,您必须使用显示为已注释掉的BackgroundPowerSaver
。如果启用,它将监视暂停和恢复的活动,并在没有活动可见时将扫描速率设置为后台设置。
如果您不想使用BackgroundPowerSaver
,则必须致电beaconManager.setBackgroundMode(true);
以激活后台扫描率。