Created
March 5, 2016 19:16
-
-
Save Yannicked/62b26b4c892b04f74c9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RangingActivity extends Activity implements BeaconConsumer { | |
protected static final String TAG = "RangingActivity"; | |
private BeaconManager beaconManager; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_ranging); | |
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() { | |
@Override | |
public void onDismiss(DialogInterface dialog) { | |
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION); | |
} | |
}); | |
builder.show(); | |
} | |
} | |
beaconManager = BeaconManager.getInstanceForApplication(this); | |
// To detect proprietary beacons, you must add a line like below corresponding to your beacon | |
// type. Do a web search for "setBeaconLayout" to get the proper expression. | |
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); | |
} | |
@Override | |
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; | |
} | |
} | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
beaconManager.unbind(this); | |
} | |
@Override | |
public void onBeaconServiceConnect() { | |
beaconManager.setRangeNotifier(new RangeNotifier() { | |
@Override | |
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { | |
if (beacons.size() > 0) { | |
Log.i(TAG, "The first beacon I see is about "+beacons.iterator().next().getDistance()+" meters away."); | |
} | |
} | |
}); | |
try { | |
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null)); | |
} catch (RemoteException e) { } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment