The code below is (attempting) to handle a bluetooth serial connection between android phone and RPi. Devices are paired already.
With this code running on the RPi, without a connection to the phone, all is well. The RPi is just idle waiting for connection, no errors. Upon opening BlueTerm on phone and connecting, all is well. Whatever I type on the phone shows up in the RPi terminal window.
It all goes to hell when I close the connection from the phone. Bombs out on SerialException, of all things!
I do not understand for the life of me WHY this code bombs out when I disconnect. I have been hammering on this for 5 hours, seriously, with all sorts of other variations.
***** If I remove the print(BTport.read(1)) line, I can connect and disconnect without any problems at all, but that's a completely useless bit of code if I can't actually send/receive information.
What am I missing here???
Code: Select all
import serial
from serial import SerialException
BTon = 0
while 1:
try:
BTport = serial.Serial("/dev/rfcomm0", baudrate=9600, timeout=3.0)
BTport.close()
except serial.SerialException:
BTon = 0
else:
BTon = 1
if BTon == 1:
BTport = serial.Serial("/dev/rfcomm0", baudrate=9600, timeout=3.0)
print(BTport.read(1))
BTport.close()