Ali*_*zun 8 python python-3.x bluez
我正在使用 python 开发一个 Linux 应用程序,它将连接到我的 BLE 设备并通过通知特性获取数据。我正在使用pygatt进行 BLE 通信。我可以成功连接并绑定到设备并读取/写入特性。即使我可以订阅通知特性,但问题是,我的 BLE 设备是一台定制机器,里面有 4 个计数器,每次计数器的数据发生变化时,它都会设置相应的通知标志,因此,使用 onDataChanged-像我可以从读取特性中读取计数器数据的方法。在使用 pygatt 的 Python 中,我可以订阅通知特性:
class_name.device.subscribe(uuid.UUID(notify_characteristic),callback=notifyBle)
Run Code Online (Sandbox Code Playgroud)
和 notifyBle 是:
def notifyBle(self,handle,data):
read_data = class_name.device.char_read(uuid.UUID(read_characteristic))
print(read_data)
Run Code Online (Sandbox Code Playgroud)
当我运行程序时,首先我扫描设备并连接到我的设备并与之绑定,然后我发现特征并列出它们。一切顺利。列出特性后,我写写特性来清除通知标志,它也成功了。最后我订阅通知特性它是成功的。
在所有这些过程之后,我物理地增加了我的设备的计数器(设备上有用于增加计数器的按钮)。当我按下按钮程序转到notifyBle方法时,它给出了错误,即:
Exception in thread Thread-3:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.5/dist-packages/pygatt/backends/gatttool/gatttool.py", line 137, in run
event["callback"](event)
File "/usr/local/lib/python3.5/dist-packages/pygatt/backends/gatttool/gatttool.py", line 479, in _handle_notification_string
self._connected_device.receive_notification(handle, values)
File "/usr/local/lib/python3.5/dist-packages/pygatt/device.py", line 226, in receive_notification
callback(handle, value)
File "/home/acd/Masaüstü/python_workspace/ble.py", line 54, in notifyBle
read_data = bleFunctions.dev.char_read(uuid.UUID(bleFunctions.read_characteristic))
File "/usr/local/lib/python3.5/dist-packages/pygatt/backends/gatttool/device.py", line 17, in wrapper
return func(self, *args, **kwargs)
File "/usr/local/lib/python3.5/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.5/dist-packages/pygatt/backends/gatttool/gatttool.py", line 53, in wrapper
return func(self, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/pygatt/backends/gatttool/gatttool.py", line 519, in char_read
self.sendline('char-read-uuid %s' % uuid)
File "/usr/lib/python3.5/contextlib.py", line 66, in __exit__
next(self.gen)
File "/usr/local/lib/python3.5/dist-packages/pygatt/backends/gatttool/gatttool.py", line 180, in event
self.wait(event, timeout)
File "/usr/local/lib/python3.5/dist-packages/pygatt/backends/gatttool/gatttool.py", line 154, in wait
raise NotificationTimeout()
pygatt.exceptions.NotificationTimeout
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激。
PS:我在 Android 和 Windows UWP 中编写了完全相同的程序。使用 python,我的目标是在 raspberry pi 3 上运行它。
PSS:我使用 raspberry pi 3 和 Ubuntu Mate 安装在 python 中开发这个程序。
首先,创建如下事件类,
class Event:
def __init__(self):
self.handlers = set()
def handle(self, handler):
self.handlers.add(handler)
return self
def unhandle(self, handler):
try:
self.handlers.remove(handler)
except:
raise ValueError("Handler is not handling this event, so cannot unhandle it.")
return self
def fire(self, *args, **kargs):
for handler in self.handlers:
handler(*args, **kargs)
def getHandlerCount(self):
return len(self.handlers)
__iadd__ = handle
__isub__ = unhandle
__call__ = fire
__len__ = getHandlerCount
Run Code Online (Sandbox Code Playgroud)
然后,创建一个 ble 类
import pygatt
from eventclass import Event
class myBle:
ADDRESS_TYPE = pygatt.BLEAddressType.random
read_characteristic = "0000xxxx-0000-1000-8000-00805f9b34fb"
write_characteristic = "0000xxxx-0000-1000-8000-00805f9b34fb"
notify_characteristic = "0000xxxxx-0000-1000-8000-00805f9b34fb"
def __init__(self,device):
self.device = device
self.valueChanged = Event()
self.checkdata = False
def alert(self):
self.valueChanged(self.checkdata)
def write(self,data):
self.device.write_char(self.write_characteristic,binascii.unhexlify(data))
def notify(self,handle,data):
self.checkdata = True
def read(self):
if(self.checkdata):
self.read_data = self.device.char_read(uuid.UUID(self.read_characteristic))
self.write(bytearray(b'\x10\x00'))
self.checkdata = False
return self.read_data
def discover(self):
return self.device.discover_characteristics().keys()
Run Code Online (Sandbox Code Playgroud)
当您收到通知时,您将布尔值设置为 true,警报方法将通知布尔值发生更改。您将监听警报方法
def triggerEvent(checkdata):
print(str(checkdata))
ble = myBle(device)
ble.valueChanged += triggerEvent
ble.alert()
Run Code Online (Sandbox Code Playgroud)
您可以使用triggerEvent方法调用read方法来获取特征值。