简体   繁体   English

使用Awareness API获取信标围栏时间太长而且不准确

[英]Getting Beacon Fence Using Awareness API it's take too much time and not accurate

This is my code for getting BeaconFence.It's take too much time and not provide accurate information i fetch two fence lost and found 这是我获取BeaconFence的代码,这花费了太多时间并且没有提供准确的信息,我获取了两个丢失并发现的围栏

//BeaconFenceActivity.java //BeaconFenceActivity.java

import android.Manifest;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.azilen.awarenessapidemo.R;
import com.google.android.gms.awareness.Awareness;
import com.google.android.gms.awareness.fence.AwarenessFence;
import com.google.android.gms.awareness.fence.BeaconFence;
import com.google.android.gms.awareness.fence.FenceState;
import com.google.android.gms.awareness.fence.FenceUpdateRequest;
import com.google.android.gms.awareness.state.BeaconState;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.ResultCallbacks;
import com.google.android.gms.common.api.Status;

import java.util.Arrays;
import java.util.List;

public class BeaconFenceActivity extends AppCompatActivity {

    private GoogleApiClient mGoogleApiClient;
    private static final int PERMISSION_REQUEST_ACCESS_FINE_LOCATION = 940;
    private TextView txtBeacon;


    private static final String BEACON_FENCE_KEY = "BEACON_FENCE_KEY";
    private static final int BEACON_ZONE_IN = 2;
    private static final int BEACON_ZONE_OUT = 1;

    private PendingIntent mPendingIntent;
    private BeaconFenceReceiver mBeaconFenceReceiver;
    private ProgressDialog mProgress;

    //Replace this with app's Google project name
    private static final List<BeaconState.TypeFilter> BEACON_TYPE_FILTERS = Arrays.asList
            (BeaconState.TypeFilter.with("awarenessapidemo-158205", "beacondemo"));

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_beacon_fence);
        mProgress = new ProgressDialog(BeaconFenceActivity.this);
        mProgress.setTitle("Geting Near Beacon");
        mProgress.setMessage("Please wait..");

        txtBeacon = (TextView) findViewById(R.id.txt_fence_beacon);
        mGoogleApiClient = new GoogleApiClient.Builder(BeaconFenceActivity.this).addApi(Awareness.API).build();
        mGoogleApiClient.connect();

        mBeaconFenceReceiver = new BeaconFenceReceiver();
        Intent intent = new Intent(BeaconFenceReceiver.BEACON_FENCE_RECEIVER_ACTION);
        mPendingIntent = PendingIntent.getBroadcast(BeaconFenceActivity.this, 1, intent, 0);
    }


    @Override
    protected void onStart() {
        super.onStart();
        getBeaconDetails();
        registerReceiver(mBeaconFenceReceiver, new IntentFilter(BeaconFenceReceiver.BEACON_FENCE_RECEIVER_ACTION));
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterFences();
        unregisterReceiver(mBeaconFenceReceiver);
    }


    private void getBeaconDetails() {
        mProgress.show();
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    PERMISSION_REQUEST_ACCESS_FINE_LOCATION);
            mProgress.hide();
        } else {
            AwarenessFence beaconFoundFence = BeaconFence.found(BEACON_TYPE_FILTERS);
            AwarenessFence lostFence = BeaconFence.lost(BEACON_TYPE_FILTERS);
            AwarenessFence orFence = AwarenessFence.or(lostFence, beaconFoundFence);

            Awareness.FenceApi.updateFences(mGoogleApiClient,
                    new FenceUpdateRequest.Builder()
                            .addFence(BEACON_FENCE_KEY, orFence, mPendingIntent)
                         /* .addFence(BEACON_FENCE_KEY, beaconFoundFence, mPendingIntent)
                            .addFence(BEACON_FENCE_KEY, lostFence, mPendingIntent)
                           */
                            .build()).setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(@NonNull Status status) {
                    if (status.isSuccess()) {
                        Toast.makeText(BeaconFenceActivity.this, "Fence Registered", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(BeaconFenceActivity.this, "Fence Not Registered", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            mProgress.hide();
        }
    }

    private void unregisterFences() {
        Awareness.FenceApi.updateFences(
                mGoogleApiClient,
                new FenceUpdateRequest.Builder()
                        .removeFence(BEACON_FENCE_KEY)
                        .build()).setResultCallback(new ResultCallbacks<Status>() {
            @Override
            public void onSuccess(@NonNull Status status) {
                Toast.makeText(BeaconFenceActivity.this, "Fence Removed", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(@NonNull Status status) {
                Toast.makeText(BeaconFenceActivity.this, "Fence Not Removed", Toast.LENGTH_SHORT).show();
            }
        });

    }

    public void checkRestart(View view) {
        getBeaconDetails();
        registerReceiver(mBeaconFenceReceiver, new IntentFilter(BeaconFenceReceiver.BEACON_FENCE_RECEIVER_ACTION));
    }

    public class BeaconFenceReceiver extends BroadcastReceiver {

        public static final String BEACON_FENCE_RECEIVER_ACTION = "com.azilen.awarenessapidemo.activities.fence.BeaconFenceReceiver.BEACON_FENCE_RECEIVER_ACTION";

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("Recived", "Received a Beacon Fence Broadcast");
            FenceState fenceState = FenceState.extract(intent);
            Log.e("FenceState Status:-", String.valueOf(fenceState.getFenceKey()));
            if (TextUtils.equals(fenceState.getFenceKey(), BEACON_FENCE_KEY)) {
                Log.e("FenceState:-", String.valueOf(fenceState.getCurrentState()));
                switch (fenceState.getCurrentState()) {
                    case FenceState.TRUE: {
                        setBeaconState(BEACON_ZONE_IN);
                        Toast.makeText(BeaconFenceActivity.this, "You've entered the beacon zone!", Toast.LENGTH_SHORT).show();
                        Log.e("Beacon", "In Range");
                        break;
                    }
                    case FenceState.FALSE: {
                        setBeaconState(BEACON_ZONE_OUT);
                        Log.e("Beacon", "Out of Range");
                        Toast.makeText(BeaconFenceActivity.this, "You've Out of beacon Range!", Toast.LENGTH_SHORT).show();
                        break;
                    }
                    case FenceState.UNKNOWN: {
                        setBeaconState(FenceState.UNKNOWN);
                        Log.e("Beacon", "UNKNOWN");
                        Toast.makeText(BeaconFenceActivity.this, "Oops, Beacon status is unknown!", Toast.LENGTH_SHORT).show();
                        break;
                    }
                }


            }
        }
    }

    private void setBeaconState(int beaconState) {
        if (beaconState == BEACON_ZONE_IN) {
            txtBeacon.setText("You've entered the beacon zone!");
        } else if (beaconState == BEACON_ZONE_OUT) {
            txtBeacon.setText("You're not in the beacon zone..");
        } else {
            txtBeacon.setText("Oops, Beacon status is unknown!");
        }
    }
}

I Hope you can understand my question. 希望您能理解我的问题。 Thank you. 谢谢。

I can't tell you why it is taking to much time. 我不能告诉你为什么要花很多时间。 But in case of the accuracy you have to keep in mind, that your position to the beacons is calculated from the signal/signalstrength that beacons send and like every signal in the microwave spectrum it gets reflected, blocked etc. The environment in which you use your beacons could be far from ideal to get the accuracy you want with the information provided by the beacons. 但是,在精度方面,您必须牢记,您在信标中的位置是根据信标发送的信号/信号强度来计算的,并且像微波频谱中的每个信号一样,它都会被反射,阻挡等。使用环境通过信标提供的信息,您的信标可能远远不够理想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM