Search code examples
androidsensorsandroid-sensorssensormanager

Get Sensor event.values.length() (without waiting for onSensorChanged())


I need to find All the sensor's event.values.length() programmatically. So what I'm doing is, registering all the sensor to get the every sensor's event.values.length(). For most of the sensors, onSensorChanged() was getting invoked in short time. But for some other sensors, the device need to be in motion. Then only onSensorChanged() will be triggered.

I found this official doc. But is there any way to find event.values.length() programmatically.


Solution

  • I found a way to find Sensors event.values.length. Not sure it's a right way but it's works.

     Method[] methodList = Sensor.class.getDeclaredMethods();
     int m_count = methodList.length;
     for (int j = 0; j < m_count; j++) {
         Method method = methodList[j];
    
         if (!method.getName().equals("getMaxLengthValuesArray")) {
            continue;
         }
    
         method.setAccessible(true);
         try {
              int values_length = (Integer) method.invoke(mSensor, mSensor, Build.VERSION.SDK_INT);
              Log.e(TAG,"value length is "+values_length);
              break;
         } catch (IllegalAccessException e) {
              e.printStackTrace();
         } catch (InvocationTargetException e) {
              e.printStackTrace();
         }
     }