无法在后台扫描

时间:2016-05-26 10:55:37

标签: android altbeacon beacon eddystone

这是根据altbeacon页面中的示例代码进行后台扫描的代码,但这不会发生我甚至没有得到输入区域标记。 RangingActivity ,它实现了BeaconConsumer范围信标,没有任何问题。当我第一次启动应用程序时,我看到的唯一日志是后台扫描已启动!。提前谢谢。

public class BackgroundScanner extends Application implements BootstrapNotifier {
private String TAG="Background Scanner";
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
private boolean haveDetectedBeaconsSinceBoot = false;
private RangingActivity rangingActivity;
@Override
public void onCreate() {
    Log.d(TAG,"Background Scanning Initiated!");
    super.onCreate();
    BeaconManager beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
    beaconManager.getBeaconParsers().add(new BeaconParser().
            setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT));
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));

    Region region = new Region("backgroundRegion",
            null, null, null);
    beaconManager.setBackgroundBetweenScanPeriod(60000L);
    try {
        beaconManager.updateScanPeriods();
    } catch (RemoteException e) {
        e.printStackTrace();
    }
    regionBootstrap = new RegionBootstrap(this, region);
    backgroundPowerSaver = new BackgroundPowerSaver(this);

}

@Override
public void didEnterRegion(Region region) {
    Log.d(TAG, "did enter region.");
    sendNotification();
}
@SuppressLint("NewApi")
private void sendNotification() {
    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this)
                    .setContentTitle("Notification!")
                    .setContentText("Beacon Nearby!")
                    .setSmallIcon(R.mipmap.ic_launcher);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntent(new Intent(this, RangingActivity.class));
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    builder.setContentIntent(resultPendingIntent);
    NotificationManager notificationManager =
            (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, builder.build());
}

@Override
public void didExitRegion(Region region) {

}

@Override
public void didDetermineStateForRegion(int i, Region region) {

}

1 个答案:

答案 0 :(得分:0)

图书馆可能认为它已经在该地区。从版本2.8开始,库会在应用程序重新启动时跟踪区域状态,以防止多个private void CenterToParentOverride() { this.Location = new Point( this.MdiParent.ClientSize.Width / 2 - this.Width / 2, this.MdiParent.ClientSize.Height / 2 - this.Height / 2); } 回调。你可以通过关闭附近的所有信标(或关闭蓝牙)来测试这一点,直到你得到didEnterRegion回调。然后重新打开它们,看看你是否收到了didExitRegion回调。