Piggate blog

iBeacon and Physical Web tutorials. Stay tuned for more news!

Adding authentication and iBeacon offers to your Android app

In this entry the goal will be to create a simple application using the requests of the Piggate SDK for Android to register new users to our platform or login if there is an account already created. For this entry we need to use the Piggate Dashboard to create our application and add the group offers to our Android application.

Once all of the above is done and when we have set the Application ID we will have our Android app linked to our Dashboard and when a beacon is discovered, it will show the offers.


Creating your iBeacon

First of all, go to the Piggate dashboard and in the iBeacons sections create many iBeacon as you want to use. The fields that we need are the beacon name, the type (in this case iBeacon), the major and the minor, and place them in the map:

Creating the iBeacons


Creating the campaign

Go to the Campaign section and create the campaign which will contain all the offers. You have to set the name and description fields, a notification message and the secret and publishable key for your stripe account integration (this will be explained in another post):

Creating the campaign


Creating the offers

Go to the content section and create a new offer by clicking in tickets and creating a new one. First of all set the name, description, URL for the image, price and related campaign:

Creating an offer

After this, set the unlock code, currency, and the secret and publishable keys set before:

Creating an offer

Now place the store which will apear like a green marker and activate your iBeacon by clicking it:

Creating an offer

If you want, create more offer tickets to show with your iBeacons.


Creating the application in the Piggate Dashboard

In the Apps section create a new application, like in the following image, setting the name, description, icon image URL and campaign:

Creating new app


Edit the app created before and copy the Application ID, that will appear in the top like in the following capture:

Getting the application ID

Copy this ID and in the AndroidManifest.xml of your Android application add the following lines:

Adding the application ID

With all the steps finished the application and the dashboard are linked and we can do the login and register requests, so we can get the offers to show them with our iBeacons.


Create the login and register screens

Once all of the above is done, we only have to create two activities, one for the login and another for the register, with two fields: the email and the password.

Login screen

When the EditText fields and the button are done, we are ready to set the onClick listener of the button:

1
2
3
login.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) { }

Inside this onClick method we need to get the email and password, put them into a RequestParams object:

1
2
3
RequestParams params = new RequestParams();
params.put("email",email);
params.put("password",pass);

Then, with the Piggate objet, set the login request, called RequestOpenSession. Inside the listener we have to override the onComplete and onError methods for JSONObject and JSONArray (Activity_SingIn is the activity where we do the login request, and Activity_logged is the next activity):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//Request of the Piggate object. Handles the login into the application with the user email and password
_piggate.RequestOpenSession(params).setListenerRequest(new Piggate.PiggateCallBack() {
//Method onComplete for JSONObject
//When the request is correct start Activity_Logged activity
@Override
public void onComplete(int statusCode, Header[] headers, String msg, JSONObject data) {
loadingDialog.dismiss();
Intent slideactivity = new Intent(Activity_SingIn.this, Activity_Logged.class);
slideactivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Bundle bndlanimation = ActivityOptions.makeCustomAnimation(getApplicationContext(), R.anim.slidefromright, R.anim.slidetoleft).toBundle();
startActivity(slideactivity, bndlanimation);
}
//Method onError for JSONObject
//If there's an error with the request displays the error message to the user
@Override
public void onError(int statusCode, Header[] headers, String msg, JSONObject data) {
loadingDialog.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {
errorDialog.show();
}
});
}
//Method onComplete for JSONArray
@Override
public void onComplete(int statusCode, Header[] headers, String msg, JSONArray data) {
//Unused
}
//Method onError for JSONArray
@Override
public void onError(int statusCode, Header[] headers, String msg, JSONArray data) {
//Unused
}
}).exec();
Register screen

The register screen works exactly as the login screen, using the RequestNewUser instead of RequestOpenSession request:

1
piggate.RequestNewUser(params).setListenerRequest(new Piggate.PiggateCallBack() { [...] }).exec();

Create the logged activity and get the offers

In this screen we can have a button to do a logout request like the login and register buttons. To use this request we can call the following Piggate request, which have the same methods as the above ones:

1
piggate.RequestCloseSession().setListenerRequest(new Piggate.PiggateCallBack() { [...] }).exec()

On the other hand, it’s necessary to get the list of iBeacons whose offers we need to get with two simple functions:

1
2
piggate.refreshOffers();
ArrayList<PiggateBeacon> beacons = piggate.getOffers();

With all of this done, the user can register and login into the application and get the nearby offers to their corresponding iBeacons and show as he wants.


If you don’t want to code…

We have a complete example (called Piggate Offers Demo) which allows you to see the nearby offers, buy them with Stripe and exchange them, with a fancy looking interface. You can download the examples in our GitHub page and see what can be done with our tools.

To use the complete example you only have to create your application with campaign and offers, add your own iBeacons to the Piggate dashboard and activate them. Now get your Application ID and Stripe publishable key and put them in the AndroidManifest.xml of the Android application. Once you do this steps the application will works with your created offers and iBeacons.

Enjoy our tools and feel free to write a comment and send us feedback!

Comments

Proudly published with Hexo