未检测到信标,并且在检测到信标时需要有关如何启动新活动的建议

时间:2018-04-22 15:03:43

标签: java android android-studio beacon eddystone

我还有两天的时间来计算这个代码,所以我不得不选择在这里写一个问题,希望能得到一些答案。 我正在创建一个应该检测信标的应用程序,一旦检测到信标,就应该打开一个特定的活动。 我改编了https://altbeacon.github.io/android-beacon-library/eddystone-how-to.html的代码 我有一个eddystone信标但我在log.d中没有关于信标的信息,或者即使它扫描信标也没有。我必须提到我尝试了各种方法来完成这项工作,但我认为必须完成应用程序的压力开始转向我,我无法使其工作。 所以我的问题是:我的代码下面有什么问题,如果检测到信标,我怎样才能打开活动?谢谢

package com.example.florentina.discoverlondon;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.graphics.Region;
import android.os.Build;
import android.os.RemoteException;
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 Beacon_Activity extends MainActivity 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_main);


        // 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(BeaconParser.EDDYSTONE_UID_LAYOUT));
        // Detect the telemetry Eddystone-TLM frame:
        beaconManager.getBeaconParsers().add(new BeaconParser().
                setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT));
        // Start the Beacon Manager
        beaconManager.bind(this);
    }

    // When the Beacon Service starts, search for Beacons with the following Identifier
    @Override
    public void onBeaconServiceConnect() {
        Identifier myBeaconNamespaceId = Identifier.parse("620fb7f886bece801a16");
        Identifier myBeaconInstanceId = Identifier.parse("495162a49005");
        final org.altbeacon.beacon.Region region = new org.altbeacon.beacon.Region("myBeacons", myBeaconNamespaceId, myBeaconInstanceId, null);
        beaconManager.addMonitorNotifier(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.addRangeNotifier(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_main, 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);
    }
}

0 个答案:

没有答案