使用 xposed 注入陀螺仪事件

Inject gyroscope events with xposed

我想在按下按钮时模拟陀螺仪数据。 是否可以使用 xposed 模块

以编程方式注入这些事件

我通过hook dispatchSensorEvent方法解决了

                   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;
        }