如何使用 Altbeacon:Android 信标库在后台定位信标? [英] How to range beacons in background using Altbeacon: Android Beacon Library?

查看:22
本文介绍了如何使用 Altbeacon:Android 信标库在后台定位信标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个提供后台 Beacon 监控的应用程序.当用户从定义的区域进入信标时,我想开始测距.当应用程序处于后台并且它一直在监视并且用户进入我定义的区域时,我想开始测距并获取 InstanceID 或主要、次要值以确定是什么信标,连接到服务器并向用户发送通知.最好的情况是我可以在后台与服务器进行范围通信.我用这个示例实现了后台监控:https://altbeacon.github.io/android-beacon-library/samples.html.我还从这里下载了示例项目:https://github.com/AltBeacon/android-beacon-library-reference 以它为基础.

I'm developing an app which provides background Beacon monitoring. I would like to start ranging when user enters beacon from defined region. When app is in background and it's monitoring all the time and user enters my defined region I would like to start ranging and get InstanceID or Major, Minor value to determine what beacon is that, connect to server and send user a notfication. The best would be if I could range and communicate with server in background. I used this sample to achieve background monitoring: https://altbeacon.github.io/android-beacon-library/samples.html. I also downloaded sample project from here: https://github.com/AltBeacon/android-beacon-library-reference to base on it.

不幸的是,在此示例中,当用户进入区域时,Activity 已启动...我不希望这种情况发生.Soo 我的问题是:是否可以在后台定位信标?

Unfortunately in this example when user enters the region Activity is launched... I don't want that to happen. Soo my question is: Is it possible to range beacons in background?

在我的情况下也发生了奇怪的事情,因为当我将我的应用程序置于后台时,仍然从 MainActivity 调用方法didRangeBeaconsInRegion(收集信标,区域区域)",但没有找到信标.由于 beaconManager 处于后台模式,因此方法的调用频率也较低.当我启动没有发生的示例项目时.可能是因为我没有monitoringActivity.我的 MainActivity 在启动时会立即进行测距.当然,我尝试将所有内容设置为与 BeaconReferenceApplication 示例中完全相同.

Also weird thing happens in my case beacause when I put my app in background method "didRangeBeaconsInRegion(Collection beacons, Region region)" is still called from MainActivity but no beacon is found. Also method is called less often because beaconManager is in background mode. When I launched sample project that was not happening. Maybe it's because I don't have monitoringActivity. My MainActivity does ranging instantly when launched. Of course I tried to setup everything exactly the same as it is in an example BeaconReferenceApplication.

顺便说一句,我正在使用 Android 6.0.1 的 Nexus 5 上测试我的应用

BTW I'm testing my app on Nexus 5 with Android 6.0.1

提前感谢您的任何解决方案!

Thank you in advance for any solutions!

推荐答案

我终于想通了!其实我很简单,我从一开始就是这样做的,但由于错误我一直在使用旧版本的 Altbeacon 库,这导致了我所有的问题......嗯

I finally figured it out how to do this! Actually I was quite simple and I was doing it right from the begining but by a mistake I'v been using old version of Altbeacon library and that caused all my problems... Ehh

没关系.这是我的代码.Meybe有人可以使用它;)我通过创建集中式应用程序类来实现它,该类在进入定义的区域时为后台通知实现 BootstrapNotifier.我的班级还实现了 BeaconConsumer、RangeNotifier 接口,这些接口是进行信标测距所必需的.

Never mind. Here is my code. Meybe someone could use it ;) I made it by creating centralized Application class which implements BootstrapNotifier for background notifications when entering defined Region. My class also implements interfaces BeaconConsumer, RangeNotifier which are necessery to do beacon ranging.

package com.smartmachi.smartmachi_android;
import android.app.Application;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
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.BeaconParser;
import org.altbeacon.beacon.Identifier;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.powersave.BackgroundPowerSaver;
import org.altbeacon.beacon.startup.BootstrapNotifier;
import org.altbeacon.beacon.startup.RegionBootstrap;

import java.util.Collection;

public class BeaconReferenceApplication extends Application implements BootstrapNotifier, BeaconConsumer, RangeNotifier {
    private static final String TAG = "BeaconReferenceApp";
    private RegionBootstrap regionBootstrap;
    private BackgroundPowerSaver backgroundPowerSaver;
    private MainActivity rangingActivity = null;
    BeaconManager beaconManager;


    public void onCreate() {
        super.onCreate();
        beaconManager = 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"));

        Region region = new Region("backgroundRegion", Identifier.parse("0xf7826da6bc5b71e0893e"), null, null);
        regionBootstrap = new RegionBootstrap(this, region);

        backgroundPowerSaver = new BackgroundPowerSaver(this);

        beaconManager.setBackgroundBetweenScanPeriod(30000l);
        beaconManager.setForegroundBetweenScanPeriod(2000l);
        beaconManager.bind(this);
    }

    @Override
    public void didEnterRegion(Region region) {
        Log.d(TAG, "did enter region.");
        try {
            beaconManager.startRangingBeaconsInRegion(region);
        }
        catch (RemoteException e) {
            if (BuildConfig.DEBUG) Log.d(TAG, "Can't start ranging");
        }
    }

    @Override
    public void didExitRegion(Region region) {
        try {
            beaconManager.stopRangingBeaconsInRegion(region);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void didDetermineStateForRegion(int state, Region region) {
        Log.d(TAG,"I have just switched from seeing/not seeing beacons: " + state);
    }

    private void sendNotification(String text) {
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this)
                        .setContentTitle("Beacon Reference Application")
                        .setContentText(text)
                        .setSmallIcon(R.drawable.ic_launcher);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntent(new Intent(this, MainActivity.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 didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
        if (beacons.size() > 0) {
            for (Beacon b : beacons) {
                if(b.getId2().toString().equals("0x6d767674636e")) {
                    Log.e(TAG, "Beacon with my Instance ID found!");
                    sendNotification("Beacon with my Instance ID found!");
                }
            }
        }
    }

    @Override
    public void onBeaconServiceConnect() {
        beaconManager.setRangeNotifier(this);
    }
}

这篇关于如何使用 Altbeacon:Android 信标库在后台定位信标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆