androidandroid-sensorsinjectgyroscopexposed

Inject gyroscope events with xposed


i want to simulate gyroscope data when button is pressed. Is it possible to inject those events programmatically by using xposed module


Solution

  • i solved by hook dispatchSensorEvent method

                       findAndHookMethod(
                                "android.hardware.SystemSensorManager$SensorEventQueue",
                                lpparam.classLoader, "dispatchSensorEvent", int.class, float[].class, int.class, long.class, new XC_MethodHook() {
                                    @Override
                                    protected void beforeHookedMethod(MethodHookParam param) {                        
                                    Object systemSensorManager = XposedHelpers.getObjectField(param.thisObject, "mManager");
                                        SparseArray<Sensor> sensors = getSensors(systemSensorManager);
                                        int handle = (Integer) (param.args[0]); 
                                        Sensor sensor = sensors.get(handle);
                                        XposedBridge.log("sensor Name:"+sensor.getName() +" Value= "+ Arrays.toString((float[]) param.args[1]) );
    
                }
    //getSensors method
        protected SparseArray<Sensor> getSensors(Object systemSensorManager) {
                @SuppressWarnings("unchecked")
                HashMap<Integer, Sensor> map = (HashMap<Integer, Sensor>) XposedHelpers.getObjectField(systemSensorManager, "mHandleToSensor");
    
                SparseArray<Sensor> sensors = new SparseArray<>(map.size());
                for (Integer i : map.keySet()) {
                    sensors.append(i, map.get(i));
                }
                return sensors;
            }