iLocky Android SDK Usage Manual

This article will teach you how to integrate our iLocky SDK into your app.

update message:

  1. v1.0:  SDK supports foreground mode usage in this version. The developer can bypass background code if foreground mode is only considered in your project.

 

Import iLocky.aar into your project

  • File->New-> New Module -> import .JAR/AAR Package, and select iLocky-sdk.aar
  •  add iLocky-SDK, animation sdk, and mixpanel sdk dependencies in app gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'

compile 'com.mixpanel.android:mixpanel-android:4.6.3'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.daimajia.easing:library:1.0.1@aar'
compile 'com.daimajia.androidanimations:library:1.1.3@aar'
compile project(':ilocky-sdk')
}

In AndroidManifest.xml (Foreground)

use SDK in foreground mode:

  • uses-permission
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.CAMERA"/>
  • add iLocky acrtivity
<activity android:name="com.ilocky.ILockyActivity" 
android:theme="@style/Theme.Transparent"
android:launchMode= "singleTop">

 

In AndroidManifest.xml (Background)

if background access is needed, please add additional code in AndroidManifest.xml :

  • uses-permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  • add iLocky receiver
<receiver android:name="com.ilocky.ILockyReceiver" android:exported="true" android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        <action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
        <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>
  • add iLocky service
<service android:name="com.ilocky.ILockyService"/>

MainActivity (Foreground mode)

  • initialize iLocky SDK
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ILockyPassport.initialize(this); //initialize the iLocky Passport(Keys)
  • set language(optional)

iLocky SDK will automatically detect OS language and display the corresponding message in foreground dialog. The developer can manually change the displayed language by using following code setting*.

ILocky.setLocale(Locale.SIMPLIFIED_CHINESE);
ILocky.setLocale(Locale.TRADITIONAL_CHINESE);
ILocky.setLocale(Locale.ENGLISH);

Only three languages, English, Traditional Chinese, and Simplified Chinese, are supported. If another language is setting. SDK will use English.

* caution: change the iLocky SDK language will also affect your app language. We suggest developer to directly modify your app default language.

  • start ILockyActivity when user intend to access iLocky Device
startActivityForResult(new Intent(MainActivity.this, ILockyActivity.class),0);

you should see the dialog in your app when start iLocky activity:

Screenshot_2016-01-05-23-15-28.png

developer can get the status if user successfully accesses iLocky device through

ILocky.isForegroundAccessSuccess()

MainActivity (Background mode)

  • initialize iLocky SDK
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ILockyPassport.initialize(this); //initialize the iLocky Passport(Keys)
startService(new Intent(getBaseContext(), ILockyService.class));
doBindService();
  • doBindService() and doUnBindService()
ILockyService myService=null;
boolean bound=false;
void doBindService() {
boolean bound = bindService( new Intent( this, ILockyService.class ), serviceConnection, Context.BIND_AUTO_CREATE );
if ( bound ) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
myService.setDoInBackground(true); // the iLocky service will run in background during the app open.
myService.setBackgroundSound(R.raw.open); //set background open door sound(raw resource sound id)
myService.setNotCloseEnoughVibrateAndSound(true,R.raw.notclose);//set not close enough vibrate and sound (true/false for vibrate, not close engough of raw resource sound id)
}
}, 1000); // not run immdeiately , delay 1 sec to set this service.
}
else {
Log.d( TAG, "Failed to bind service" );
}
}
//doUnbindService
void doUnbindService() {
unbindService(serviceConnection);
}
//Service Connection
private final ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceConnected( ComponentName className, IBinder service ) {
myService = ( (ILockyService.ServiceBinder) service ).getService();
bound = true;
}
public void onServiceDisconnected( ComponentName className ) {
myService = null;
bound = false;
}
};
  • onResume(), onPause(), and onDestroy() in MainActivity
@Override
protected void onResume() {
super.onResume();
SharedPreferences prefs = getSharedPreferences("iLocky", Context.MODE_PRIVATE);
doInBackground=prefs.getBoolean(ILockyService.PREFS_DO_IN_BACKGROUND,true);
if(myService!=null)
myService.setDoInBackground(true);
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences prefs = getSharedPreferences("iLocky", Context.MODE_PRIVATE);
prefs.edit().putBoolean(ILockyService.PREFS_DO_IN_BACKGROUND,doInBackground).apply();
if(myService!=null) {
myService.setDoInBackground(doInBackground); // will keep doinbackground setting in service
}
}
@Override
protected void onDestroy() {
super.onDestroy();
doUnbindService();
}

Get smart phone’s UUID

when request passport from global server, you will need to know the uuid of user’s smart phone. you can get it from the following code:
ILockyPassport.getDeviceUuid()
it will return the uuid string. you can send the uuid string to your local server for the requesting passport usage from global server.

Import iLocky passport(key)

you can simply import the passport(key) into this app by
try {
ILockyPassport.importPassport(p);
} catch (ILockyPassport.ImportPassportException e) {
e.printStackTrace();
}
p is the String format, the local server can directly pass the passport file from global server to this method.
there are iLocky_id, a duration(start and end time), times limit in the the passport(key). You only need to import into our sdk. don’t need to do key setting in app.
you don’t need to handle any bluetooth handshaking things, the iLocky service will do it for you.

iLocky Event callback(In Background Mode)

You can do the extra tasks when passport(key) is successfully used, is failure to use(BLE connection error), or not close enough between iLocky device and smart phone. Set the event callback for ILockyService, e.g.
myService.setILockyEventCallback(new ILocky.ILockyEventCallback() {
@Override
public void onDeviceUnauthorized() {

}
@Override
public void onPassportUsed(final ILockyPassport iLockyPassport) {

}

@Override
public void onILockyAccessSuccess(ILockyPassport iLockyPassport) {
}

@Override
public void onILockyAccessFail(ILockyPassport iLockyPassport) {
}

@Override
public void onNotCloseEnough() {
}
});

Check Bluetooth status

check bluetooth is turned on or not, if not, ask user to turn on bluetooth. you can add the following code in onResume() to make sure user’s phone BT is turned on when running this app.
bluetoothAdapter=bluetoothManager.getAdapter();
if(!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableBtIntent);
}

Proguard

if use Proguard, please add keep options in proguard-rules.pro
-keep class com.mixpanel.android.abtesting.** { *; }
-keep class com.mixpanel.android.mpmetrics.** { *; }
-keep class com.mixpanel.android.surveys.** { *; }
-keep class com.mixpanel.android.util.** { *; }
-keep class com.mixpanel.android.java_websocket.** { *; }
-keep class com.daimajia.easing.** { *; }
-keep interface com.daimajia.easing.** { *; }

-keep class com.facebook.** { *; }
-keepattributes Signature
-dontwarn org.apache.http.**
-dontwarn com.google.android.gms.**
-keepattributes InnerClasses

-keep class **.R
-keep class **.R$* {
<fields>;
}

3 thoughts on “iLocky Android SDK Usage Manual”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s