要在后台使用前台服务扫描beacons并使RegionBootstrap / BootstrapNotifier正常工作,您可以按照以下步骤进行操作:
implementation 'org.altbeacon:android-beacon-library:2.16.3'
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.Region;
public class BeaconService extends Service implements BeaconConsumer, BootstrapNotifier {
private static final String TAG = "BeaconService";
private BeaconManager beaconManager;
@Override
public void onCreate() {
super.onCreate();
// 初始化BeaconManager
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
// 设置前台服务通知
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "beacon_scanning_channel";
NotificationChannel channel = new NotificationChannel(channelId, "Beacon Scanning", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Scanning for Beacons");
channel.enableLights(false);
channel.setLightColor(Color.BLUE);
channel.enableVibration(false);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Beacon Scanning")
.setContentText("Scanning for Beacons")
.setContentIntent(getMainActivityIntent())
.build();
startForeground(1, notification);
} else {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Beacon Scanning")
.setContentText("Scanning for Beacons")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(getMainActivityIntent())
.build();
startForeground(1, notification);
}
// 设置BeaconConsumer
beaconManager.bind(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
stopForeground(true);
}
@Override
public void onBeaconServiceConnect() {
// 定义要扫描的Region
Region region = new Region("all-beacons-region", null, null, null);
// 设置BootstrapNotifier
beaconManager.addBootstrapNotifier(this);
try {
// 启动Beacon扫描
beaconManager.startMonitoringBeaconsInRegion(region);
} catch (RemoteException e) {
Log.e(TAG, "Error starting beacon monitoring", e);
}
}
@Override
public Context getApplicationContext() {
return this;
}
@Override
public void didEnterRegion(Region region) {
Log.d(TAG, "Entered region: " + region.getUniqueId());
}
@Override
public void didExitRegion(Region region) {
Log.d(TAG, "Exited region: " + region.getUniqueId());
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
Log.d(TAG, "Region state changed: " + state);
}
private PendingIntent getMainActivityIntent() {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
return PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG