Android Beacon Implementation – Step By Step Guide For Developers

commas-left

"In today’s world of IT, people playing with trending technologies encash more rather than people who prefer going into a competitive idea. Let’s go through the trending technology which has much for Apps world to get fruits from."

commas-right

beacons androud app

1) 2)
3) 4)
5) 6)
7)  

What Are Beacons?

Beacons are the tiny devices that allow transmission of data in the form of Bluetooth frames. Beacons have limited information to transfer hence we call it lightweight devices. In this guide, we will take you through the concept & process of detecting beacons in Android App.

Beacons have a battery usually lasts from 1 to 2 years. For the range, you can expect within 1 meter very accurate & above 1 meter less accurate.

How Beacons Can Help?

If you are in search of some ideas to create apps then this article worth reading for exploring the possibilities with beacons and smartphones.

In a layman term, beacon emits the signal when a person with smartphone passes through it. With this signal you can display any data based on your business model.

For example, in a mall, you want to show new arrivals when a user passes through jeans rack. Yes!

you can achieve this with beacons only.

Which Are the SmartPhones Beacons Support?

What are the Appropriate Places of Beacon Application?

  • Indoor navigation in Universities or Malls: People love being guided wherever they go and in big campus they would prefer the smartphone guide them and bring out all point of interests.
  • Roadside advertising for small business & restaurants: When we travel in some city then we definitely not aware of local famous shops or food courts this is the other future Application of beacon.

Now, lets move on beacons technical implementation. Whether or not you are a mobile developer, this article is intended for all of you who are looking for an out-of-the-box concept.

Technical Overview

Beacons broadcast tiny packets of data, containing their Beacon ID and information about signal strength, so that the phone can understand which beacon it hears and how far it is.

Basically beacons include UUIDs which helps detecting it in Android application, not only this major and minor values allows us to detect particular beacon.

Every Beacon ID is 20 bytes long and is divided into three sections as given below:

  • UUID of 16 bytes
  • Device Major (2 bytes)
  • Device Minor (2 bytes)

Minimum Device Configuration Required

  • Android Version : 5.0.1 & Above
  • BLE version : 4.1 and above

Currently, we can see Nexus 5x, Nexus 6, Nexus 9 are the device having perfectly running BLE feature.

Note: We cannot test this app in any emulators as of now.

Step 1 – Editing Manifest

Like any permissions in android app we have to set permission for Bluetooth in our project manifest:

1
2
<uses-permission android:name=''android.permission.BLUETOOTH''/>
<uses-permission android:name=''android.permission.BLUETOOTH_ADMIN''/>

If you want to declare that your app is available to BLE-enabled handsets only then just include the following in the project manifest:

1
2
<uses-feature android:name=''android.hardware.bluetooth_le''
android:required=''true''/>

Now, we have to initialize a bluetoothAdapter Object before we start working with the BLE

1
2
3
4
5
6
7
8
9
// Initializes Bluetooth adapter.
final BluetoothManager bluetoothManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_your_activity);
  bluetoothManager =(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);    
  mBluetoothAdapter = bluetoothManager.getAdapter();
}

Step 2 – Instantiate

For instantiating, allocate a bluetooth scanner object:

1
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();

Step 3 – Scan Settings

Go to onCreate method and set Scan Settings & Filters:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private void setScanFilter() {
  ScanFilter.Builder mBuilder = new ScanFilter.Builder();
  ByteBuffer mManufacturerData = ByteBuffer.allocate(23);
  ByteBuffer mManufacturerDataMask = ByteBuffer.allocate(24);
  //Below to set uuid of beacon
  byte[] uuid = getIdAsByte(UUID.fromString(''0CF052C297CA407C84F8B62AAC4E9020'');
  mManufacturerData.put(0, (byte)0xBE);
  mManufacturerData.put(1, (byte)0xAC);
  for (int i=2; i<=17; i++) {
    mManufacturerData.put(i, uuid[i-2]);
  }  for (int i=0; i<=17; i++) {
    mManufacturerDataMask.put((byte)0x01);
  }
  mBuilder.setManufacturerData(224, mManufacturerData.array(),
mManufacturerDataMask.array());
  mScanFilter = mBuilder.build();
}

Now we are creating manufacturer array.

1
2
3
4
5
6
private void setScanSettings() {
  ScanSettings.Builder mBuilder = new ScanSettings.Builder();
  mBuilder.setReportDelay(0);
  mBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER);
  mScanSettings = mBuilder.build();
}

Now we can start the scanner.

1
2
mBluetoothLeScanner.startScan(Arrays.asList(mScanFilter), mScanSettings,
mScanCallback);

Now we require to override to get the callback for scan callback.

Step 4: Define ScanCallBack function

In above callback, we fetch the settings along with received signal from getScanRecord() method.

1
2
3
4
5
6
7
8
protected ScanCallback mScanCallback = new ScanCallback() {
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        ScanRecord mScanRecord = result.getScanRecord();
        int[] manufacturerData = mScanRecord.getManufacturerSpecificData(224);
        int mRssi = result.getRssi();
    }
}

Here we are ready to detect the distance and most of the data we require. We are using AltBeacon’s algorithm.

1
2
3
4
5
6
7
8
9
10
11
12
13
public double calculateDistance(int txPower, double rssi) {
  if (rssi == 0) {
    return -1.0; // if we cannot determine accuracy, return -1.
  }
  double ratio = rssi*1.0/txPower;
  if (ratio < 1.0) {
    return Math.pow(ratio,10);
  }
  else {
    double accuracy =  (0.89976)*Math.pow(ratio,7.7095) + 0.111;
    return accuracy;
  }
}

How about detecting the distance of beacon whether it’s far, near or immediate. Lets focus on below method to getDistance();

1
2
3
4
5
6
7
8
9
10
private String getDistance(accuracy) {
  if (accuracy <= -1.0) {
    return ''Unknown'';
  } else if (accuracy < 1) {
    return ''Immediate'';
  } else if (accuracy < 3) {
    return ''Near'';
  } else {
    return ''Far'';  }
}

AND THAT’S IT! YOU HAVE EVERYTHING YOU NEED TO DETECT YOUR BEACON!

Note: We can easily develop the BLE sample using the third party library.

Reference: https://altbeacon.github.io/android-beacon-library/

Conclusion

if you are interested to develop mobile app for deacons or anything in your mind related to iOS or android app development, Please share your detail with us for further conversation.

Contact Us

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA

*