0

I am trying to detect whether user is indoor or outdoor using Acceleromter, magnetic field and light sensor. I am using the following method:

Registering the Sensor Manager

    @Override
    protected void onResume()
    {
        super.onResume();
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
                SensorManager.SENSOR_DELAY_FASTEST);

        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_FASTEST);

        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_FASTEST);
    }

Sensor Listener and Calcualtions for different sensor types

override fun onSensorChanged(event: SensorEvent) {
        val values = event.values
        when (event.sensor.type) {
            Sensor.TYPE_MAGNETIC_FIELD -> {
                mag_val = Math.sqrt(values[0] * values[0] + values[1] * values[1] + (values[2] * values[2]).toDouble())
            }
            Sensor.TYPE_LIGHT -> {
                light_val = values[0].toDouble()
            }
            Sensor.TYPE_ACCELEROMETER -> {
                acc_val = Math.sqrt(values[0] * values[0] + values[1] * values[1] + (values[2] * values[2]).toDouble()) / 10
            }
        }
    }

Which combination of values should I consider to decide that user is Indoor or Outdoor? Currently, searching online I used the following values but it is showing wrong decisions:

fun isOutDoor(): Boolean {
        if (light_val > 900) {
            return true
        } else {
            if (mag_val > 80) {
                if (acc_val > 1.3) {
//                    "OUTDOOR_NIGHT"
                    return true
                } else {
//                    "OUTDOOR_BUS"
                    return true
                }
            } else {
//                "INDOOR"
                return false
            }
        }
    }

Please guide which combination of values should be for Indoor buildings and outside?

Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46
  • It is hard to say what values are the best for some case. It would be better for you to make an app and print some values on a screen when you are in some scenarios... – DM developing Sep 14 '20 at 18:44

1 Answers1

0

You need do gather some dataset from all of the possible sensors (seems barometer, date-time etc. are also useful) and use Machine Learning to create more accurate classification model (take a look at this, that or that as examples).

Also, you can try workaround like that:

  1. Use GPS (or other location provider) to get location coordinates;

  2. Use Styled Static Maps and set all outdoor places to same solid color e.g. pure red (0xFF0000) get one pixel map bitmap for location from p.1 and check it color (like in this answer). If it pure red (0xFF0000) - user is highly likely outdoor.

Of course you can use combinations of workaround with yours "decision tree" approach.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79