-1

I am developing application for monitoring and configuring beacon devices. I need to monitor both iBeacon and Eddystone beacon devices. I have go through the nRF Master control application. Its working perfectly. But I need a source code for that. Is any other option available. Kindly get me the best solution for analysing beacon devices. Thanks in advance.

Arunraj Jeyaraj
  • 639
  • 2
  • 11
  • 18

1 Answers1

0

I would suggest you to use AltBeacon library, which I have used in one of my projects and it's pretty good (I am not associated with it in any way). It provides APIs to interact with beacons.

Here is a sample Activity to get you started:

public class MyActivity extends AppCompatActivity implements
    BeaconConsumer,
    BootstrapNotifier,
    RangeNotifier
{
    private RegionBootstrap                    mRegionBootstrap;
    private org.altbeacon.beacon.BeaconManager mAltBeaconManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_application);

        mAltBeaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(MyActivity.this);

        boolean isBleAvailableAndEnabled;
        try {
            isBleAvailableAndEnabled = mAltBeaconManager.checkAvailability();
        } catch (BleNotAvailableException ex) {
            isBleAvailableAndEnabled = false;
        }

        if (!isBleAvailableAndEnabled) {
            // Handle case ...
            finish();
        }

        // Disable Android L scanning on devices with Android 5.0 and above
        if (Build.VERSION.SDK_INT >= 21) mAltBeaconManager.setAndroidLScanningDisabled(true);

        // Add iBeacon Layout
        mAltBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));

        // Add Eddystone Layout
        mAltBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("Eddystone_layout"));

        mAltBeaconManager.setBackgroundBetweenScanPeriod(3000); // 3 sec
        mAltBeaconManager.setBackgroundScanPeriod(5000); // 5 sec
        mAltBeaconManager.bind(MyActivity.this);

        // Enable Beacon scanning
        mRegionBootstrap = new RegionBootstrap(MyActivity.this, getScanningRegion());
    }

    @Override
    public Context getApplicationContext() {
        return (!isFinishing()) ? MyActivity.this : null);
    }

    @Override
    public void onBeaconServiceConnect() {
        try {
            // Attach beacon range listener
            mAltBeaconManager.setRangeNotifier(this);
            mAltBeaconManager.startRangingBeaconsInRegion(new Region("Region", null, null, null));
        } catch (RemoteException ex) {
            // Handle exception
        }
    }

    @Override
    public void unbindService(ServiceConnection serviceConnection) {
        // Not needed
    }

    @Override
    public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
        return false;
    }

    @Override
    public void didEnterRegion(Region region) {
        // Handle event
    }

    @Override
    public void didExitRegion(Region region) {
        // Handle event
    }

    @Override
    public void didDetermineStateForRegion(int i, Region region) {
        // Handle event
    }

    @Override
    public void didRangeBeaconsInRegion(Collection<org.altbeacon.beacon.Beacon> rangingBeacons, Region region) {
        // Here you will receive the beacons which are currently in range
    }
}

Add this to your Manifest:

<uses-sdk android:minSdkVersion="18" android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

Note: Also, pay attention to this library's limitations, which essentially are Android's software and hardware limitations regarding beacons support.

S. Brukhanda
  • 132
  • 10