安卓远程操控事件传递的2种方案

1.genymobile的scrcpy使用反射的方法获取InputManager
https://github.com/Genymobile/scrcpy/blob/master/server/src/main/java/com/genymobile/scrcpy/Controller.java

 private Method getInjectInputEventMethod() throws NoSuchMethodException {
        if (injectInputEventMethod == null) {
            injectInputEventMethod = manager.getClass().getMethod("injectInputEvent", InputEvent.class, int.class);
        }
        return injectInputEventMethod;
    }

    public boolean injectInputEvent(InputEvent inputEvent, int mode) {
        try {
            Method method = getInjectInputEventMethod();
            return (boolean) method.invoke(manager, inputEvent, mode);
        } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
            Ln.e("Could not invoke method", e);
            return false;
        }
    }

2.minitouch的方案是unistd方式写入监听触控输入设备

    if ((fd = open(devpath, O_RDWR)) < 0) {   //监听触控输入设备
        perror("open");
        fprintf(stderr, "Unable to open device %s for inspection", devpath);
        goto mismatch;
    }
#define WRITE_EVENT(state, type, code, value) _write_event(state, type, #type, code, #code, value)

static int _write_event(internal_state_touchpad_t *state,
                        uint16_t type, const char *type_name,
                        uint16_t code, const char *code_name,
                        int32_t value) {
    // It seems that most devices do not require the event timestamps at all.
    // Left here for reference should such a situation arise.
    //
    //   timespec ts;
    //   clock_gettime(CLOCK_MONOTONIC, &ts);
    //   input_event event = {{ts.tv_sec, ts.tv_nsec / 1000}, type, code, value};

    struct input_event event = {{0, 0}, type, code, value}; //对 input_event 进行赋值
    ssize_t result;
    ssize_t length = (ssize_t) sizeof(event);

    if (g_verbose)
        fprintf(stderr, "%-12s %-20s %08x\n", type_name, code_name, value); //输出日志

    result = write(state->fd, &event, length); //写入事件
    return result - length;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值