0

我有 2 个来自 Radius 网络的 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");
    }
4

2 回答 2

0

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

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

这将对某些人使用完整的..

在我的测试设备中,我没有启用位置服务。所以无法在后台模式下接收信标。.所以启用位置服务以在后台获取信标通知,在另一侧前台不需要此设置..

谢谢..

于 2017-08-15T07:44:18.543 回答
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);

此外,您可以查看以下链接以获取有关使用 AltBeacon Library for Eddystone Beacon 的更多信息;

顺便说一句,您可以设置定位应用程序以仅检测 Eddystone 信标,以便您可以缩小问题范围并查看您的代码是否无法正常工作或您的信标。

于 2015-12-02T15:23:22.237 回答