Select Page

My previous post explains basics of the beacon. We are going to see how to transmit as a beacon using your application.

AndroidBeaconLibrary is an open source project providing APIs to interact with beacons. Using this library we can easily create an app act as a beacon transmitter.

You can only start this when all following requirements are met:

* BLE hardware
* Bluetooth on
* Location on (Android 6+)
* Location runtime permission granted (Android 6+)

1. Basic Setup:

Configure your app’s build.gradle File. Set the minSDK value as 21 and add the following dependency in your dependencies list.


compile 'org.altbeacon:android-beacon-library:2.9.2'


2. Add permissions to the manifest:

Now you need to add the permission declarations in the manifest file.

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION" />
3. Checking Bluetooth connection:

First, you need to check the Bluetooth connection of your device. For this, we are using the util class called BluetoothHelperand also implements the needed interface they provide the relevant callback function based on the Bluetooth connection status.


public class BeaconTransmitterActivity extends AppCompatActivity implements
BluetoothListener.OnBluetoothSupportedCheckListener,
BluetoothListener.OnBluetoothEnabledCheckListener,
BluetoothListener.BluetoothTrigger
{

BluetoothHelper bluetoothHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {

bluetoothHelper = new BluetoothHelper();
bluetoothHelper.initializeBluetooth(this);

}

}

4. Initiate the BeaconManager:

To initiate the beacon manager and reduce the power consumption of the app, you need to add the following set of code into your application class.


public class MyApplication extends Application {

BackgroundPowerSaver backgroundPowerSaver;
BeaconManager beaconManager;

@Override
public void onCreate() {
super.onCreate();
beaconManager = BeaconManager.getInstanceForApplication(this);
// enables auto battery saving of about 60%
backgroundPowerSaver = new BackgroundPowerSaver(this);
}

}

5. Transmitting as a Beacon:

For beacon transmission, we need UUID, major and minor values. So you need to enter all these inputs via input field or hard-coded values. Now we can transmit as a beacon using the following snippet.


String major, minor, uuid;

uuid = etUUID.getText().toString().trim();
major = etMajorValue.getText().toString().trim();
minor = etMinorValue.getText().toString().trim();

beacon = new Beacon.Builder().setId1(uuid).setId2(major).setId3(minor)
.setManufacturer(0x0118)
.setTxPower(-59)
.setDataFields(Arrays.asList(new Long[]{6l, 7l})) .build();

// Change the layout below for other beacon types

beaconParser = new BeaconParser()
.setBeaconLayout(parserLayout[beaconLayout]);

beaconTransmitter = new BeaconTransmitter(getApplicationContext(), beaconParser);
beaconTransmitter.startAdvertising(beacon, new AdvertiseCallback() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
}

@Override
public void onStartFailure(int errorCode) {
super.onStartFailure(errorCode);
}
});

6. Changing the beacon format:

We can easily change the beacon format by setting the beacon layout. For changing the beacon format you can use the following code and here is the list of the main beacon layouts you can use with the android beacon library.


beaconParser = new BeaconParser().setBeaconLayout("##FORMAT##");

// Replace the ##FORMAT## for the corrosponding beacon types

ALTBEACON  m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
IBEACON       m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24
EDDYSTONE TLM  x,s:0-1=feaa,m:2-2=20,d:3-3,d:4-5,d:6-7,d:8-11,d:12-15
EDDYSTONE UID  s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19
EDDYSTONE URL s:0-1=feaa,m:2-2=10,p:3-3:-41,i:4-20v

I created a project on GitHub to demonstrate the things covered in this article.

View on GithubDownload .zip

Feel free to comment and share, keep watching this space get more updates on Android Stuff!

Elsewhere

Jaison Fernando

Android Developer at NFN Labs
Elsewhere