所以,我试着用我的覆盆子皮3B+型号通过BLE连接到飞利浦色调灯。因此,我使用pygatt
库编写了以下python脚本
import pygatt
ADDRESS_TYPE = pygatt.BLEAddressType.random
CHARACTERISTIC = '932c32bd-0002-47a2-835a-a8d455b859dd'
adapter = pygatt.GATTToolBackend()
try:
adapter.start()
devices = adapter.scan(run_as_root=True, timeout=3)
for entry in devices:
if entry['name'] == 'Hue Lamp':
try:
device = adapter.connect(entry['address'], address_type=ADDRESS_TYPE)
device.char_write(CHARACTERISTIC, bytearray([0x00]), wait_for_response=False)
value = device.char_read(CHARACTERISTIC)
device.disconnect()
except pygatt.exceptions.NotConnectedError:
print('Failed to connect to %s' % entry['address'])
finally:
adapter.stop()
当我运行脚本时,似乎成功地建立了连接。char_write()
调用应该关闭指示灯,但什么也没发生。char_read()
调用引发了一个异常,如下所述
Traceback (most recent call last):
File "main.py", line 21, in <module>
value = device.char_read(CHARACTERISTIC)
File "/usr/local/lib/python3.7/dist-packages/pygatt/backends/gatttool/device.py", line 17, in wrapper
return func(self, *args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/pygatt/backends/gatttool/device.py", line 40, in char_read
return self._backend.char_read(self, uuid, *args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/pygatt/backends/gatttool/gatttool.py", line 50, in wrapper
return func(self, *args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/pygatt/backends/gatttool/gatttool.py", line 593, in char_read
self.sendline('char-read-uuid %s' % uuid)
File "/usr/lib/python3.7/contextlib.py", line 119, in __exit__
next(self.gen)
File "/usr/local/lib/python3.7/dist-packages/pygatt/backends/gatttool/gatttool.py", line 191, in event
self.wait(event, timeout)
File "/usr/local/lib/python3.7/dist-packages/pygatt/backends/gatttool/gatttool.py", line 157, in wait
raise NotificationTimeout()
pygatt.exceptions.NotificationTimeout: None
我可以用pygatt
读出灯的可调谐特性。我还可以通过NRF Connect Android应用程序控制灯。当我用app向上述特性写入值0x00
时,灯会按预期关闭
目前没有回答
相关问题 更多 >
编程相关推荐