在Android中无法在后台检测我的信标

时间:2015-11-30 17:09:07

标签: altbeacon android-ibeacon beacon

我有来自Radius Network的2个Rad信标。我已使用Locate应用程序将它们配置为Eddystone。现在我编写了一个小程序来在后台发送通知,即应用程序未运行时。我需要在应用程序处于后台时发送通知。我正在使用Android信标库来实现这一目标。我已经尝试了几乎所有的链接,但我无法检测到它。

我在这里粘贴我的代码

public class BeaconReferenceApplication extends Application implements BootstrapNotifier, RangeNotifier {
    private static final String TAG = "BeaconReferenceApp";
    private RegionBootstrap regionBootstrap;
    private BackgroundPowerSaver backgroundPowerSaver;
    private boolean haveDetectedBeaconsSinceBoot = false;
    private MonitoringActivity monitoringActivity = null;    

    public void onCreate() {
        super.onCreate();

        BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);

        beaconManager.getBeaconParsers().clear();
        beaconManager.getBeaconParsers().add(new BeaconParser().
           setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
        beaconManager.setBackgroundBetweenScanPeriod(1000);

        Log.i(TAG, "setting up background monitoring for beacons and power saving");

        //Toast.makeText(getApplicationContext(), "Called!!!" , Toast.LENGTH_LONG).show();

        // wake up the app when a beacon is seen
        Region region = new Region("backgroundRegion", null, null, null);

        regionBootstrap = new RegionBootstrap(this, region);

        // simply constructing this class and holding a reference to it in your custom Application
        // class will automatically cause the BeaconLibrary to save battery whenever the application
        // is not visible.  This reduces bluetooth power usage by about 60%
        //backgroundPowerSaver = new BackgroundPowerSaver(this);

        // If you wish to test beacon detection in the Android Emulator, you can use code like this:
        // BeaconManager.setBeaconSimulator(new TimedBeaconSimulator() );
        // ((TimedBeaconSimulator) BeaconManager.getBeaconSimulator()).createTimedSimulatedBeacons();
    }

    @Override
    public void didEnterRegion(Region arg0) {
        // In this example, this class sends a notification to the user whenever a Beacon
        // matching a Region (defined above) are first seen.
        Log.i(TAG, "did enter region.");

        //sendNotification();

        if (!haveDetectedBeaconsSinceBoot) {
            Log.i(TAG, "auto launching MainActivity");

            // The very first time since boot that we detect an beacon, we launch the
            // MainActivity
            Intent intent = new Intent(this, MonitoringActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // Important:  make sure to add android:launchMode="singleInstance" in the manifest
            // to keep multiple copies of this activity from getting created if the user has
            // already manually launched the app.
            this.startActivity(intent);
            haveDetectedBeaconsSinceBoot = true;
        } else {
            if (monitoringActivity != null) {
                // If the Monitoring Activity is visible, we log info about the beacons we have
                // seen on its display
                monitoringActivity.logToDisplay("I see a beacon again" );
            } else {
                // If we have already seen beacons before, but the monitoring activity is not in
                // the foreground, we send a notification to the user on subsequent detections.
                Log.i(TAG, "Sending notification.");
                //sendNotification();
            }
        }    
    }

    @Override
    public void didExitRegion(Region region) {
        if (monitoringActivity != null) {
            monitoringActivity.logToDisplay("I no longer see a beacon.");
        }
    }

    @Override
    public void didDetermineStateForRegion(int state, Region region) {
        if (monitoringActivity != null) {
            monitoringActivity.logToDisplay("I have just switched from seeing/not seeing beacons: " + state);
        }
    }

    private void sendNotification() {
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this)
                        .setContentTitle("Beacon Reference Application")
                        .setContentText("An beacon is nearby in application.")
                        .setSmallIcon(R.drawable.ic_launcher);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntent(new Intent(this, MonitoringActivity.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());
    }

    public void setMonitoringActivity(MonitoringActivity activity) {
        Log.i("Log", "TEST ONLY");

        this.monitoringActivity = activity;
    }

    @Override
    public void didRangeBeaconsInRegion(Collection<Beacon> arg0, Region arg1) {
        // TODO Auto-generated method stub

        sendNotification();

        //Log.i("Log", "TEST ONLY");
    }

2 个答案:

答案 0 :(得分:0)

从您使用的布局中,我可以说您正在扫描AltBeacon而不是Eddystone。因此,您应该使用以下内容更改(或添加)信标布局;

  

S:0-1 = FEAA,M:2-2 = 00,P:3-3:-41,I:4-13,I:14-19

beaconManager.getBeaconParsers().add(BeaconParser.EDDYSTONE_UID_LAYOUT);

此外,您可以查看以下链接,了解有关使用适用于Eddystone Beacons的AltBeacon库的更多信息;

顺便说一句,您可以设置Locate应用程序以仅检测Eddystone Beacons,以便您可以缩小问题范围并查看代码是否无效或您的信标。

答案 1 :(得分:0)

我遇到了同样的问题,花了一天多的时间来解决这个问题。代码没有问题。

(作为开发人员犯了一个愚蠢的错误。)

这将完全用于某个人..

在我的测试设备中,我没有启用位置服务。因此无法在后台模式下接收信标。所以启用位置服务以获取背景上的信标通知,另一方面前景不需要此设置.. < / p>

谢谢..