如何使用Altbeacon:Android信标库在后台对信标进行测距? [英] How to range beacons in background using Altbeacon: Android Beacon Library?

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

问题描述

我正在开发一个提供后台信标监视的应用程序.当用户从定义的区域输入信标时,我想开始进行测距.当应用程序处于后台并且一直在监视并且用户进入我定义的区域时,我想开始进行范围测试并获取InstanceID或Major,Minor值来确定那是什么信标,然后连接到服务器并向用户发送通知.最好的办法是,我可以在后台与服务器进行通讯并与之通信.我使用此示例来实现后台监视: 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.

不幸的是,在此示例中,当用户进入区域时,活动已启动...我不希望这种情况发生.所以我的问题是:可以在后台对信标进行测距吗?

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?

当我将应用程序放入后台方法"didRangeBeaconsInRegion(Collection beacons,Region region)"仍然从MainActivity调用时,在我的情况下,也发生了奇怪的事情,但是没有找到信标.由于beaconManager处于后台模式,因此也很少调用method.当我启动示例项目时,这没有发生.也许是因为我没有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有人可以使用它;) 我通过创建集中的Application类来实现这一目的,该类在输入定义的Region时实现了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天全站免登陆