I took the rfcomm-server.py and rfcomm-client.py from the PyBluez repository on github. The contents of these scripts is the following:
rfcomm-server.py:
import bluetooth
server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
server_sock.bind(("", bluetooth.PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
bluetooth.advertise_service(server_sock, "SampleServer", service_id=uuid,
service_classes=[uuid, bluetooth.SERIAL_PORT_CLASS],
profiles=[bluetooth.SERIAL_PORT_PROFILE],
# protocols=[bluetooth.OBEX_UUID]
)
print("Waiting for connection on RFCOMM channel", port)
client_sock, client_info = server_sock.accept()
print("Accepted connection from", client_info)
try:
while True:
data = client_sock.recv(1024)
if not data:
break
print("Received", data)
except OSError:
pass
print("Disconnected.")
client_sock.close()
server_sock.close()
print("All done.")
rfcomm-client.py:
import sys
import bluetooth
addr = None
if len(sys.argv) < 2:
print("No device specified. Searching all nearby bluetooth devices for "
"the SampleServer service...")
else:
addr = sys.argv[1]
print("Searching for SampleServer on {}...".format(addr))
# search for the SampleServer service
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
service_matches = bluetooth.find_service(uuid=uuid, address=addr)
if len(service_matches) == 0:
print("Couldn't find the SampleServer service.")
sys.exit(0)
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
print("Connecting to \"{}\" on {}".format(name, host))
# Create the client socket
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((host, port))
print("Connected. Type something...")
while True:
data = input()
if not data:
break
sock.send(data)
sock.close()
I first startup the server script which outputs "Waiting for connection on RFCOMM channel 1" and then block waiting for incoming connections. Then I run the client script with the argument 'localhost'. When I do this the SampleServer is found, but when the script tries to connect it blocks for a few second and then raises "bluetooth.btcommon.BluetoothError: [Errno 115] Operation now in progress". If I then try again I get the exception: "bluetooth.btcommon.BluetoothError: [Errno 16] Device or resource busy". The full output is:
$> ./client.c localhost
Searching for SampleServer on localhost...
Connecting to "SampleServer" on localhost
Traceback (most recent call last):
File "<string>", line 3, in connect
_bluetooth.error: (115, 'Operation now in progress')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/user/test/./client.py", line 42, in <module>
sock.connect((host, port))
File "<string>", line 5, in connect
bluetooth.btcommon.BluetoothError: [Errno 115] Operation now in progress
$> ./client.c localhost
Searching for SampleServer on localhost...
Connecting to "SampleServer" on localhost
Traceback (most recent call last):
File "<string>", line 3, in connect
_bluetooth.error: (16, 'Device or resource busy')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/user/test/./client.py", line 42, in <module>
sock.connect((host, port))
File "<string>", line 5, in connect
bluetooth.btcommon.BluetoothError: [Errno 16] Device or resource busy
One point which may be notable. When I leave out the localhost argument from the client. It won't find the SampleServer, which is odd because in that case it should be scanning all devices including localhost.
I have tried setting the host and port number manually in the connect call of client. I have tried changing the uuid of the server. Both of which didn't make a difference.