Add to build.gradle
the library
android { repositories { maven { url "https://outbarriers.com/sdk/repository/" } } } dependencies { compile('com.outbarriers:outbarrierssdk:1.2.0:release@aar') { transitive = true; } }
(ver 1.2.0)
<application>
in AndroidManifest.xml
, add the API KEY
and API SECRET
for your app<meta-data android:name="com.outbarriers.sdk.API_KEY" android:value="YOUR_API_KEY" /> <meta-data android:name="com.outbarriers.sdk.API_SECRET" android:value="YOUR_API_SECRET" />
public class YourActivity extends AppCompatActivity implements OutbeaconListener { private OutbarriersManager oManager; private final int PERMISSION_REQUEST_COARSE_LOCATION = 0x123; ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("App need location permission"); builder.setMessage("To detect beacons is necessary that you allow the location permission."); builder.setPositiveButton(android.R.string.ok, null); builder.setOnDismissListener(new DialogInterface.OnDismissListener() { @TargetApi(Build.VERSION_CODES.M) public void onDismiss(DialogInterface dialog) { requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION); } }); builder.show(); } else { initOBManager(); } } private void initOBManager() { OutbarriersManager.getManager(this, new OutbarriersManagerListener() { @Override public void onSuccess(OutbarriersManager ob) { oManager = ob; oManager.setOnOutbeaconDetected(YourActivity.this); oManager.setGPSEnabled(false); oManager.startDetection(); } @Override public void onError(OutbarriersManagerError err) { // Handle OutbarriersManager error } }); } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case PERMISSION_REQUEST_COARSE_LOCATION: { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { initOBManager(); } else { // Handle no permission location context } return; } } } @Override public void onOutbeaconDetected(OutbeaconMatch od) { Toast.makeText(getApplicationContext(), "Outbeacon detected: " + od.outbeacon.uuid, Toast.LENGTH_LONG).show(); } @Override public void onDetectionError(OutbarriersError error) { // Handle Outbarriers Beacon detection error } @Override public void onNearestBeacons(OutbeaconList outbeaconList) { // Handle nearest beacons } @Override public void onScanningStatus(boolean b) { // Handle scanning status } @Override public void onDestroy() { super.onDestroy(); if (oManager != null) { oManager.onDestroy(); } } }