【问题标题】:Writing HID service with PyBluez使用 PyBluez 编写 HID 服务
【发布时间】:2014-05-06 11:18:40
【问题描述】:

需要使用 PyBluez 在 Linux 上模拟 HID 设备。 我有一个 HID 设备(我将模拟它)。 sdptool browse 命令显示它提供以下服务

Service Name: HID service
Service RecHandle: 0x10001
Service Class ID List:
  "Human Interface Device" (0x1124)
Protocol Descriptor List:
  "L2CAP" (0x0100)
    PSM: 17
  "HIDP" (0x0011)
Language Base Attr List:
  code_ISO639: 0x656e
  encoding:    0x6a
  base_offset: 0x100
Profile Descriptor List:
  "Human Interface Device" (0x1124)
    Version: 0x0101

所以我需要创建一个与此等效的 python 服务。 问题是它完全忽略了我提供的Service class ID list

代码如下:

from bluetooth import *

server_sock=BluetoothSocket(L2CAP)
server_sock.bind(("", 17))
server_sock.listen(1)

uuid = "1f16e7c0-b59b-11e3-95d2-0002a5d5c51b"

advertise_service( server_sock, "PyBluez TEST",
                   service_id = uuid,
                   service_classes = [ HID_CLASS ],
                   profiles = [ HID_PROFILE ])

print("Waiting for connection on L2CAP")

try:
    client_sock, client_info = server_sock.accept()
    print("Accepted connection from ", client_info)

    while True:
        data = client_sock.recv(1024)
        if len(data) == 0:
                break
        print("received [%s]" % data)
except IOError:
    pass
except KeyboardInterrupt:
    print "Stopping..."
    stop_advertising(server_sock)
    sys.exit()  

print("disconnected")

client_sock.close()
server_sock.close()
print("all done")

现在我使用另一个蓝牙适配器运行sdptool,这就是我看到的

Service Name: PyBluez TEST
Service RecHandle: 0x10011
Protocol Descriptor List:
  "L2CAP" (0x0100)
    PSM: 17
Profile Descriptor List:
  "Human Interface Device" (0x1124)
    Version: 0x0100

服务类 ID 列表在哪里?

根据this book(第 65 页),它应该在那里,但不是。

这里也是HCI配置

hci0:   Type: BR/EDR  Bus: USB
    BD Address: 00:17:9A:3F:54:6F  ACL MTU: 1017:8  SCO MTU: 64:0
    UP RUNNING PSCAN 
    RX bytes:37882 acl:320 sco:0 events:949 errors:0
    TX bytes:41443 acl:352 sco:0 commands:629 errors:0
    Features: 0xff 0xff 0x8d 0xfe 0x9b 0xf9 0x00 0x80
    Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3 
    Link policy: RSWITCH HOLD SNIFF PARK 
    Link mode: SLAVE ACCEPT 
    Name: 'My HID Device'
    Class: 0x6e0100
    Service Classes: Networking, Rendering, Capturing, Audio, Telephony
    Device Class: Computer, Uncategorized
    HCI Version: 2.0 (0x3)  Revision: 0x403d
    LMP Version: 2.0 (0x3)  Subversion: 0x430e
    Manufacturer: Broadcom Corporation (15)

我很乐意在这方面得到任何帮助。书籍、文档、示例...任何可能有助于模拟 HID 设备的东西。

** 更新 **

看起来我在连接级别上也有问题。我的 HID 主机在配对后会自动断开连接。

是否可以让我的蓝牙适配器表现得像 HID 设备?

【问题讨论】:

  • 如果你 print HID_CLASS 怎么办?你看到值“1124”了吗?
  • 是的。它打印 1124,它实际上与 HID 类无关。当我设置任何服务类 ID 时,我看到了相同的行为。 SERIAL_PORT_CLASS 或其他任何东西。我也尝试运行hciconfig hci0 reset,但这也无济于事。

标签: python bluetooth hid bluez


【解决方案1】:

您可以使用sdptool 设置服务类uuid:

sdptool setseq 0x10011 0x0001 u0x1124

但它的功能有限。

我找到了另一种使用dbus-python library 来宣传服务的方法:

import dbus

def advertise_service(sdp_record_xml):
    bus = dbus.SystemBus()
    manager = dbus.Interface(bus.get_object("org.bluez", "/"),
                             "org.bluez.Manager")
    adapter_path = manager.FindAdapter(self.device_id)
    service = dbus.Interface(bus.get_object("org.bluez", adapter_path),
                             "org.bluez.Service")
    service.AddRecord(sdp_record_xml)

以下是 XML SDP 记录的示例:

<?xml version="1.0" encoding="utf-8" ?>
<record>
    <attribute id="0x0001">
        <!-- ServiceClassIDList -->
        <sequence>
            <uuid value="00000000-0000-1000-8000-00805F9B34FB" />
        </sequence>
    </attribute>
    <attribute id="0x0003">
        <!-- ServiceID -->
        <uuid value="00000000-0000-1000-8000-00805F9B34FB" />
    </attribute>
    <attribute id="0x0004">
        <!-- ProtocolDescriptorList -->
        <sequence>
            <sequence>
                <uuid value="0x0100" />
            </sequence>
            <sequence>
                <uuid value="0x0003" />
                <uint8 value="0x3" />  <!-- RFCOMM channel -->
            </sequence>
        </sequence>
    </attribute>
    <attribute id="0x0005">
        <!-- BrowseGroupList -->
        <sequence>
            <uuid value="0x1002" />
        </sequence>
    </attribute>
    <attribute id="0x0100">
        <!-- Service name -->
        <text value="My Service" />
    </attribute>
</record>

【讨论】:

  • 如何编辑 sap_record.xml 或以编程方式更改它?我正在尝试在树莓派上创建具有 30 个按钮和 6 个轴的虚拟蓝牙游戏手柄,如何将这些功能写入 sdp_record?
猜你喜欢
  • 2018-01-25
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多