Search in sources :

Example 1 with RangingData

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

the class RangingDataTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    Context context = ShadowApplication.getInstance().getApplicationContext();
    ArrayList<Identifier> identifiers = new ArrayList<Identifier>();
    identifiers.add(Identifier.parse("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6"));
    identifiers.add(Identifier.parse("1"));
    identifiers.add(Identifier.parse("2"));
    Region region = new Region("testRegion", identifiers);
    ArrayList<Beacon> beacons = new ArrayList<Beacon>();
    Beacon beacon = new Beacon.Builder().setIdentifiers(identifiers).setRssi(-1).setTxPower(-50).setBluetoothAddress("01:02:03:04:05:06").build();
    for (int i = 0; i < 10; i++) {
        beacons.add(beacon);
    }
    RangingData data = new RangingData(beacons, region);
    Parcel parcel = Parcel.obtain();
    data.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);
    RangingData data2 = new RangingData(parcel);
    assertEquals("beacon count shouild be restored", 10, data2.getBeacons().size());
    assertEquals("beacon identifier 1 shouild be restored", "2f234454-cf6d-4a0f-adf2-f4911ba9ffa6", data2.getBeacons().iterator().next().getId1().toString());
    assertEquals("region identifier 1 shouild be restored", "2f234454-cf6d-4a0f-adf2-f4911ba9ffa6", data2.getRegion().getId1().toString());
}
Also used : Context(android.content.Context) RangingData(org.altbeacon.beacon.service.RangingData) Identifier(org.altbeacon.beacon.Identifier) Parcel(android.os.Parcel) ArrayList(java.util.ArrayList) Beacon(org.altbeacon.beacon.Beacon) Region(org.altbeacon.beacon.Region) Test(org.junit.Test)

Example 2 with RangingData

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

the class BeaconIntentProcessor method onHandleIntent.

@Override
protected void onHandleIntent(Intent intent) {
    LogManager.d(TAG, "got an intent to process");
    MonitoringData monitoringData = null;
    RangingData rangingData = null;
    if (intent != null && intent.getExtras() != null) {
        monitoringData = (MonitoringData) intent.getExtras().get("monitoringData");
        rangingData = (RangingData) intent.getExtras().get("rangingData");
    }
    if (rangingData != null) {
        LogManager.d(TAG, "got ranging data");
        if (rangingData.getBeacons() == null) {
            LogManager.w(TAG, "Ranging data has a null beacons collection");
        }
        Set<RangeNotifier> notifiers = BeaconManager.getInstanceForApplication(this).getRangingNotifiers();
        java.util.Collection<Beacon> beacons = rangingData.getBeacons();
        if (notifiers != null) {
            for (RangeNotifier notifier : notifiers) {
                notifier.didRangeBeaconsInRegion(beacons, rangingData.getRegion());
            }
        } else {
            LogManager.d(TAG, "but ranging notifier is null, so we're dropping it.");
        }
        RangeNotifier dataNotifier = BeaconManager.getInstanceForApplication(this).getDataRequestNotifier();
        if (dataNotifier != null) {
            dataNotifier.didRangeBeaconsInRegion(beacons, rangingData.getRegion());
        }
    }
    if (monitoringData != null) {
        LogManager.d(TAG, "got monitoring data");
        Set<MonitorNotifier> notifiers = BeaconManager.getInstanceForApplication(this).getMonitoringNotifiers();
        if (notifiers != null) {
            for (MonitorNotifier notifier : notifiers) {
                LogManager.d(TAG, "Calling monitoring notifier: %s", notifier);
                notifier.didDetermineStateForRegion(monitoringData.isInside() ? MonitorNotifier.INSIDE : MonitorNotifier.OUTSIDE, monitoringData.getRegion());
                if (monitoringData.isInside()) {
                    notifier.didEnterRegion(monitoringData.getRegion());
                } else {
                    notifier.didExitRegion(monitoringData.getRegion());
                }
            }
        }
    }
}
Also used : RangingData(org.altbeacon.beacon.service.RangingData) MonitoringData(org.altbeacon.beacon.service.MonitoringData)

Example 3 with RangingData

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

the class RangingDataTest method testSerializationBenchmark.

@Test
public // On MacBookPro 2.5 GHz Core I7, 10000 serialization/deserialiation cycles of RangingData took 22ms
void testSerializationBenchmark() throws Exception {
    Context context = ShadowApplication.getInstance().getApplicationContext();
    ArrayList<Identifier> identifiers = new ArrayList<Identifier>();
    identifiers.add(Identifier.parse("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6"));
    identifiers.add(Identifier.parse("1"));
    identifiers.add(Identifier.parse("2"));
    Region region = new Region("testRegion", identifiers);
    ArrayList<Beacon> beacons = new ArrayList<Beacon>();
    Beacon beacon = new Beacon.Builder().setIdentifiers(identifiers).setRssi(-1).setTxPower(-50).setBluetoothAddress("01:02:03:04:05:06").build();
    for (int i = 0; i < 10; i++) {
        beacons.add(beacon);
    }
    RangingData data = new RangingData(beacons, region);
    long time1 = System.currentTimeMillis();
    for (int i = 0; i < 10000; i++) {
        Parcel parcel = Parcel.obtain();
        data.writeToParcel(parcel, 0);
        parcel.setDataPosition(0);
        RangingData data2 = new RangingData(parcel);
    }
    long time2 = System.currentTimeMillis();
    System.out.println("*** Ranging Data Serialization benchmark: " + (time2 - time1));
}
Also used : Context(android.content.Context) RangingData(org.altbeacon.beacon.service.RangingData) Identifier(org.altbeacon.beacon.Identifier) Parcel(android.os.Parcel) ArrayList(java.util.ArrayList) Beacon(org.altbeacon.beacon.Beacon) Region(org.altbeacon.beacon.Region) Test(org.junit.Test)

Aggregations

RangingData (org.altbeacon.beacon.service.RangingData)3 Context (android.content.Context)2 Parcel (android.os.Parcel)2 ArrayList (java.util.ArrayList)2 Beacon (org.altbeacon.beacon.Beacon)2 Identifier (org.altbeacon.beacon.Identifier)2 Region (org.altbeacon.beacon.Region)2 Test (org.junit.Test)2 MonitoringData (org.altbeacon.beacon.service.MonitoringData)1