At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

How to Use Beacon in Android Application

  • By Gowtham S
  • January 23, 2018
  • 1406 Views

Beacon, a technology that uncovers incredible features to customers through Android Development. Here learn to implement Beacon in Android Application. Engage your customers with detailed information along with better navigations.

What is Beacon?

Beacon a small physical device that is used to broadcast the wireless signals to nearby Smartphone’s or tablets.

What will these beacons do?

Beacons will enable the Bluetooth devices to broadcast or receive tiny, static piece of data by constantly sending out the radio signals to the nearby mobile phones.
in-store-beacon-technology

To connect this beacon’s, we must need SDK so what is SDK?

SDK is a software development kit, a part of Android Studio that consists a set of software development tools which enables the developers to build and compile Android applications.
This Android SDK includes required libraries, Debugger, An emulator, library, source code, development tools to build Android applications.
This Android Studio must have Java Development Kit (JDK) installed with it and the recommended Android Development Tools (ADT) plug-in.
Most of these IDEs provide a graphical interface that enables developers to perform development tasks faster. However, other IDEs, such as NetBeans or IntelliJ, will also work in the same way.
Note: Estimote provides the SDK for connecting beacon & sticker and accessing the features. The SDK system works on Android 4.3 or above and requires a device with Bluetooth Low Energy (Estimote SDK’s minimum Android SDK version is 9).

Create Your app in Estimate

 

Why do we need to use Estimote?

To connect this beacons & stickers and to access the features we must need SDK and this SDK will be provided by Estimote cloud.
Here you can find the step by step process to create your app.

Step 1: To create your app in Estimote you must sign up first.

Once signed in you can see the dashboard as shown in below
Where you can see the beacons list, Registered Apps, locations, Analytics, etc.
Output Screenshot

Step 2: Create and register your new App

Click on “AddNewApp” option as shown in the below screen.
Output Screenshot

Step 3: Provide your App info

After clicking on it, you will be asked to provide your app info
Output Screenshot
Note: In “Associated Beacons” Based on your app specification choose your beacon.
After creating your App, you could see your App info as shown in below screen.
Output Screenshot
Cool! We had successfully created our App in Estimote.
And now it’s time to create your project in Android Studio

How to create your project in Android Studio

If you are new to Android Studio and you don’t have an idea of creating your project on it. Well, we made it easy for you. In our previous blog, we gave a step by step procedure to create a new project in Android Studio
Click here to know: https://www.agiratech.co.uk/kotlin-in-android-development/
After creating a project, we must initialize “Estimote SDK” by doing the below steps in MainActivity file.

Step 1: Add SDK in your project

To add Estimote SDK in your project, find build.gradle file in your App folder.
In build.gradle file you should add your SDK name in below-mentioned code.

dependencies {
compile 'com.estimote:sdk:0.11.1@aar'
}

 

Step 2: Initialize SDK

Now go to main activity file
To Initialize Estimote SDK, we should give a global declaration in onCreate() method
You can find the “oncreate method” in MainActivity file.
Declare it like the below way

private BeaconManager beaconManager;
private EstimoteCloud estimoteCloud;
private static final Region ALL_ESTIMOTE_BEACONS_REGION =
new Region("rid", null, null, null);

 

Step 3: Create a Beacon Manager object to add your app id and app token

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
beaconManager = new BeaconManager(this); object creation
// To get your AppId and AppToken, you need to create new
an app in Estimote Cloud.
EstimoteSDK.initialize(applicationContext, appId, appToken);
//Optional, debug logging.
EstimoteSDK.enableDebugLogging(true);
}

 

Step 4: Connect the Beacon Manager to the beacon scanning service maintained by the Estimote SDK.

@Override
protected void onResume() {
super.onResume();
SystemRequirementsChecker.checkWithDefaultDialogs(this); // to enable bluetooth
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS_REGION);        }
});
}

 

Step 5: Set ranging listeners

To set this ranging listener, we should call the “setranginglistener” method. By typing “setranginglistener” you will automatically get the recommended function suggestions and you can choose it accordingly.

beaconManager.setRangingListener(new BeaconManager.RangingListener() {
@Override
public void onBeaconsDiscovered(Region region, List<Beacon> beacons) {
if (beacons.size() != 0) {
Beacon beacon = beacons.get(0);
}
}
});

 
Note: Here the output will be in an array format.
Get (0) will get the details of the first beacon.
Once you were done with all the above steps, run your project. Now you will get the output which holds all the details of Beacon.
Output Screenshot
Note: Beacon name will not be displayed on the output screen. In case if you like to view or identify the beacon name you can follow the below procedure.

Procedure to get beacon name

To find the beacon name developer need to pass two type of input as follows :

  • If we are using proximity beacon, we must use fetchBeaconDetails methods and in case if we are using location beacon then we must use fetchDeviceDetails.
  • A user has to send beaconUUID and Major id. By using a callback method, so internet is mandatory to get back the details.
estimoteCloud.fetchBeaconDetails(beacon.getProximityUUID(),beacon.getMajor(),
nearestBeacon.getMinor(), new CloudCallback<BeaconInfo>() {
@Override
public void success(BeaconInfo beaconInfo) {
String beaconname = beaconInfo.name; // name will get stored
}
}
@Override
public void failure(EstimoteServerException e) {
Log.d("mmmmmmmmm", e.getMessage());
}
});

 

Output

Output Screenshot
On other hands, if you want to start or stop the service you can follow the below procedure.

To Start the Service

If you like to start the service you can do the below process in onResume method.

@Override
protected void onResume() {
super.onResume();
SystemRequirementsChecker.checkWithDefaultDialogs(this); // to enable bluetooth
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS_REGION);        }
});
}

 

To Stop The Service

If you need to stop this service you have to use stop scanning process inside of onPause method.

@Override
protected void onPause() {
beaconManager.stopRanging(ALL_ESTIMOTE_BEACONS_REGION);
super.onPause();
}

 

Source Code

package agira.com.mybeacon;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatTextView;
import android.util.Log;
import android.widget.TextView;
import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.EstimoteSDK;
import com.estimote.sdk.Region;
import com.estimote.sdk.SystemRequirementsChecker;
import com.estimote.sdk.cloud.CloudCallback;
import com.estimote.sdk.cloud.model.BeaconInfo;
import com.estimote.sdk.exception.EstimoteServerException;
import com.estimote.sdk.cloud.EstimoteCloud;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final Region ALL_ESTIMOTE_BEACONS_REGION =
new Region("rid", null, null, null);
private BeaconManager beaconManager;
private EstimoteCloud estimoteCloud;
private AppCompatTextView appCompatTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appCompatTextView = (AppCompatTextView) findViewById(R.id.txt);
beaconManager = new BeaconManager(this); - object initiate
EstimoteSDK.initialize(this,"****-tech-llc-s-****-mwp", "8836b7****bf2481d2da******"); -  config app id nd app token
estimoteCloud = EstimoteCloud.getInstance(); estimote object creation
Ranging listeners are set to  find the signal of beacons
-  beaconManager.setRangingListener(new BeaconManager.RangingListener() {
@Override
public void onBeaconsDiscovered(Region region, final List<Beacon> list) {
try {
Log.d("oooooooooo", "" + list.size());
if (!list.isEmpty()) {
Beacon nearestBeacon = list.get(0);
estimoteCloud.fetchBeaconDetails(nearestBeacon.getProximityUUID(), nearestBeacon.getMajor(),
nearestBeacon.getMinor(), new CloudCallback<BeaconInfo>() {
@Override
public void success(BeaconInfo beaconInfo) {
String patientname = beaconInfo.name;
appCompatTextView.setText(patientname);
}
@Override
public void failure(EstimoteServerException e) {
Log.d("mmmmmmmmm", e.getMessage());
}
});
}
} catch (Exception d) {
d.getMessage();
}
}
});
}
@Override
protected void onResume() {
super.onResume();
SystemRequirementsChecker.checkWithDefaultDialogs(this);
startlistening();
}
private void startlistening() {
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
//Toast.makeText(getApplicationContext(), "Staring", Toast.LENGTH_SHORT).show();
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS_REGION);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.disconnect();
BM.enable();
}
}

 

Conclusion:

While bringing various levels of advancements in Mobile technology, Beacons brings an effective way of carrying the piece of information to the customers. It continuously acknowledges the customers with the required information for the better navigations. By implementing these Beacons with mobile technology we could make the bright path for successful businesses. Aside from big companies, now the start-ups are playing the significant role in identifying the future of beacons.