如何将HID数据发送到Android设备?(使用Python)

huangapple go评论98阅读模式
英文:

How to send hid data to android device? (Using python)

问题

你想从Windows计算机向Android设备发送HID数据(来自鼠标和键盘)。我应该采取什么一般方法?

这样的HID数据包会是什么样子?

操作系统:Windows

目标设备:Android 6.0+

英文:

I want to send hid data (from a mouse and a keyboard) from a windows computer to an android device. What is the general approach I should take for this?

What would such a hid packet look like?

OS: windows

target device: android 6.0+

答案1

得分: 1

要从Python向您的Android设备发送HID数据,您可以使用pyhidapi库。以下是如何使用它的帮助:

第一步:
安装pyhidapi库:

pip install pyhidapi

第二步:
导入模块:

import hid
import time

第三步:查找HID设备:

vendor_id = 0x1234  # 用您的HID设备的供应商ID替换
product_id = 0x6969  # 用您的HID设备的产品ID替换

# 获取连接的HID设备列表
devices = hid.enumerate(vendor_id, product_id)

if len(devices) == 0:
    print("没有找到HID设备。")
    exit()

device_info = devices[0]
device_path = device_info['path']
device = hid.device()
device.open_path(device_path)

第四步:发送HID数据:

report_id = 0x01  # 用您的HID报告ID替换
data = [0x00, 0x01, 0x02]  # 用您要发送的数据替换

# 准备要发送的数据
if report_id is not None:
    data.insert(0, report_id)

# 发送数据到手机
device.write(data)

time.sleep(0.1)

# 关闭连接
device.close()

希望这有助于您从Python向手机发送数据。

英文:

To send HID data to your Android device from Python, you can use the pyhidapi library. Here is some help on how to use it:

1st step:
Install the pyhidapi library:

pip install pyhidapi

Second step:
Import the modules:

import hid
import time

Third step: Find the hid device:

vendor_id = 0x1234  # Replace with your HID device's vendor ID
product_id = 0x6969  # Replace with your HID device's product ID

# Get the list of connected HID devices
devices = hid.enumerate(vendor_id, product_id)

if len(devices) == 0:
    print("Nope, no Hid devices.")
    exit()

device_info = devices[0]
device_path = device_info['path']
device = hid.device()
device.open_path(device_path)

Fourth step: Send HID data:

report_id = 0x01  # Replace with your HID report ID
data = [0x00, 0x01, 0x02]  # Replace with the data you want to send

# Prepare the data to be sent 
if report_id is not None:
    data.insert(0, report_id)

# Send the data to the phone

device.write(data)


time.sleep(0.1) 

# Close the connection
device.close()

i hope this helps you send stuff to your phone from python.

huangapple
  • 本文由 发表于 2023年5月29日 22:09:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定