Reconnecting a Bluetooth device using socket library (RFCOMM mode) in python 3?

0

I am trying to connect to a Bluetooth GPS unit from a Raspberry Pi3 using the socket library in python 3. I am able to connect and get data flowing the first time but if I disconnect and then try reconnecting I get:

[Errno 16] Device or resource busy

I have tried placing the connection in a sub process killing it and recreating it (end goal) and I get the same error. If I close and restart the test program it connects no problem.

Here is a test script based on a demo I found, that opens the connection closes it then tries to reconnect for ever. When I try it I get tick tick tick... until I hit ^c to kill it

import io
import socket
from time import sleep
from bluetooth import *
import sys
class SocketIO(io.RawIOBase):
    def __init__(self, sock):
        self.sock = sock
    def read(self, sz=-1):
        if (sz == -1): sz=0x7FFFFFFF
        return self.sock.recv(sz)
    def seekable(self):
        return False

# file: l2capclient.py
# desc: Demo L2CAP client for bluetooth module.
# $Id: l2capclient.py 524 2007-08-15 04:04:52Z albert $

if sys.version < '3':
    input = raw_input

if len(sys.argv) < 2:
    print("usage: l2capclient.py <addr>")
    sys.exit(2)

bt_addr=sys.argv[1]
port = 1

print("trying to connect to %s on PSM 0x%X" % (bt_addr, port))

# Create the client socket
sock=BluetoothSocket( RFCOMM )

sock.connect((bt_addr, port))
fd = SocketIO(sock)
bno = 0
for line in fd:
    print(line)
    bno +=1
    if bno >10:
         break
sock.shutdown(socket.SHUT_RDWR)
sock.close()
print("closed")
sock=BluetoothSocket( RFCOMM )
not_connected = True
while not_connected:
    try:
        sock.connect((bt_addr, port))
        not_connected = False
    except:
        sleep(1)
        print("tick")
        pass
fd = SocketIO(sock)
try:
    for line in fd:
        print(line)
except IOError:
    pass
sock.close()

The SocketIO class is just for convenience of getting data line by line I have tried it with sock.recv(1024) and got the same results.

python
sockets
bluetooth
raspberry-pi3
rfcomm
asked on Stack Overflow Mar 1, 2018 by Jason R. Moore • edited Mar 1, 2018 by Aditi

1 Answer

0

I have a similar issue. I send data to an HC-05 bluetooth module from my PC using python sockets and a bluetooth RFCOMM socket. Here are a few things which have seemed to improve the debugging situation working with bluetooth...

  • If you havent already, make your socket a nonblocking socket, it sends out a flag when something goes wrong instead of crashing the program
  • Make sure you close the connection properly (it seems that you are doing that though)
  • Make sure that the GPS has no factory settings that prevent you from connecting instantly again. It could maybe have to do with a factory setting/timeout thing not agreeing with the way you request to connect again, and that error could be due to your code and quite possibly in a factory setting if there are any.
answered on Stack Overflow Jul 25, 2018 by user P520

User contributions licensed under CC BY-SA 3.0