Advertisement
Guest User

Untitled

a guest
May 21st, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.16 KB | None | 0 0
  1. package com.daniellazarov.tempapp;
  2.  
  3. import android.Manifest;
  4. import android.app.AlertDialog;
  5. import android.content.DialogInterface;
  6. import android.content.pm.PackageManager;
  7. import android.os.Build;
  8. import android.os.Bundle;
  9. import android.os.RemoteException;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.util.Log;
  12. import android.widget.TextView;
  13.  
  14. import org.altbeacon.beacon.Beacon;
  15. import org.altbeacon.beacon.BeaconConsumer;
  16. import org.altbeacon.beacon.BeaconManager;
  17. import org.altbeacon.beacon.BeaconParser;
  18. import org.altbeacon.beacon.MonitorNotifier;
  19. import org.altbeacon.beacon.Region;
  20.  
  21. import java.util.Collection;
  22.  
  23. public class MainActivity extends AppCompatActivity implements BeaconConsumer{
  24.  
  25.     protected static final String TAG = "MonitoringActivity";
  26.     private static final int PERMISSION_REQUEST_COARSE_LOCATION = 0;
  27.     private BeaconManager beaconManager;
  28.  
  29.     private boolean beaconInsideZone;
  30.  
  31.     @Override
  32.     protected void onCreate(Bundle savedInstanceState) {
  33.         super.onCreate(savedInstanceState);
  34.         setContentView(R.layout.activity_main);
  35.  
  36.         beaconManager = BeaconManager.getInstanceForApplication(this);
  37. //        beaconManager.setForegroundScanPeriod(1000);
  38. //        beaconManager.setBackgroundScanPeriod(5000);
  39. //        beaconManager.setBackgroundBetweenScanPeriod(5000);
  40.         beaconManager.getBeaconParsers().add(new BeaconParser().
  41.                 setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
  42.         beaconManager.bind(this);
  43.  
  44.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  45.             // Android M Permission check
  46.             if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  47.                 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
  48.                 builder.setTitle("This app needs location access");
  49.                 builder.setMessage("Please grant location access so this app can detect beacons.");
  50.                 builder.setPositiveButton(android.R.string.ok, null);
  51.                 builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
  52.                     public void onDismiss(DialogInterface dialog) {
  53.                         requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
  54.                     }
  55.                 });            builder.show();
  56.             }
  57.         }
  58.  
  59.     }
  60.  
  61.     protected void onDestroy () {
  62.         super.onDestroy();
  63.         beaconManager.unbind(this);
  64.     }
  65.  
  66.     public void onBeaconServiceConnect() {
  67.  
  68. //        Region region1 = new Region("RaspberryPi", Identifier.parse("E20A39F4-73F5-4BC4-A12F-17D1AD07A961"), null, null);
  69.         Region region1 = new Region("RaspberryPi", null, null, null);
  70.  
  71.         beaconInsideZone = false;
  72.         beaconManager.setMonitorNotifier(new MonitorNotifier() {
  73.             @Override
  74.             public void didEnterRegion(Region region) {
  75.                 Log.i(TAG, "I just saw a beacon for the first time!");
  76.             }
  77.  
  78.             @Override
  79.             public void didExitRegion(Region region) {
  80.                 Log.i(TAG, "I no longer see a beacon");
  81.             }
  82.  
  83.             @Override
  84.             public void didDetermineStateForRegion(int state, Region region) {
  85.                 Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+ state);
  86.             }
  87.         });
  88.  
  89. //        beaconManager.setRangeNotifier(new RangeNotifier() {
  90. //            @Override
  91. //            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
  92. ////                if(beacons.size() > 0) {
  93. ////                    Beacon firstBeacon = beacons.iterator().next();
  94. ////                    Log.i(TAG, "The first beacon " + firstBeacon.toString() + " is about " + firstBeacon.getDistance() + " meters away.");
  95. ////                    logToDisplay("The first beacon " + firstBeacon.toString() + " is about " + firstBeacon.getDistance() + " meters away.");
  96. ////                    logToDisplay("Beacon uuid: " + firstBeacon.getId1().toString());
  97. //                    for(Beacon tempBeacon : beacons) {
  98. //                        if(tempBeacon.getDistance() < 2.0 && beaconInsideZone == false && foundCorrectBeacon(tempBeacon)) {
  99. //                            logToDisplay("INSIDE " + "ID " + tempBeacon.getId1());
  100. //                            beaconInsideZone = true;
  101. //                        }
  102. //                        else if(tempBeacon.getDistance() > 15.0 && beaconInsideZone == true && foundCorrectBeacon(tempBeacon)) {
  103. //                            logToDisplay("OUTSIDE");
  104. //                            beaconInsideZone = false;
  105. //                        }
  106. //                    }
  107. //            }
  108. //        });
  109.         try {
  110.             beaconManager.startRangingBeaconsInRegion(new Region("myRegion", null, null, null));
  111.             beaconManager.startMonitoringBeaconsInRegion(region1);
  112.         } catch (RemoteException e) {      }
  113.  
  114.     }
  115.  
  116.     public void clearBeaconList (Collection<Beacon> collectionToClear) {
  117.         collectionToClear.clear();
  118.         Log.i("clearCollection ----", "Collection cleared");
  119.     }
  120.  
  121.     public boolean foundCorrectBeacon (Beacon beacon) {
  122.         String wantedBeaconUuid = "e20a39f4-73f5-4bc4-a12f-17d1ad07a961";
  123.  
  124.         if(beacon.getId1().toString().equals(wantedBeaconUuid)) {
  125.             return true;
  126.         }
  127.         return false;
  128.     }
  129.  
  130.     private void logToDisplay(final String line) {
  131.         runOnUiThread(new Runnable() {
  132.             public void run() {
  133.                 TextView editText = (TextView)MainActivity.this
  134.                         .findViewById(R.id.monitoringText);
  135.                 editText.append(line+"\n");
  136.             }
  137.         });
  138.     }
  139.  
  140.     private void rangedBeacon(final String line) {
  141.         runOnUiThread(new Runnable() {
  142.             public void run() {
  143.                 TextView editText = (TextView)MainActivity.this
  144.                         .findViewById(R.id.rangedBeacon);
  145.             }
  146.         });
  147.     }
  148.  
  149.     public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
  150.         switch (requestCode) {
  151.             case PERMISSION_REQUEST_COARSE_LOCATION: {
  152.                 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  153.                     Log.d(TAG, "coarse location permission granted");
  154.                 } else {
  155.                     final AlertDialog.Builder builder = new AlertDialog.Builder(this);
  156.                     builder.setTitle("Functionality limited");
  157.                     builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons when in the background.");
  158.                     builder.setPositiveButton(android.R.string.ok, null);
  159.                     builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
  160.  
  161.                         @Override
  162.                         public void onDismiss(DialogInterface dialog) {
  163.                         }
  164.  
  165.                     });
  166.                     builder.show();
  167.                 }
  168.                 return;
  169.             }
  170.         }
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement