Reputation: 174
I wrote an app which can obtain GNSS raw measurement data using
LocationManager.registerGnssMeasurementsCallback(...)
The app can process this data and calculate a new positions. Now, I would like to mock this calculated positions into the Android OS, so other apps can work with this position.To realize this, I can give the app location mocking permissions, add it to the list of location mocking apps in the developer options and work with methods like
LocationManager.addTestProvider(...)
LocationManager.setTestProviderEnabled(...)
LocationManager.setTestProvderLocation(...)
First, I need to decide what location provider I want to mock. PASSIVE_PROVIDER is not allowed to be mocked. NETWORK_PROVIDER can be mocked, but the user would have to select this locating method in the system settings. If he does this, the system stops sending GNSS measurements callbacks to any app which has registered these callbacks, so no position can be calculated anymore. A callback with a status change "GNSS measurements listener disabled" is invoked in this case. The same thing happens when I try to mock GPS_PROVIDER (as soon as setTestProviderEnabled() is called). So I thought about mocking the fused location provider. Since my Android Studio linker cannot resolve LocationManager.FUSED_PROVIDER (no idea why) I directly used the String "fused" and tried to mock the locations. No exceptions come up and gnss raw measurements can still be received. However, on GoogleMaps, I only see the native locations provided by the internal chip. This is the case no matter if the location method "GPS only" or "best" is selected in the system.
So, how can I solve this? Is there any way to mock GPS_PROVIDER without interrupting raw gnss measurements or preventing gnss chip locations to be applied to the fused provider while I am mocking it?
Btw, my code looks like this:
locationManager.addTestProvider("fused" , false, false, false, false, false, false, false, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
locationManager.setTestProviderEnabled("fused", true);
Location location = new Location("fused");
location.setLatitude(lat);
location.setLongitude(lon);
location.setAltitude(alt);
location.setAccuracy(1.0f);
location.setTime(System.currentTimeMillis());
location.setElapsedRealtimeNanos(System.nanoTime());
locationManager.setTestProviderLocation("fused", location);
Upvotes: 1
Views: 977