Search in sources :

Example 6 with Beacon

use of org.altbeacon.beacon.Beacon in project android-beacon-library by AltBeacon.

the class BeaconService method onCreate.

@Override
public void onCreate() {
    LogManager.i(TAG, "beaconService version %s is starting up", BuildConfig.VERSION_NAME);
    bluetoothCrashResolver = new BluetoothCrashResolver(this);
    bluetoothCrashResolver.start();
    // Create a private executor so we don't compete with threads used by AsyncTask
    // This uses fewer threads than the default executor so it won't hog CPU
    mExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);
    mCycledScanner = CycledLeScanner.createScanner(this, BeaconManager.DEFAULT_FOREGROUND_SCAN_PERIOD, BeaconManager.DEFAULT_FOREGROUND_BETWEEN_SCAN_PERIOD, mBackgroundFlag, mCycledLeScanCallback, bluetoothCrashResolver);
    beaconManager = BeaconManager.getInstanceForApplication(getApplicationContext());
    //flatMap all beacon parsers
    boolean matchBeaconsByServiceUUID = true;
    if (beaconManager.getBeaconParsers() != null) {
        beaconParsers.addAll(beaconManager.getBeaconParsers());
        for (BeaconParser beaconParser : beaconManager.getBeaconParsers()) {
            if (beaconParser.getExtraDataParsers().size() > 0) {
                matchBeaconsByServiceUUID = false;
                beaconParsers.addAll(beaconParser.getExtraDataParsers());
            }
        }
    }
    //initialize the extra data beacon tracker
    mExtraDataBeaconTracker = new ExtraDataBeaconTracker(matchBeaconsByServiceUUID);
    defaultDistanceCalculator = new ModelSpecificDistanceCalculator(this, BeaconManager.getDistanceModelUpdateUrl());
    Beacon.setDistanceCalculator(defaultDistanceCalculator);
    monitoringStatus = MonitoringStatus.getInstanceForApplication(getApplicationContext());
    // Look for simulated scan data
    try {
        Class klass = Class.forName("org.altbeacon.beacon.SimulatedScanData");
        java.lang.reflect.Field f = klass.getField("beacons");
        this.simulatedScanData = (List<Beacon>) f.get(null);
    } catch (ClassNotFoundException e) {
        LogManager.d(TAG, "No org.altbeacon.beacon.SimulatedScanData class exists.");
    } catch (Exception e) {
        LogManager.e(e, TAG, "Cannot get simulated Scan data.  Make sure your org.altbeacon.beacon.SimulatedScanData class defines a field with the signature 'public static List<Beacon> beacons'");
    }
}
Also used : BeaconParser(org.altbeacon.beacon.BeaconParser) Beacon(org.altbeacon.beacon.Beacon) BluetoothCrashResolver(org.altbeacon.bluetooth.BluetoothCrashResolver) ModelSpecificDistanceCalculator(org.altbeacon.beacon.distance.ModelSpecificDistanceCalculator) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 7 with Beacon

use of org.altbeacon.beacon.Beacon in project android-beacon-library by AltBeacon.

the class ExtraDataBeaconTracker method trackGattBeacon.

// The following code is for dealing with merging data fields in beacons
private Beacon trackGattBeacon(Beacon beacon) {
    Beacon trackedBeacon = null;
    HashMap<Integer, Beacon> matchingTrackedBeacons = mBeaconsByKey.get(getBeaconKey(beacon));
    if (matchingTrackedBeacons != null) {
        for (Beacon matchingTrackedBeacon : matchingTrackedBeacons.values()) {
            if (beacon.isExtraBeaconData()) {
                matchingTrackedBeacon.setRssi(beacon.getRssi());
                matchingTrackedBeacon.setExtraDataFields(beacon.getDataFields());
            } else {
                beacon.setExtraDataFields(matchingTrackedBeacon.getExtraDataFields());
                // replace the tracked beacon instance with this one so it has updated values
                trackedBeacon = beacon;
            }
        }
    }
    if (!beacon.isExtraBeaconData()) {
        updateTrackingHashes(beacon, matchingTrackedBeacons);
    }
    if (trackedBeacon == null && !beacon.isExtraBeaconData()) {
        trackedBeacon = beacon;
    }
    return trackedBeacon;
}
Also used : Beacon(org.altbeacon.beacon.Beacon)

Example 8 with Beacon

use of org.altbeacon.beacon.Beacon in project android-beacon-library by AltBeacon.

the class RangeState method finalizeBeacons.

// returns a list of beacons that are tracked, and then removes any from the list that should not
// be there for the next cycle
public synchronized Collection<Beacon> finalizeBeacons() {
    Map<Beacon, RangedBeacon> newRangedBeacons = new HashMap<Beacon, RangedBeacon>();
    ArrayList<Beacon> finalizedBeacons = new ArrayList<Beacon>();
    synchronized (mRangedBeacons) {
        for (Beacon beacon : mRangedBeacons.keySet()) {
            RangedBeacon rangedBeacon = mRangedBeacons.get(beacon);
            if (rangedBeacon.isTracked()) {
                // calculates accuracy
                rangedBeacon.commitMeasurements();
                if (!rangedBeacon.noMeasurementsAvailable()) {
                    finalizedBeacons.add(rangedBeacon.getBeacon());
                }
            }
            // tracked anymore so we don't pass it on as visible unless it is seen again
            if (!rangedBeacon.noMeasurementsAvailable() == true) {
                //measurements for a certain amount of time
                if (!UseTrackingCache || rangedBeacon.isExpired())
                    rangedBeacon.setTracked(false);
                newRangedBeacons.put(beacon, rangedBeacon);
            } else {
                LogManager.d(TAG, "Dumping beacon from RangeState because it has no recent measurements.");
            }
        }
        mRangedBeacons = newRangedBeacons;
    }
    return finalizedBeacons;
}
Also used : HashMap(java.util.HashMap) Beacon(org.altbeacon.beacon.Beacon) ArrayList(java.util.ArrayList)

Example 9 with Beacon

use of org.altbeacon.beacon.Beacon in project android-beacon-library by AltBeacon.

the class BeaconSimulatorTest method testSetBeacons.

@Test
public void testSetBeacons() {
    StaticBeaconSimulator staticBeaconSimulator = new StaticBeaconSimulator();
    byte[] beaconBytes = hexStringToByteArray("02011a1bff1801beac2f234454cf6d4a0fadf2f4911ba9ffa600010002c509");
    Beacon beacon = new AltBeaconParser().fromScanData(beaconBytes, -55, null);
    ArrayList<Beacon> beacons = new ArrayList<Beacon>();
    beacons.add(beacon);
    staticBeaconSimulator.setBeacons(beacons);
    assertEquals("getBeacons should match values entered with setBeacons", staticBeaconSimulator.getBeacons(), beacons);
}
Also used : StaticBeaconSimulator(org.altbeacon.beacon.simulator.StaticBeaconSimulator) AltBeaconParser(org.altbeacon.beacon.AltBeaconParser) Beacon(org.altbeacon.beacon.Beacon) AltBeacon(org.altbeacon.beacon.AltBeacon) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 10 with Beacon

use of org.altbeacon.beacon.Beacon in project android-beacon-library by AltBeacon.

the class ExtraDataBeaconTrackerTest method gattBeaconExtraDataIsNotReturned.

@Test
public void gattBeaconExtraDataIsNotReturned() {
    Beacon extraDataBeacon = getGattBeaconExtraData();
    ExtraDataBeaconTracker tracker = new ExtraDataBeaconTracker();
    Beacon trackedBeacon = tracker.track(extraDataBeacon);
    assertNull("trackedBeacon should be null", trackedBeacon);
}
Also used : Beacon(org.altbeacon.beacon.Beacon) Test(org.junit.Test)

Aggregations

Beacon (org.altbeacon.beacon.Beacon)17 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)7 Context (android.content.Context)2 Parcel (android.os.Parcel)2 AltBeacon (org.altbeacon.beacon.AltBeacon)2 BeaconParser (org.altbeacon.beacon.BeaconParser)2 Identifier (org.altbeacon.beacon.Identifier)2 Region (org.altbeacon.beacon.Region)2 RangingData (org.altbeacon.beacon.service.RangingData)2 StaticBeaconSimulator (org.altbeacon.beacon.simulator.StaticBeaconSimulator)2 HashMap (java.util.HashMap)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 AltBeaconParser (org.altbeacon.beacon.AltBeaconParser)1 ModelSpecificDistanceCalculator (org.altbeacon.beacon.distance.ModelSpecificDistanceCalculator)1 BluetoothCrashResolver (org.altbeacon.bluetooth.BluetoothCrashResolver)1