使用Android Beacon Library启动和停止Beacon扫描仪应用程序

时间:2016-06-02 09:10:10

标签: java android android-studio ibeacon altbeacon

我已经构建了一个小应用程序,它使用Android Beacon Library在用户启动应用程序后查找附近的信标:http://www.software7.com/blog/creating-a-beacon-app-for-android-in-less-than-10-minutes-from-scratch/

如果应用是startet且找到属于特定区域的信标,则会在控制台中打印信息,也会在用户界面中的文本标签上打印。

我正在 OnCreate OnDestroy 方法中启动和关闭Beacon Manager,这些方法正在管理搜索过程。

当我开始第一次时,应用程序运行良好。但是当我用手机上的箭头后退按钮关闭它并再次打开时,文本标签上会出现没有更多控制台消息或更新。这也会在屏幕进入睡眠状态时发生,我必须再次解锁 - 没有控制台更新,没有UI更新。

根据控制台消息, OnBeaconServiceConnect 正在恢复应用,但我没有收到来自 didEnterRegion 的日志消息,也没有 setRangeNotifier 再次。

我还尝试将绑定和取消绑定放入OnPause和OnResume,这也没有用。

如何恢复搜索&再次找到正确的流程? 谢谢你的帮助:))

这是我评论的代码,以防你想看一下:

package de.mediatoni.beacontut01;

import android.graphics.Region;
import android.os.RemoteException;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

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.MonitorNotifier;
import org.altbeacon.beacon.RangeNotifier;

import java.util.Collection;


public class BeaconActivity extends ActionBarActivity implements BeaconConsumer{

    public static final String TAG = "BeaconsEverywhere";
    // Beacon Manager Variable
    private BeaconManager beaconManager;

    //GUI Text Label
    TextView rangeElement;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_beacon);

        // Vars for XML Elements
        rangeElement = (TextView) findViewById(R.id.range);

        // Instantiate a Beacon Manager via factory method
        beaconManager = BeaconManager.getInstanceForApplication(this);

        // Tell Library how to decode the signal
        beaconManager.getBeaconParsers().add(new BeaconParser()
                .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
        // Start the Beacon Manager
        beaconManager.bind(this);
    }

    // When the Beacon Service starts, search for Beacons with the following Identifier
    @Override
    public void onBeaconServiceConnect() {
        final org.altbeacon.beacon.Region region = new org.altbeacon.beacon.Region("myBeacons", Identifier.parse("73676723-7400-0000-ffff-0000ffff0005"), null, null);
        beaconManager.setMonitorNotifier(new MonitorNotifier() {
            // If the phone enters a Beacon region
            @Override
            public void didEnterRegion(org.altbeacon.beacon.Region region) {
                try {
                    Log.d(TAG, "did Enter Region");
                    beaconManager.startRangingBeaconsInRegion(region);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            // If the phone leaves a Beacon region
            @Override
            public void didExitRegion(org.altbeacon.beacon.Region region) {
                try {
                    Log.d(TAG, "did Exit Region");
                    beaconManager.stopRangingBeaconsInRegion(region);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void didDetermineStateForRegion(int i, org.altbeacon.beacon.Region region) {

            }
        });

        // If the phone finds a Beacon fitting the rules, print it in the console
        beaconManager.setRangeNotifier(new RangeNotifier() {
            @Override
            //Log out welche beacons in der Nähe sind
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, org.altbeacon.beacon.Region region) {
                for(final Beacon oneBeacon : beacons) {
                    Log.d(TAG, "distance: " + oneBeacon.getDistance() + "id: " + oneBeacon.getId1() + "/" + oneBeacon.getId2() + "/" + oneBeacon.getId3());

                    // Access UI thread and make changes to the UI
                    // Placing rangeElement.setText outside of the Runnable will crash the App
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // Change the text label in the UI
                            rangeElement.setText(String.valueOf(oneBeacon.getDistance()));
                        }
                    });
                }
            }
        });

        try {
            beaconManager.startMonitoringBeaconsInRegion(region);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    // When the App gets closed, stop the Beacon Manager
    @Override
    protected void onDestroy() {
        super.onDestroy();
        beaconManager.unbind(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_beacon, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:1)

问题很可能是当Activity重新启动时你没有得到第二个入口事件,因为信标仍然可见并且从未离开视图。从库版本2.8开始,除非存在相应的退出事件,否则将禁止重复输入事件。

为了在每次开始监控时获得进入事件,只需在再次开始监控之前停止监控该区域。像这样:

beaconManager.stopMonitoringBeaconsInRegion(region);
beaconManager.startMonitoringBeaconsInRegion(region);