简体   繁体   English

Python-连接BLE设备

[英]Python - Connect a BLE device

As the title indicates I have a BLE device that I wish to connect to via a python script. 如标题所示,我有一个希望通过python脚本连接的BLE设备。 I am using a Raspberry Pi and have the newest version of Bluez installed. 我正在使用Raspberry Pi,并安装了最新版本的Bluez。

I have connected to a different BLE device using Bluepy, unfortunately I am unable to retrieve any data using this method with the current BLE that I have, which is why I want to connect to it in a different way. 我已使用Bluepy连接到其他BLE设备,但不幸的是,我无法使用当前拥有的BLE使用此方法检索任何数据,这就是为什么我想以其他方式连接到它。

I have connected to the new device using GATTTool and have successfully obtained data, I am aware that there are libraries that facilitate a connection with GATTTool within a python script. 我已经使用GATTTool连接到新设备并成功获取了数据,我知道在python脚本中有一些库可以促进与GATTTool的连接。 I have tried pexpect and pygatt but neither seem to work due to it timing out before a connection is made. 我尝试了pexpect和pygatt,但由于建立连接之前超时而似乎都不起作用。

Here is a piece of code that I found online; 这是我在网上找到的一段代码;

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()

When I execute the code I get the following output: 当我执行代码时,得到以下输出:

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

I have also tried the following code: 我还尝试了以下代码:

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")

After executing this code I got the exact same error. 执行此代码后,我得到了完全相同的错误。 I have tried changing the Timeout but it doesn't seem to make a difference, all that happens is that it waits the allocated time and the same output will be displayed. 我尝试过更改超时,但似乎没有什么不同,所有发生的事情是它等待分配的时间,并且将显示相同的输出。

What am I missing? 我想念什么? Is there a better way to do this? 有一个更好的方法吗?

Thanks, in advance for any help given. 谢谢,提前提供任何帮助。

Probably it's too late now, but if anyone has the same doubt, you can use the pexpect library. 可能现在为时已晚,但是如果有人有同样的疑问,则可以使用pexpect库。 I'll leave some example code that works with me (i can read some data sent from a PSOC 4200 ble device). 我将留下一些适用的示例代码(我可以读取从PSOC 4200 ble设备发送的一些数据)。

import pexpect
DEVICE = "00:A0:50:CF:62:CD"   # address of your device
if len(sys.argv) == 2:
  DEVICE = str(sys.argv[1])
# Run gatttool interactively.
child = pexpect.spawn("gatttool -I")

# Connect to the device.
print("Connecting to:"),
print(DEVICE)
NOF_REMAINING_RETRY = 3
while True:
  try:
    child.sendline("connect {0}".format(DEVICE))
    child.expect("Connection successful", timeout=5)
  except pexpect.TIMEOUT:
    NOF_REMAINING_RETRY = NOF_REMAINING_RETRY-1
    if (NOF_REMAINING_RETRY>0):
      print "timeout, retry..."
      continue
    else:
      print "timeout, giving up."
      break
  else:
    print("Connected!")
    break

And then to read something just do this: 然后阅读一些内容,只需执行以下操作:

# Presence Sensor
  child.sendline("char-read-hnd 0x16") # put the handle you want to read
  child.expect("Characteristic value/descriptor: ", timeout=5)
  child.expect("\r\n", timeout=5)
  print("Presence Sensor:  "),
  print(child.before),
  print(chr(int(child.before[0:2],16)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM