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:
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.
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))
Do you know of any method to solve this problem? Even another method with other libraries?
Thanks in advance