Android Beacon Library - Beacon的发现和消失时间

时间:2017-08-04 05:15:06

标签: android bluetooth-lowenergy ibeacon

在监控活动中(我的布局是我在其中一个中有两个textview将存储discoverTime并存储另一个disappearTime)。

    public class MonitoringActivity extends Activity  {
    protected static final String TAG = "MonitoringActivity";
    private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;

    TextView discoverTime;
    TextView disappearTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_monitoring);
        verifyBluetooth();
        logToDisplay("Application just launched");

        discoverTime = (TextView) findViewById(R.id.discoverTime);
        disappearTime = (TextView) findViewById(R.id.disappearTime);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // Android M Permission check
            if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("This app needs location access");
                builder.setMessage("Please grant location access so this app can detect beacons in the background.");
                builder.setPositiveButton(android.R.string.ok, null);
                builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

                    @TargetApi(23)
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                                PERMISSION_REQUEST_COARSE_LOCATION);
                    }

                });
                builder.show();
            }
        }
    }

我想在发现或消失信标时更改文本视图信息,因此在didEnterRegiondidExitRegion中我设置了文本视图文本。

@Override
    public void didEnterRegion(Region arg0) {
        // In this example, this class sends a notification to the user whenever a Beacon
        // matching a Region (defined above) are first seen.
        Log.d(TAG, "did enter region.");
        if (!haveDetectedBeaconsSinceBoot) {
            Log.d(TAG, "auto launching MainActivity");

            // The very first time since boot that we detect an beacon, we launch the
            // MainActivity
            Intent intent = new Intent(this, MonitoringActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // Important:  make sure to add android:launchMode="singleInstance" in the manifest
            // to keep multiple copies of this activity from getting created if the user has
            // already manually launched the app.

            //this.startActivity(intent);

            SimpleDateFormat df = new SimpleDateFormat("EEE, d MMM yyyy, HH:mm");
            String date = df.format(Calendar.getInstance().getTime());
            //Toast.makeText(getApplicationContext(), date, Toast.LENGTH_LONG).show();

            monitoringActivity.discoverTime.setText(date);


            haveDetectedBeaconsSinceBoot = true;
        } else {
            if (monitoringActivity != null) {
                // If the Monitoring Activity is visible, we log info about the beacons we have
                // seen on its display
                monitoringActivity.logToDisplay("I see a beacon again" );
            } else {
                // If we have already seen beacons before, but the monitoring activity is not in
                // the foreground, we send a notification to the user on subsequent detections.
                Log.d(TAG, "Sending notification.");
                sendNotification();
            }
        }


    }

    @Override
    public void didExitRegion(Region region) {
        if (monitoringActivity != null) {
            monitoringActivity.logToDisplay("I no longer see a beacon.");
        }

        SimpleDateFormat df = new SimpleDateFormat("EEE, d MMM yyyy, HH:mm");
        String date = df.format(Calendar.getInstance().getTime());
        //Toast.makeText(getApplicationContext(), date, Toast.LENGTH_LONG).show();
        monitoringActivity.disappearTime.setText(date);
    }

我测试了我的Beacon覆盖范围,但它没有用。

1 个答案:

答案 0 :(得分:0)

当您说您正在使用Android Beacon Library时,我认为您的意思是altbeacon,它不是Google库 - 它是第三方库。在您的代码中,我看不到任何代码来定义您要查找的信标并设置区域。我想你需要调用BeaconManager来设置一个Region。此外,您最好直接使用Android Bluetooth APIs