rapsberrytc
Posts: 4
Joined: Fri Apr 02, 2021 2:03 pm

Bluetooth python handle reconnection automatically

Thu Jun 17, 2021 6:18 pm

Hello,

I made a python program on Raspberry Pi able to connect in classic bluetooth (not BLE) with sensors. To do this, I used socket programming. I can perfectly connect to several sensors simultaneously and exchange data (receive/transmit) between the Raspberry Pi and the sensors.

Overall, the most important lines in my code are the following:

Code: Select all

import bluetooth
import socket

addr_sensor1 = xx:xx:xx:xx:xx:xx
port_sensor1 = 1

sock_sensor1=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock_sensor1.connect((addr_sensor1, port_sensor1))

sock_sensor1.send("Message\n")

while True : 
    while char != '\n' :
        char = str(sock_sensor1.recv(1).decode("utf-8", "ignore"))
        data = data + char
    print(str(data))
The problem I can't solve is when one of the sensors is out of range of the Raspberry Pi, it ends up disconnecting. I would like it if when the sensor is within range of the Raspberry Pi again, the python script is able to automatically reconnect to the sensor.

Do you know of any method to solve this problem? Even another method with other libraries?

Thanks in advance

User avatar
richonguzman
Posts: 122
Joined: Fri Aug 28, 2020 4:38 pm
Location: Chile

Re: Bluetooth python handle reconnection automatically

Thu Jun 17, 2021 8:35 pm

maybe a loop every second or 10 seg to check if connection is lost try to connect again?

rapsberrytc
Posts: 4
Joined: Fri Apr 02, 2021 2:03 pm

Re: Bluetooth python handle reconnection automatically

Sat Jun 19, 2021 4:34 pm

Thank you for your answer. I'm trying this solution. Is there a specific function in the socket library that allows to check if connection is still active ? Or schould I send a message each 10 seconds to my sensor in order to check the connection ?


Otherwise, I have found that I can move my sensor out of bluetooth range of the Raspberry for a while and then move the sensor back into bluetooth range, as soon as the Pi regains the signal from the sensor, it starts collecting data again (no disconnection). This only happens when the sensor is out of range of the Pi for a short time. If the sensor is out of range of the pi for too long, there will be a disconnection, so even if the sensor is moved closer to the pi again, the Pi will not receive data anymore, at this moment, the connection must be re-established.

Does this mean that there is some kind of timeout after which the Pi destroys the bluetooth connection with a device if it no longer receives the signal? If I can change the duration of this timeout and set it to infinite, that would solve my problem. Do you know if it's possible to do that with socket programming or maybe I should use another method to configure my bluetooth connection ?

Thank you in advance

katesimon123
Posts: 34
Joined: Sun Jun 13, 2021 2:45 am

Re: Bluetooth python handle reconnection automatically

Sun Jun 20, 2021 6:33 am

You have to design the logic in your programming, though polling keep on searching for available connection, and when someone comes in the range then try connecting with it.

You can also save their names or addresses so that you could recognize an already connected one.

rapsberrytc
Posts: 4
Joined: Fri Apr 02, 2021 2:03 pm

Re: Bluetooth python handle reconnection automatically

Fri Jun 25, 2021 6:55 pm

Thank you for your answer.

And do you know how I can do this? Maybe I should use another library than PyBluez? Is there a lower level library than PyBluez?

Thank you in advance.

Return to “Python”