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 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.
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...
User contributions licensed under CC BY-SA 3.0