英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论