1

I've created a virtual device in AVD with a new hardware profile with sensors disabled. I'm then checking both getDefaultSensor for null and also PackageManager.FEATURE_SENSOR_ACCELEROMETER.

On the emulator with a no sensor hardware profile its reporting true for both.

Context context = mRegistrar.context();
SensorManager sensorManager = (SensorManager) context.getSystemService(context.SENSOR_SERVICE);
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);


PackageManager manager = context.getPackageManager();
boolean hasAccelerometer = manager.hasSystemFeature(PackageManager.FEATURE_SENSOR_ACCELEROMETER);

result.success(hasAccelerometer);

Sensor is not null and returns

{Sensor name="Goldfish 3-axis Magnetic field sensor", vendor="The Android Open Source Project", version=1, type=2, maxRange=2000.0, resolution=1.0, power=6.7, minDelay=10000}

There also doesn't appear to be an option for disabling sensors in the emulator settings menu either.

Is there a way for the emulator to report the same way it would if running on a device with no sensors?

Emile
  • 11,451
  • 5
  • 50
  • 63

1 Answers1

0

Seems like there are ways to detect if your app is running on an emulated device: https://stackoverflow.com/a/13815880/3034696

Try boolean hasAccelerometer = manager.hasSystemFeature(PackageManager.FEATURE_SENSOR_ACCELEROMETER) && !sensor.getName().contains("Goldfish");

prfarlow
  • 3,624
  • 2
  • 15
  • 24
  • yeah, i was wondering if goldfish was a reliable way of testing for the emulator. What i don't know though is what a real phone will receive. I can only assume that if it contains Goldfish then thats the same as false? – Emile Jun 27 '19 at 20:34