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

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

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库:

  1. pip install pyhidapi

第二步:
导入模块:

  1. import hid
  2. import time

第三步:查找HID设备:

  1. vendor_id = 0x1234 # 用您的HID设备的供应商ID替换
  2. product_id = 0x6969 # 用您的HID设备的产品ID替换
  3. # 获取连接的HID设备列表
  4. devices = hid.enumerate(vendor_id, product_id)
  5. if len(devices) == 0:
  6. print("没有找到HID设备。")
  7. exit()
  8. device_info = devices[0]
  9. device_path = device_info['path']
  10. device = hid.device()
  11. device.open_path(device_path)

第四步:发送HID数据:

  1. report_id = 0x01 # 用您的HID报告ID替换
  2. data = [0x00, 0x01, 0x02] # 用您要发送的数据替换
  3. # 准备要发送的数据
  4. if report_id is not None:
  5. data.insert(0, report_id)
  6. # 发送数据到手机
  7. device.write(data)
  8. time.sleep(0.1)
  9. # 关闭连接
  10. 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:

  1. pip install pyhidapi

Second step:
Import the modules:

  1. import hid
  2. import time

Third step: Find the hid device:

  1. vendor_id = 0x1234 # Replace with your HID device's vendor ID
  2. product_id = 0x6969 # Replace with your HID device's product ID
  3. # Get the list of connected HID devices
  4. devices = hid.enumerate(vendor_id, product_id)
  5. if len(devices) == 0:
  6. print("Nope, no Hid devices.")
  7. exit()
  8. device_info = devices[0]
  9. device_path = device_info['path']
  10. device = hid.device()
  11. device.open_path(device_path)

Fourth step: Send HID data:

  1. report_id = 0x01 # Replace with your HID report ID
  2. data = [0x00, 0x01, 0x02] # Replace with the data you want to send
  3. # Prepare the data to be sent
  4. if report_id is not None:
  5. data.insert(0, report_id)
  6. # Send the data to the phone
  7. device.write(data)
  8. time.sleep(0.1)
  9. # Close the connection
  10. 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:

确定