Quantcast
Channel: Yudiz Solutions Ltd.
Viewing all articles
Browse latest Browse all 595

Beacon in Android

$
0
0

Overview

A beacon is a small Bluetooth radio transmitter. It simply broadcasts radio signals that are made up of a combination of letters and numbers transmitted on a regular interval of approximately 1/10 of a second. A beacon uses BLE technology

beacon-image-1

What is BLE?

Bluetooth Low Energy (BLE) also called Bluetooth Smart is a wireless personal area network.The BLE design by Bluetooth SIG. The key difference between classic bluetooth and BLE are :

Classic Bluetooth VS BLE

How beacon works?

A beacon simply broadcasts radio signals at every regular interval of time. It’s like a lighthouse instead of emitting visible light, it broadcasts radio signals.

As per image shown beacon simply transmit a radio signals, once device is in its range. The device will be notified, then we can handle this event as per our requirements.

Components of a beacon

  1. Tiny computer with bluetooth smart connectivity.
  2. Battery
  3. Firmware telling the beacon what it should do exactly.
  4. Sensors

What beacon packet contains?

A beacon generally contains three of fields that are

  1. Beacon unique identifiers
  2. Minor
  3. Major

Let’s take small example to understand what is this, Suppose company X that is located in a city and has many of its branches and each branch contains many departments.
Here,
Beacon unique identifier is equal to company X
Major can be compared to a branch
And minor is equal to a department.

Now when your device gets into beacon’s range it will throw information such as company X is connected with Y branch and Z department.

Types of beacons

There are following type of beacons available in market

  1. iBeacon
  2. Eddystone
  3. AltBeacon, and many more.

1. iBeacon :

Beacon format was introduced in December, 2013. Devices implementing iBeacon protocol can only send one type of signal name UUID.

2. Eddystone :

A new beacon format by google introduced in July, 2015. It is open source and is
available in Github. Devices implementing Eddystone protocol can send three types of signals:

  1. UID
  2. URL
  3. TLM

Example

Here is a code snippet of how to scan a beacon in android. Here, I’m using estimote android SDK to scan beacon.

Add dependencies

implementation 'com.estimote:sdk:1.4.1'

Initialize sdk, if you don’t have appId and appToken than pass it blank or null. Create beaconManager object that will help us to connect with beacon. As shown in below code

EstimoteSDK.initialize(context, appId, appToken);
EstimoteSDK.enableDebugLogging(true);
beaconManager = new BeaconManager(this);
beaconManager.setBackgroundScanPeriod(1000, 1000);
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
   @Override
   public void onServiceReady() {
       Log.d(TAG, "onServiceReady: ");
   }
});
beaconManager.setErrorListener(new BeaconManager.ErrorListener() {
   @Override
   public void onError(Integer errorId) {
       Log.e(TAG, "onError: " + errorId);
   }
});

Now, if you want to scan specific beacon,you need to create a beacon region, in this beacon region you need to pass region name, its UUID, major and minor to its constructor.

beaconManager = ((MyApp) getApplicationContext()).beaconManager;
beaconRegion = new BeaconRegion("monitored region",
       UUID.fromString("B9407F30-F5F8-466E-AFF9-25556B57FE6D"),
       "major", "minor");
beaconManager.setMonitoringListener(new BeaconManager.BeaconMonitoringListener() {
   @Override
   public void onEnteredRegion(BeaconRegion region, List<Beacon> beacons) {
       beaconAdapter.addItems(beacons);
       Log.d(TAG, "onEnteredRegion: ");
   }

   @Override
   public void onExitedRegion(BeaconRegion region) {
       Toast.makeText(MainActivity.this, "onExitedRegion", Toast.LENGTH_SHORT).show();
       Log.d(TAG, "onExitedRegion: " + region);
   }
});

To start and stop specific beacon region.

beaconManager.startMonitoring(beaconRegion);

beaconManager.stopMonitoring(beaconRegion.getIdentifier());

Using beaconManager object, set beacon monitoring listeners. In its listener, there are two overridden methods, onEnteredRegion gives us all nearby scanned beacon list. And another is onExitedRegion which gives us beacon region from where device exists.

@Override
protected void onDestroy() {
   if (beaconManager != null)
       beaconManager.disconnect();
     super.onDestroy();
}

Finally when you don’t want to scan any more beacon you need to disconnect using beacon manager object.

Yudiz portfolio

Here, at Yudiz, we have developed apps wherein beacons are used at restaurants and when user’s device gets into the it range, he gets awarded with offers.

Conclusion

Beacon’s future is bright and one can imagine a wide range of possible applications targeting this technology.


Viewing all articles
Browse latest Browse all 595

Trending Articles