Я использую библиотеку altbeacon для сканирования маяков.Я использую Android 8.1 OnePlus 3T.Ранжирование и мониторинг прекращаются через 5 минут, хотя я использую сервис переднего плана.Я вижу активное уведомление, но обнаружение не работает.
Это мой код
Это основной класс приложения
@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
startBeacon();
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("Scanning for Beacons");
Intent intent = new Intent(this, MainApplication.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT
);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(NotificationChannelId, "Beacons Scanning",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setShowBadge(true);
channel.setSound(null, null);
channel.setLightColor(Color.BLUE);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
assert mNotificationManager != null;
mNotificationManager.createNotificationChannel(channel);
builder = new Notification.Builder(this,
NotificationChannelId);
} else {
builder = new Notification.Builder(this);
}
builder.setContentIntent(pendingIntent);
beaconManager.enableForegroundServiceScanning(builder.build(), 456);
beaconManager.setEnableScheduledScanJobs(false);
beaconManager.setBackgroundBetweenScanPeriod(0);
beaconManager.setBackgroundScanPeriod(1100);
Log.d(TAG, "setting up background monitoring for beacons and power saving");
// wake up the app when a beacon is seen
Region region = new Region("backgroundRegion",
null, null, null);
regionBootstrap = new RegionBootstrap(this, region);
backgroundPowerSaver = new BackgroundPowerSaver(this);
Intent MyIntent = new Intent(this, MainActivity.class);
this.startActivity(MyIntent);
}
private void startBeacon() {
beaconManager = BeaconManager.getInstanceForApplication(getApplicationContext());
//BeaconManager.setRegionExitPeriod(5000); //5 seconds
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(ALTBEACON));
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(EDDYSTONE_TLM));
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(EDDYSTONE_UID));
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(EDDYSTONE_URL));
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(IBEACON));
}
@Override
public void didEnterRegion(Region region) {
System.out.println("did enter region");
if (!beaconManager.isBound(this)) {
beaconManager.bind(this);
}
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) {
e.printStackTrace();
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
getNewandOldBeacons(MainApplication.ListofBeacons, "Entry");
}
}, 1000);
}
@Override
public void didExitRegion(Region region) {
System.out.println("did exit region");
try {
beaconManager.stopRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) {
e.printStackTrace();
}
getNewandOldBeacons(MainApplication.ListofBeacons, "Exit");
if(beaconManager.isBound(this)) {
beaconManager.unbind(this);
}
}
@Override
public void didDetermineStateForRegion(int i, Region region) {
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
MainApplication.ListofBeacons = beacons;
}
}
});
}
В MainActivity я просто запрашиваю разрешениядля ACCESS_COURSE_LOCATION и ACCESS_FINE_LOCATION.Приложение находится в React Native, это родной код Android.