AltBeacon/android-beacon-library

Custom Distance calculator is being overwritten

Closed this issue · 6 comments

I use a custom-created Distance calculator.

Expected behavior

Whichever calculator is set with Beacon.setDistanceCalculator is kept

Actual behavior

Calculator is overwritten with a ModelSpecificDistanceCalculator

Steps to reproduce this behavior

Lines 239-240 of the BeaconService.java file overwrite any custom calculator. These lines can be executed after a scan has been started, making it difficult to re-overwrite the overwrite

I have also noticed this happening in ScanJob.java, lines 329-330.

Android Beacon Library version

Current code in Master

I will investigate

This mechanism for setting the distance calculator will soon be deprecated and replaced int the 3.0 version of the library.

In the mean time, I think this can be solved by wrapping the code that sets it in an if block like this:

if (Beacon.getDistanceCalculator == null) {
        DistanceCalculator defaultDistanceCalculator =  new ModelSpecificDistanceCalculator(this, BeaconManager.getDistanceModelUpdateUrl());
        Beacon.setDistanceCalculator(defaultDistanceCalculator);
}

Hi David,

Yes, this would work!

Thank you for looking into it.

PS: I'm curious about how v3 will work (not just with the calculator!)

Fixed in #1191

@gerardmundo-birket an example of the new settings api for the distance calculator is visible in the working branch here:

https://github.com/AltBeacon/android-beacon-library/blob/settings-api/lib/src/test/java/org/altbeacon/beacon/SettingsTest.kt#L127

As you can see, instead of setting a class name, the settings pass a custom DistanceClaculatorFactory instance that is responsible for constructing the new custom DistanceCalculator.

Hi David!

Ah, elegant solution!
I appreciate the fix.

Once again, thank you very much, and looking forward to v3!