Beacon SDK - Android
Guide for Android Beacon SDK
ZBeacon SDK
Requirement
minSdkVersion=14
but if sdkVersion < 19, it doesn't work.
Downloads
jitpack.io
Add jitpack.io to your build file's list of repositories.
Replace $[version] to jitpack version on top
Gradle
repositories {
maven { url "https://jitpack.io" }
}dependencies {
compile 'com.github.zoyi:sdk-ibeacon-android:$[version]'
}Maven
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories><dependency>
<groupId>com.github.zoyi</groupId>
<artifactId>sdk-ibeacon-android</artifactId>
<version>$[version]</version>
</dependency>SBT
resolvers += "jitpack" at "https://jitpack.io"libraryDependencies += "com.github.zoyi" % "sdk-ibeacon-android" % "$[version]"Start
You must create a class that extends Application and then declare this in your AndroidManifest.xml.
If you already use own Application class, you can use that.
It is important because booting event runs Application class.
You must call ZBeaconManager.init() once in
Application'sonCreate()method.
<application
android:name="com.example.MyApplicationName"
...
>
</application>Here is an example Application class.
public class MyApplicationName extends Application {
@Override
public void onCreate() {
super.onCreate();
ZBeaconManager.init(this, email, token, Target.PRODUCTION);
// *** Target: PRODUCTION | DEVELOPMENT ***
// *** if you want to send data to dev server, init with Target.DEVELOPMENT ***
// if you want to see logs.
// ZBeaconManager.setDebugMode(true);
// you must set customer id for send signal.
String deviceId = Settings.Secure.getString(
getApplicationContext().getContentResolver(),
Settings.Secure.ANDROID_ID);
String customerId = hmacDigest(deviceId + "YOUR_SALT", "YOUR_KEY_FOR_HMAC", "HmacSHA512");
if (customerId != null) {
Log.i("CustomerId", customerId);
ZBeaconManager.setCustomerId(customerId);
}
// You must start manually.
ZBeaconManager.start();
// And if you want to stop,
// ZBeaconManager.stop();
}
public static String hmacDigest(String msg, String keyString, String algo) {
String digest = null;
try {
SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), algo);
Mac mac = Mac.getInstance(algo);
mac.init(key);
byte[] bytes = mac.doFinal(msg.getBytes("ASCII"));
StringBuffer hash = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
hash.append('0');
}
hash.append(hex);
}
digest = hash.toString();
} catch (UnsupportedEncodingException e) {
} catch (InvalidKeyException e) {
} catch (NoSuchAlgorithmException e) {
}
return digest;
}
}Deployment Target
Note that you must describe deployment target when initializing.
The deployment target depends on ZOYI's actual O2O server endpoint.
This flag is only used for mutual test with ZOYI Corp.
So most of 3rd parties does not need to change this.
// Set target as PRODUCTION
ZBeaconManager.init(this, email, token, Target.PRODUCTION);
// Set target as DEVELOPMENT (signals can not be seen by BLE API)
ZBeaconManager.init(this, email, token, Target.DEVELOPMENT);
// You can set nothing for PRODUCTION
ZBeaconManager.init(this, email, token);Sample Code
Here is a sample code.
Requesting Permission
Android SDK 23+ (Marshmallow) must get ACCESS_COARSE_LOCATION permission to use this library.
You must add code like following in Activity if your app targets Android SDK 23+ (if you set targetSdkVersion 23)
If your android app targets Under Android SDK 23, you need not to write following code.
private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
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();
}
}
}
@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;
}
}
}
}Permission
We use following permissions. These are added automatically.
- android.permission.BLUETOOTH
- android.permission.BLUETOOTH_ADMIN
- android.permission.RECEIVE_BOOT_COMPLETED
- android.permission.ACCESS_COARSE_LOCATION
- android.permission.INTERNET
- android.permission.ACCESS_NETWORK_STATE
- android.permission.WAKE_LOCK
Updated 8 months ago
