You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BlueZ中Scan Responses未设置:PyBlueZ外设主动扫描响应失效

问题排查与修复方案

核心问题分析

你的代码存在两个关键错误,导致扫描响应数据未被bluetoothd正确识别:

  1. 属性归属接口错误
    get_properties()返回的字典使用ADVERTISING_MANAGER_INTERFACE(即org.bluez.LEAdvertisingManager1)作为键,但LEAdvertisement1接口的属性必须挂载在org.bluez.LEAdvertisement1命名空间下,而非广告管理器接口。

  2. GetAll方法中的接口名称拼写错误
    返回语句里的org.bluez..LEAdvertisingManager1多了一个多余的点,即使忽略接口归属问题,这个拼写错误也会导致属性无法被正确读取。

修复后的代码

修改GetAll方法

@dbus.service.method(bluetooth_constants.DBUS_PROPERTIES, in_signature='s', out_signature='a{sv}')
def GetAll(self, interface):
    if interface != "org.bluez.LEAdvertisement1":
        raise bluetooth_exceptions.InvalidArgsException()
    # 改为返回LEAdvertisement1接口对应的属性集合
    return self.get_properties()["org.bluez.LEAdvertisement1"]

修改get_properties方法

def get_properties(self):
    return {
        # 将属性归属改为LEAdvertisement1接口
        "org.bluez.LEAdvertisement1": {
            "Type": dbus.String("peripheral"),  
            "ServiceUUIDs": dbus.Array(["180D"], signature="s"),  
            "Discoverable": dbus.Boolean(True),

            "ScanResponseData": dbus.Dictionary({
                dbus.Byte(0x09): dbus.Array([  
                    dbus.Byte(b) for b in b"TestDev"
                ], signature="y"),
                dbus.Byte(0xFF): dbus.Array([
                    dbus.Byte(0x12), dbus.Byte(0x34)
                ], signature="y")
            }, signature="yv")
        }
    }

额外验证步骤

  1. 重启bluetoothd服务确保配置生效:
    sudo systemctl restart bluetooth
    
  2. 重新运行应用后,用btmon抓包检查扫描响应长度是否正常显示。
  3. 确认扫描响应数据总长度不超过31字节(BLE扫描响应包的最大限制),当前你的数据总长度为11字节,符合要求。

内容的提问来源于stack exchange,提问作者IntelCycloneV

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 22:12:46