ashasingh18
Posts: 5
Joined: Mon Nov 13, 2017 7:56 pm

Raspberry PI fails to connect to Bluetooth LE device thru Python script

Mon Nov 13, 2017 10:22 pm

Hi,
I was able to connect to 2 Bluetooth LE devices one at a time from my Raspberry PI 3 by using the below set of bluez commands:
1. sudo hcitool lescan
2. sudo gatttool -b <BLE ADDRESS> --interactive -t random --listen
3. connect

As well as retrieve some data using "char-read-hnd 0x0010"

But I am not able to connect to these BLE devices using python script such as "python GetData1.py" where the contents of file
GetData1.py is :
==================================================================
import pexpect
import time
import sys
import os

DEVICE = "ED:E0:E1:D6:CE:D1" # device #24

# Run gatttool interactively.
#child = pexpect.spawn("gatttool -I")
child = pexpect.spawn('sudo gatttool -b ' + DEVICE + ' -I')
child.expect('\[LE\]>')
# Connect to the device.
print("Connecting to:"),
print(DEVICE)
child.sendline('connect')
child.sendline('\[CON\].*>')
child.sendline('char-desc')
child.expect('\[LE\]>')
==================================================================

Any help and suggestions will be very much appreciated.
Thanks,
Asha

User avatar
Douglas6
Posts: 4089
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Raspberry PI fails to connect to Bluetooth LE device thru Python script

Tue Nov 14, 2017 2:10 pm

I'll try to play around with this (there's some odd stuff you seem to be trying to write to gatttool), but most obviously, you are not using the address type flag (-t random) in the Python code, which would prevent you from connecting.

If there are typos in the Python code you posted, please correct them.

andrum99
Posts: 379
Joined: Fri Jul 20, 2012 2:41 pm

Re: Raspberry PI fails to connect to Bluetooth LE device thru Python script

Tue Nov 14, 2017 3:37 pm

I would also check that you can successfully execute some other simple shell commands (ls for example) from Python. My next port of call would be to execute the required commands at the command line, being sure to use the exact same syntax as you are trying to use from within Python. If that is successful, you can then try from Python.

HTH

ashasingh18
Posts: 5
Joined: Mon Nov 13, 2017 7:56 pm

Re: Raspberry PI fails to connect to Bluetooth LE device thru Python script

Tue Nov 14, 2017 6:15 pm

Hi,

So there is typo in the posted GetData1.py, "child = pexpect.spawn('sudo gatttool -b ' + DEVICE + ' -I') where there needs to be a space after 'b" and space before "-I" as "child = pexpect.spawn('sudo gatttool -b ' + DEVICE + ' -I')

But even with the space, this does not work with the Nordic BLE device nRF52832-DK
what does work is "child = pexpect.spawn('sudo gatttool -b ' + DEVICE + ' -I -t random --listen')"

I tried attaching the file GetData1.py modified for the Nordic BLE device nRF52832-DK but I am unable to.

I run "python GetData1.py" and it does not connect to my device because I do not see solid green led on on the device.

But when I go into Python interactive mode and enter each command in the GetData1.py, it seems work because I see the solid green light on the Nordic BLE device nRF52832-DK indicating that it has connected. However, I do not see any output while in python active mode when I enter "child.sendline('char-desc'). How can I see the output?

As mentioned above, when I run the same "python GetData1.py" it does not work and so
I tried to attach the files but have problems and so below is the contents:

# python GetData.py <device>
# e.g. python GetData.py "ED:E0:E1:D6:CE:D1"
import pexpect

DEVICE = "ED:E0:E1:D6:CE:D1" # device #24

# Run gatttool interactively.
#child = pexpect.spawn("gatttool -I")
child = pexpect.spawn('sudo gatttool -b ' + DEVICE + ' -I -t random --listen')
child.expect('\[LE\]>')
# Connect to the device.
print("Connecting to:"),
print(DEVICE)
child.sendline('connect')
child.sendline('\[CON\].*>')
child.sendline('char-desc')
child.expect('\[LE\]>')
print("Connected")

Below is the Python interactive mode output:

pi@raspberrypi:~ $ python
Python 2.7.13 (default, Jan 19 2017, 14:48:08)
[GCC 6.3.0 20170124] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pexpect
>>>
>>> DEVICE = "ED:E0:E1:D6:CE:D1" # device #24
>>> child = pexpect.spawn('sudo gatttool -b ' + DEVICE + ' -I')
>>> child.expect('\[LE\]>')
0
>>> # Connect to the device.
... print("Connecting to:"),
Connecting to:
>>> print(DEVICE)
ED:E0:E1:D6:CE:D1
>>> child.sendline('connect')
8
>>> child.sendline('\[CON\].*>')
11
>>> child.sendline('char-desc')
10
>>> child.expect('\[LE\]>')
0
>>> print("Connected")
Connected
>>>

Appreciate all the help I can get.

Thanks,
Asha

User avatar
Douglas6
Posts: 4089
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Raspberry PI fails to connect to Bluetooth LE device thru Python script

Tue Nov 14, 2017 6:27 pm

I'm pretty sure that

Code: Select all

child.sendline('\[CON\].*>')
should be something like

Code: Select all

child.expect('\[CON\].*>')
I don't recall off hand the regex syntax that pexpect uses. Or, for that matter, what gatttool is sending. I'll try and play with my TI SensorTag today.

ashasingh18
Posts: 5
Joined: Mon Nov 13, 2017 7:56 pm

Re: Raspberry PI fails to connect to Bluetooth LE device thru Python script

Tue Nov 14, 2017 6:52 pm

Hi,
Thanks for your reply. You are correct on "child.expect('\[CON\].*>')". I changed my script and now I am getting the following
error output from both -- Python interactive and running the script GetData1.py:

@raspberrypi:~ $ python GetData1.py
Connecting to: ED:E0:E1:D6:CE:D1
Traceback (most recent call last):
File "GetData1.py", line 15, in <module>
child.expect('\[CON\].*>')
File "/home/pi/.local/lib/python2.7/site-packages/pexpect/spawnbase.py", line 327, in expect
timeout, searchwindowsize, async_)
File "/home/pi/.local/lib/python2.7/site-packages/pexpect/spawnbase.py", line 355, in expect_list
return exp.expect_loop(timeout)
File "/home/pi/.local/lib/python2.7/site-packages/pexpect/expect.py", line 106, in expect_loop
return self.timeout(e)
File "/home/pi/.local/lib/python2.7/site-packages/pexpect/expect.py", line 70, in timeout
raise TIMEOUT(msg)
pexpect.exceptions.TIMEOUT: Timeout exceeded.
<pexpect.pty_spawn.spawn object at 0x76a268d0>
command: /usr/bin/sudo
args: ['/usr/bin/sudo', 'gatttool', '-b', 'ED:E0:E1:D6:CE:D1', '-I', '-t', 'random', '--listen']
buffer (last 100 chars): '1:D6:CE:D1\r\n[ED:E0:E1:D6:CE:D1][LE]> \r\x1b[KConnection successful\r\n\x1b[0;94m [ED:E0:E1:D6:CE:D1]\x1b[0m[LE]> '
before (last 100 chars): '1:D6:CE:D1\r\n[ED:E0:E1:D6:CE:D1][LE]> \r\x1b[KConnection successful\r\n\x1b[0;94m[ED:E0:E1:D6:CE:D1]\x1b[0m[LE]> '
after: <class 'pexpect.exceptions.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 1120
child_fd: 5
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
searcher: searcher_re:
0: re.compile("\[CON\].*>")

Thanks,
Asha

User avatar
Douglas6
Posts: 4089
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Raspberry PI fails to connect to Bluetooth LE device thru Python script

Tue Nov 14, 2017 8:07 pm

This works for me using my TI SensorTag (which does not use a random address type):

Code: Select all

import pexpect

BDADDR = "34:B1:F7:D5:16:5E"

child = pexpect.spawn("sudo gatttool -b " + BDADDR + " --interactive")
child.expect("\[LE\]>")
print("Spawned gatttool")

child.sendline("connect")
child.expect("Connection successful\r\n")
print("Connected to device: " + BDADDR)

child.sendline("char-desc")
child.readline()
try:
    while True:
        line = child.readline()
        print(line.strip())
except pexpect.exceptions.TIMEOUT:
    pass

child.close()
print("Completed")



ashasingh18
Posts: 5
Joined: Mon Nov 13, 2017 7:56 pm

Re: Raspberry PI fails to connect to Bluetooth LE device thru Python script

Tue Nov 14, 2017 8:39 pm

Hi,

Thank you very much. Very much appreciated your help.
I was able to use your script to test BLE connection between my SensorTag and Raspberry PI3.
Just modified the BLE MAC address.

I was also able to use your script to test BLE connection between my Nordic nRF52-DK and Raspberry PI3.
Just modified the BLE MAC address and the line "child = pexpect.spawn("sudo gatttool -b " + BDADDR + " --interactive")"
to "child = pexpect.spawn("sudo gatttool -b " + BDADDR + " --interactive -t random")"

My next step is to have BLE connections between my Nordic nRF52-DK and Raspberry PI3
and BLE connection between my SensorTag(or another Nordic nRF52-DK) and Raspberry PI3 at the same time without disconnecting.

Thanks,
Asha

User avatar
Douglas6
Posts: 4089
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Raspberry PI fails to connect to Bluetooth LE device thru Python script

Tue Nov 14, 2017 8:47 pm

If you're having trouble with pexpect and gatttool, you might try a Python wrapper for gatttool, such as pygatt.

I don't believe the Bluetooth 4.2 spec allows for multiple simultaneous connections. I believe you will need to connect to multiple devices sequentially.

ashasingh18
Posts: 5
Joined: Mon Nov 13, 2017 7:56 pm

Re: Raspberry PI fails to connect to Bluetooth LE device thru Python script

Tue Nov 14, 2017 9:39 pm

Hi,
Thanks and I did try the Python wrapper which never connected was always "Waiting" to my sensortag:

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('B0:91:22:F6:AA:00')
break
except pygatt.exceptions.NotConnectedError:
print('Waiting...')

device.disconnect()
adapter.stop()

I am told that up to 8 BLE devices can be connected to a central device. In my test scenario, the Raspberry PI is the central device and it will connect to multiple BLE peripheral devices.

Thanks,
Asha

Return to “General discussion”

Who is online

Users browsing this forum: No registered users and 53 guests