无法向 BLE 设备 Philips Hue Bulb 发送命令(连接断开)

问题描述 投票:0回答:2

我正在尝试使用 Raspberry Pi 4B 打开/关闭蓝牙 Hue 灯泡并更改亮度。灯泡已亮起,我已使用 bluez 成功连接到它。当我尝试运行“char-write-req 0x0027 01”将其打开时,我收到以下消息:

GLib-警告 **:22:53:34.807:文件描述符无效

我可以看到连接成功,但每当我尝试向其写入值时,我都会收到此消息,然后它就会断开连接。运行 bluetoothctl 5.50。我在这里看到了补丁对话:https://www.spinics.net/lists/linux-bluetooth/msg67617.html。但我不确定它是否适用,我什至不知道如何应用它。有人可以帮帮我吗!

编辑我放弃了 gatttool 并使用 bluetoothctl 连接到灯泡和菜单 gatt 向其发送命令。

我发现开关灯的特性是 932c32bd-0002-47a2-835a-a8d455b859dd (对于我的飞利浦 Hue A19)。连接到灯泡后,我可以选择此属性并使用“write 01”将其打开,“write 00”将其关闭。 亮度特性为932c32bd-0002-47a2-835a-a8d455b859dd。当我读取时,它输出“fe”,它是 254 的十六进制。这是最高亮度设置,它已经设置为。我可以使用“write”(值范围为 1-254)来更改亮度。

bluetooth bluetooth-lowenergy bluez philips-hue
2个回答
1
投票

acquire-write
中使用
bluetoothctl
通常不是正确的命令。
read
write
就是您想要的。

开始启动后

bluetoothctl
我希望这一系列命令是:

connect <Address of bulb>
menu gatt
select-attribute 932c32bd-0002-47a2-835a-a8d455b859dd
write 1
write 0

如果你想编写这个脚本,那么下面是一个Python3脚本,我希望它可以打开然后关闭灯泡。

from time import sleep
from pydbus import SystemBus

BLUEZ_SERVICE = 'org.bluez'
BLUEZ_DEV_IFACE = 'org.bluez.Device1'
BLUEZ_CHR_IFACE = 'org.bluez.GattCharacteristic1'


class Central:

    def __init__(self, address):
        self.bus = SystemBus()
        self.mngr = self.bus.get(BLUEZ_SERVICE, '/')
        self.dev_path = self._from_device_address(address)
        self.device = self.bus.get(BLUEZ_SERVICE, self.dev_path)
        self.chars = {}

    def _from_device_address(self, addr):
        """Look up D-Bus object path from device address"""
        mng_objs = self.mngr.GetManagedObjects()
        for path in mng_objs:
            dev_addr = mng_objs[path].get(BLUEZ_DEV_IFACE, {}).get('Address', '')
            if addr.casefold() == dev_addr.casefold():
                return path

    def _get_device_chars(self):
        mng_objs = self.mngr.GetManagedObjects()
        for path in mng_objs:
            chr_uuid = mng_objs[path].get(BLUEZ_CHR_IFACE, {}).get('UUID')
            if path.startswith(self.dev_path) and chr_uuid:
                self.chars[chr_uuid] = self.bus.get(BLUEZ_SERVICE, path)


    def connect(self):
        """
        Connect to device.
        Wait for GATT services to be resolved before returning
        """
        self.device.Connect()
        while not self.device.ServicesResolved:
            sleep(0.5)
        self._get_device_chars()

    def disconnect(self):
        """Disconnect from device"""
        self.device.Disconnect()

    def char_write(self, uuid, value):
        """Write value to given GATT characteristic UUID"""
        if uuid.casefold() in self.chars:
            self.chars[uuid.casefold()].WriteValue(value, {})
        else:
            raise KeyError(f'UUID {uuid} not found')

    def char_read(self, uuid):
        """Read value of given GATT characteristic UUID"""
        if uuid.casefold() in self.chars:
            return self.chars[uuid.casefold()].ReadValue({})
        else:
            raise KeyError(f'UUID {uuid} not found')


device_address = '11:22:33:44:55:66'
light_state = '932c32bd-0002-47a2-835a-a8d455b859dd'


dev = Central(device_address )
dev.connect()
dev.char_write(light_state , [1])
sleep(5)
dev.char_write(light_state , [0])
print(dev.char_read(light_state ))
dev.disconnect()

由于我没有灯泡,上述内容未经测试。但应该是所需内容的良好概述。


1
投票

在我使用蓝牙应用程序将灯泡重置为出厂设置后,它对我有用。

灯泡似乎只能与单个设备配对/绑定。如果其他设备尝试与灯泡通信,连接就会丢失,正如 @ukBaz 在他们的评论中提到的。

© www.soinside.com 2019 - 2024. All rights reserved.