Simple python bluetooth server
Posted: Sat Jan 07, 2017 5:46 pm
Hi,
I'm trying to write a simple bluetooth server. I have the connection to the phone working, which creates /dev/rfcomm0 when connected, and removes the device when connection is lost.
The problem is I sometimes lose the connection, but the rfcomm0 device is not removed.
Typing :Produces :
produces :
My script looks like this:Maybe I'm just doing this all wrong
Any help greatly appreciated.
Cheers, Ian
I'm trying to write a simple bluetooth server. I have the connection to the phone working, which creates /dev/rfcomm0 when connected, and removes the device when connection is lost.
The problem is I sometimes lose the connection, but the rfcomm0 device is not removed.
Typing :
Code: Select all
stty < /dev/rfcomm0- speed 115200 baud; line = 0;
min = 0; time = 0;
-brkint -icrnl -imaxbel
-opost
-isig -icanon -iexten -echo -echoe -echok -echoctl -echoke
Code: Select all
rfcomm -a- rfcomm0: B8:27:EB:C0:6E:A1 -> E4:90:7E:B1:84:D0 channel 1 closed [reuse-dlc release-on-hup
My script looks like this:
Code: Select all
#!/usr/bin/python
# encoding=utf8
import time
import serial
ser = None
while 1:
try:
ser = serial.Serial('/dev/rfcomm0', timeout=1, baudrate=115200)
if ser:
print "connected"
while True:
print ser.readline(),
# we need this to be able to stop the server running
except KeyboardInterrupt:
print "ctrl C pressed"
if ser:
print "closing connection"
ser.close()
time.sleep(0.1)
exit()
# catch all IO errors
except IOError as e:
time.sleep(0.1)
# if we lost connection then close the open port
if ser:
print "closing connection"
ser.close()
ser = None
# now we ignore the error and try to open the port again
continue
Any help greatly appreciated.
Cheers, Ian