【发布时间】:2016-08-17 14:09:14
【问题描述】:
正如标题所示,我有一个希望通过 python 脚本连接的 BLE 设备。我正在使用 Raspberry Pi 并安装了最新版本的 Bluez。
我已使用 Bluepy 连接到不同的 BLE 设备,但遗憾的是,我无法使用我拥有的当前 BLE 使用此方法检索任何数据,这就是我想以不同方式连接到它的原因。
我已经使用 GATTTool 连接到新设备并成功获取数据,我知道有一些库可以在 python 脚本中促进与 GATTTool 的连接。我已经尝试过 pexpect 和 pygatt,但似乎都没有工作,因为它在建立连接之前就超时了。
这是我在网上找到的一段代码;
import pygatt.backends
from binascii import hexlify
def printIndication(handle, value):
print('Indication received {} : {}'.format(hex(handle), hexlify(str(value))))
adapter = pygatt.backends.GATTToolBackend()
adapter.start()
while True:
try:
device = adapter.connect('00:38:40:0A:00:04', 5)
break
except pygatt.exceptions.NotConnectedError:
print('Waiting...')
device.subscribe('0002021-0000-1000-8000-00805f9b34fb', callback = printIndication, indication = True)
device.subscribe('00002022-0000-1000-8000-00805f9b34fb', callback = printIndication, indication = True)
device.subscribe('00002a19-0000-1000-8000-00805f9b34fb', callback = printIndication, indication = True)
device.disconnect()
adapter.stop()
当我执行代码时,我得到以下输出:
Traceback (most recent call last):
File "./test.py", line 10, in <module>
adapter.start()
File "/usr/local/lib/python2.7/dist-packages/pygatt/backends/gatttool/gatttool.py", line 90, in start
self._con.expect(r'\[LE\]>', timeout=1)
File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1418, in expect
timeout, searchwindowsize)
File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1433, in expect_list
timeout, searchwindowsize)
File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1535, in expect_loop
raise TIMEOUT(str(err) + '\n' + str(self))
pexpect.TIMEOUT: Timeout exceeded.
<pexpect.spawn object at 0x76737730>
version: 3.2
command: /usr/bin/gatttool
args: ['/usr/bin/gatttool', '-i', 'hci0', '-I']
searcher: <pexpect.searcher_re object at 0x76737770>
buffer (last 100 chars): ''
before (last 100 chars): ''
after: <class 'pexpect.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 5062
child_fd: 3
closed: False
timeout: 30
delimiter: <class 'pexpect.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
我也试过下面的代码:
import pygatt.backends
# The BGAPI backend will attemt to auto-discover the serial device name of the
# attached BGAPI-compatible USB adapter.
adapter = pygatt.backends.GATTToolBackend()
adapter.start()
device = adapter.connect('01:23:45:67:89:ab')
value = device.char_read("a1e8f5b1-696b-4e4c-87c6-69dfe0b0093b")
执行此代码后,我得到了完全相同的错误。我尝试更改超时,但似乎没有什么不同,所发生的只是它等待分配的时间并且将显示相同的输出。
我错过了什么?有没有更好的方法来做到这一点?
在此先感谢您提供的任何帮助。
【问题讨论】:
-
我也有类似的问题。你找到解决办法了吗?
标签: python bluetooth-lowenergy