Created
January 10, 2019 13:46
-
-
Save mentos1386/c1a535ea66f387b5604fec5696912003 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
apply plugin: 'com.android.application' | |
android { | |
compileSdkVersion 28 | |
defaultConfig { | |
applicationId "com.example.tinej.iota3" | |
minSdkVersion 27 | |
targetSdkVersion 28 | |
versionCode 1 | |
versionName "1.0" | |
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
dependencies { | |
implementation fileTree(dir: 'libs', include: ['*.jar']) | |
implementation 'com.android.support:appcompat-v7:28.0.0-alpha1' | |
implementation 'com.android.support.constraint:constraint-layout:1.1.0' | |
implementation 'org.altbeacon:android-beacon-library:2+' | |
testImplementation 'junit:junit:4.12' | |
androidTestImplementation 'com.android.support.test:runner:1.0.2' | |
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' | |
} |
This file contains hidden or 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
package com.example.tinej.iota3; | |
import android.Manifest; | |
import android.app.AlertDialog; | |
import android.content.DialogInterface; | |
import android.content.pm.PackageManager; | |
import android.os.Build; | |
import android.os.RemoteException; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import org.altbeacon.beacon.Beacon; | |
import org.altbeacon.beacon.BeaconConsumer; | |
import org.altbeacon.beacon.BeaconManager; | |
import org.altbeacon.beacon.BeaconParser; | |
import org.altbeacon.beacon.Identifier; | |
import org.altbeacon.beacon.MonitorNotifier; | |
import org.altbeacon.beacon.RangeNotifier; | |
import org.altbeacon.beacon.Region; | |
import java.util.Collection; | |
public class MainActivity extends AppCompatActivity implements BeaconConsumer, MonitorNotifier { | |
protected static final String TAG = "MonitoringActivity"; | |
private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1; | |
private BeaconManager mBeaconManager; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
// Android M Permission check | |
if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION); | |
} else { | |
Log.d(TAG, "Location permission already granted."); | |
} | |
} | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
mBeaconManager = BeaconManager.getInstanceForApplication(this.getApplicationContext()); | |
mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.ALTBEACON_LAYOUT)); | |
mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT)); | |
mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT)); | |
mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT)); | |
mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.URI_BEACON_LAYOUT)); | |
mBeaconManager.setDebug(true); | |
mBeaconManager.bind(this); | |
} | |
@Override | |
public void onBeaconServiceConnect() { | |
Region region = new Region("all-beacons-region", null, null, null); | |
mBeaconManager.addMonitorNotifier(this); | |
try { | |
mBeaconManager.startMonitoringBeaconsInRegion(region); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
} | |
mBeaconManager.addRangeNotifier(new RangeNotifier() { | |
@Override | |
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { | |
Log.d(TAG, beacons.toString()); | |
if (beacons.size() > 0) { | |
Log.i(TAG, "The first beacon I see is about "+beacons.iterator().next().getDistance()+" meters away."); | |
} | |
} | |
}); | |
try { | |
mBeaconManager.startRangingBeaconsInRegion(region); | |
} catch (RemoteException e) { } | |
} | |
public void didEnterRegion(Region region) { | |
Log.d(TAG, "I detected a beacon in the region with namespace id " + region.getId1() + | |
" and instance id: " + region.getId2() + "; unique id: " + region.getUniqueId()); | |
} | |
public void didExitRegion(Region region) { | |
Log.d(TAG, "Exited region?: " + region.getUniqueId() + " - " + region); | |
} | |
public void didDetermineStateForRegion(int state, Region region) { | |
Log.d(TAG, "Something something: " + state + " : " + region); | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
mBeaconManager.unbind(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; | |
} | |
} | |
} | |
} |
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.tinej.iota3"> | |
<uses-permission android:name="android.permission.BLUETOOTH"/> | |
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> | |
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> | |
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment