Page 1 of 1

Simple python bluetooth server

Posted: Sat Jan 07, 2017 5:46 pm
by haylocki
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 :

Code: Select all

stty < /dev/rfcomm0
Produces :
  • speed 115200 baud; line = 0;
    min = 0; time = 0;
    -brkint -icrnl -imaxbel
    -opost
    -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke
Typing:

Code: Select all

rfcomm -a
produces :
  • rfcomm0: B8:27:EB:C0:6E:A1 -> E4:90:7E:B1:84:D0 channel 1 closed [reuse-dlc release-on-hup
[/list]
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
Maybe I'm just doing this all wrong :?

Any help greatly appreciated.

Cheers, Ian

Re: Simple python bluetooth server

Posted: Sat Jan 07, 2017 6:14 pm
by Douglas6
Not sure exactly what the problem is. If you lose the connection, does /dev/rfcomm0 go away after a minute or so? Does it go away after a 'sudo rfcomm release 0'?

Re: Simple python bluetooth server

Posted: Sat Jan 07, 2017 6:36 pm
by haylocki
To be clear it doesn't always fail to remove the device. I have to connect and disconnect a number of times before this problem happens.

The device does not disappear in the time I've waited for it to.

Typing "rfcomm release 0" did not remove the device. Typing the command again replies with "Can't release device: Operation already in progress".

I thought maybe my forcing connection errors until a device was connected might have something to do with it.

I have googled for examples of servers that use an rfcomm0 device that only appears on connection, but haven't found anything yet.

Cheers, Ian

Re: Simple python bluetooth server

Posted: Sat Jan 07, 2017 9:32 pm
by haylocki
OK, so I looked to see if anyone else had the same problem, and although there is another post in this forum with the same error, there was no solution posted.

So I thought maybe it's a race error where a new connection was started, before the old connection had been fully disconnected.

To prove this I added a "time.sleep(0.1)" command after the "ser.close()" command. This seems to have fixed the problem, as I now cannot cause the same error.

Unless of course I'm just being lucky :)

Cheers, Ian

Re: Simple python bluetooth server

Posted: Sat Jan 07, 2017 9:46 pm
by Douglas6
Excellent. Thanks for the update.