-1

I'm a french student in ingineering and I am learning Android language by myself. My friends and I have to create an Android app which is based on iBeacon technology. I discovered the AltBeacon library few days ago and I found it awesome but I have some questions to ask on it.

Firstly, you must understand I am a novice in programming, and my questions will be idiots for you. But please I really need help ;)

Android provides a Bluetooth.LE Api and I understood that I can use the method startLeScan() to get a BluetoothDevice. But if I want to use the AltBeacon library which is the equivalent method who allow us to scan iBeacon devices and get an Beacon object?

Another question, If I use startLeScan() and get a BluetoothDevice, how can I transform it into Beacon in order to use AltBeacon methods ?

I am sorry for my english mistakes, I hope my questions will be understandable. Bye

3 Answers3

5

This is what we use to detect iBeacons and get a beacon object in a Android service using the AltBeacon lib.

Setup the BeaconManager

BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.setForegroundScanPeriod(5100);
beaconManager.setForegroundBetweenScanPeriod(2000);
beaconManager.setBackgroundScanPeriod(5100);
beaconManager.setBackgroundBetweenScanPeriod(2000);

//Parse IBeacon structure
beaconManager.getBeaconParsers().add(new BeaconParser().
                setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
beaconManager.bind(this);

Start Ranging the beacons

private void startBeaconRangeFinderService() {
        beaconManager.setRangeNotifier(new RangeNotifier() {

        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, org.altbeacon.beacon.Region region) {
            try {
                if (beacons.size() > 0) {
                    for (Beacon b : beacons) {
                        processYourBeaconInThisMethod(b);
                    }
                }
            } catch (Exception ex) {
                Log.e(TAG_BEACON_ACTIVITY, "Error was thrown: " + ex.getMessage());
            }
        }
    });
    try {
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
    } catch (RemoteException e) {
        Log.e(TAG_BEACON_ACTIVITY, "Error was thrown: " + e.getMessage());
    }
}
Hendri
  • 91
  • 1
  • 7
1

You can easily use the Android Beacon Library to scan for beacons and return results using the "Ranging APIs" as described here:

http://altbeacon.github.io/android-beacon-library/samples.html

If you want to directly call startLeScan() and use library code to convert the results to beacon objects, you can call the following method in the scan callback:

Beacon beacon = beaconParser.fromScanData(scanData, rssi, bluetoothDevice)

However, if using a proprietary beacon format (like from from Apple), you will need to construct a BeaconParser with the proper layout. This is proprietary info, but you can do a Google search to find out the proper way to construct a BeaconParser for proprietary layouts.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
1

I think nv-bluetooth is the easiest way to extract iBeacon from advertising packets. Below is a sample implementation of onLeScan method.

public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)
{
    // Parse the payload of the advertising packet.
    List<ADStructure> structures =
        ADPayloadParser.getInstance().parse(scanRecord);

    // For each AD structure contained in the advertising packet.
    for (ADStructure structure : structures)
    {
        if (structure instanceof IBeacon)
        {
            // iBeacon was found.
            IBeacon iBeacon = (IBeacon)structure;

            // Proximity UUID, major number, minor number and power.
            UUID uuid = iBeacon.getUUID();
            int major = iBeacon.getMajor();
            int minor = iBeacon.getMinor();
            int power = iBeacon.getPower();

            ........

See "iBeacon as a kind of AD structures" for details.

Takahiko Kawasaki
  • 18,118
  • 9
  • 62
  • 105
  • But BLE unstable in android, so should not use `BLE scan` way. Ex. beacon already turned off, but BLE scan still find out it. It is wrong. – Huy Tower Jun 25 '15 at 07:18