Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.daniellazarov.tempapp;
- import android.Manifest;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.pm.PackageManager;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.RemoteException;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- 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.MonitorNotifier;
- import org.altbeacon.beacon.Region;
- import java.util.Collection;
- public class MainActivity extends AppCompatActivity implements BeaconConsumer{
- protected static final String TAG = "MonitoringActivity";
- private static final int PERMISSION_REQUEST_COARSE_LOCATION = 0;
- private BeaconManager beaconManager;
- private boolean beaconInsideZone;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- beaconManager = BeaconManager.getInstanceForApplication(this);
- // beaconManager.setForegroundScanPeriod(1000);
- // beaconManager.setBackgroundScanPeriod(5000);
- // beaconManager.setBackgroundBetweenScanPeriod(5000);
- beaconManager.getBeaconParsers().add(new BeaconParser().
- setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
- beaconManager.bind(this);
- 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.");
- builder.setPositiveButton(android.R.string.ok, null);
- builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
- public void onDismiss(DialogInterface dialog) {
- requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
- }
- }); builder.show();
- }
- }
- }
- protected void onDestroy () {
- super.onDestroy();
- beaconManager.unbind(this);
- }
- public void onBeaconServiceConnect() {
- // Region region1 = new Region("RaspberryPi", Identifier.parse("E20A39F4-73F5-4BC4-A12F-17D1AD07A961"), null, null);
- Region region1 = new Region("RaspberryPi", null, null, null);
- beaconInsideZone = false;
- beaconManager.setMonitorNotifier(new MonitorNotifier() {
- @Override
- public void didEnterRegion(Region region) {
- Log.i(TAG, "I just saw a beacon for the first time!");
- }
- @Override
- public void didExitRegion(Region region) {
- Log.i(TAG, "I no longer see a beacon");
- }
- @Override
- public void didDetermineStateForRegion(int state, Region region) {
- Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+ state);
- }
- });
- // beaconManager.setRangeNotifier(new RangeNotifier() {
- // @Override
- // public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
- //// if(beacons.size() > 0) {
- //// Beacon firstBeacon = beacons.iterator().next();
- //// Log.i(TAG, "The first beacon " + firstBeacon.toString() + " is about " + firstBeacon.getDistance() + " meters away.");
- //// logToDisplay("The first beacon " + firstBeacon.toString() + " is about " + firstBeacon.getDistance() + " meters away.");
- //// logToDisplay("Beacon uuid: " + firstBeacon.getId1().toString());
- // for(Beacon tempBeacon : beacons) {
- // if(tempBeacon.getDistance() < 2.0 && beaconInsideZone == false && foundCorrectBeacon(tempBeacon)) {
- // logToDisplay("INSIDE " + "ID " + tempBeacon.getId1());
- // beaconInsideZone = true;
- // }
- // else if(tempBeacon.getDistance() > 15.0 && beaconInsideZone == true && foundCorrectBeacon(tempBeacon)) {
- // logToDisplay("OUTSIDE");
- // beaconInsideZone = false;
- // }
- // }
- // }
- // });
- try {
- beaconManager.startRangingBeaconsInRegion(new Region("myRegion", null, null, null));
- beaconManager.startMonitoringBeaconsInRegion(region1);
- } catch (RemoteException e) { }
- }
- public void clearBeaconList (Collection<Beacon> collectionToClear) {
- collectionToClear.clear();
- Log.i("clearCollection ----", "Collection cleared");
- }
- public boolean foundCorrectBeacon (Beacon beacon) {
- String wantedBeaconUuid = "e20a39f4-73f5-4bc4-a12f-17d1ad07a961";
- if(beacon.getId1().toString().equals(wantedBeaconUuid)) {
- return true;
- }
- return false;
- }
- private void logToDisplay(final String line) {
- runOnUiThread(new Runnable() {
- public void run() {
- TextView editText = (TextView)MainActivity.this
- .findViewById(R.id.monitoringText);
- editText.append(line+"\n");
- }
- });
- }
- private void rangedBeacon(final String line) {
- runOnUiThread(new Runnable() {
- public void run() {
- TextView editText = (TextView)MainActivity.this
- .findViewById(R.id.rangedBeacon);
- }
- });
- }
- public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
- switch (requestCode) {
- case PERMISSION_REQUEST_COARSE_LOCATION: {
- if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- Log.d(TAG, "coarse location permission granted");
- } else {
- final AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("Functionality limited");
- builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons when in the background.");
- builder.setPositiveButton(android.R.string.ok, null);
- builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
- @Override
- public void onDismiss(DialogInterface dialog) {
- }
- });
- builder.show();
- }
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement