0

I have two RPi3 operating on Jessie OS distribution and I have already Bluez and Blueman in both of them. I want to be able to send string from one to another. To do so, I have used the Bluez scripts of client server from this blog: http://blog.kevindoran.co/bluetooth-programming-with-python-3/ To determine the mac address of each one of them for my bluetooth connection I have typed in the terminal:

$bluetoothctl

and then I have picked the address labelled: default. This setup was working and I was able to send string between the two boards but since yesterday the system stopped working and I am always getting the following error message:

bluetooth.btcommon.BluetoothError: (111, 'Connection refused')

The pairing is established and the connection establishes for just a small amount of time and then they get disconnected.

========================================================================

Those are the client-server scripts of Bluez taking from the mentioned blog:

The Client:


"""
A simple Python script to send messages to a sever over Bluetooth
using PyBluez (with Python 2).
"""

import bluetooth

serverMACAddress = 'xx:xx:xx:xx:xx:xx'
port = 3
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.connect((serverMACAddress, port))
while 1:
    text = raw_input() # Note change to the old (Python 2) raw_input
    if text == "quit":
    break
    s.send(text)
sock.close()

The server:

"""
A simple Python script to receive messages from a client over
Bluetooth using PyBluez (with Python 2).
"""

import bluetooth

hostMACAddress = 'xx:xx:xx:xx:xx:xx' # The MAC address of a Bluetooth adapter on the server. The server might have multiple Bluetooth adapters.
port = 3
backlog = 1
size = 1024
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.bind((hostMACAddress, port))
s.listen(backlog)
try:
    client, clientInfo = s.accept()
    while 1:
        data = client.recv(size)
        if data:
            print(data)
            client.send(data) # Echo back to client
except: 
    print("Closing socket")
    client.close()
    s.close()
  • I found the error! The client must obtain the server's address. However the Server must use the address of any local adapter that can be used. That means the server uses its local address to be clear (in the case where there is one single bluetooth hardware module) – Saddam Apr 16 '17 at 16:42
  • Could you please clarify on what you changed to make this work? I'm experiencing the same issue. – Mike Lawrence Jun 2 '17 at 16:28
  • Thanks for your question. Actually the problem is the address you specify when you want to bind the sockets. For the client, you should provide the address of the server you want to connect to. HOWEVER for the server you should specify the local address of that server NOT the address of the client! – Saddam Jun 3 '17 at 16:57
  • Can you provide a working example? – Mike Lawrence Jun 3 '17 at 17:19
  • ok, check my answer post – Saddam Jun 3 '17 at 18:00
0

To make it work, just modify the file of the server. You have to let the address to which you bind the socket void or you should specify your Bluetooth adapter mac address. In case you let it void, the default address of your Bluetooth adapter will be used.

"""
A simple Python script to receive messages from a client over
Bluetooth using PyBluez (with Python 2).
"""

import bluetooth

hostMACAddress = '' 
port = 3
backlog = 1
size = 1024
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.bind((hostMACAddress, port))
s.listen(backlog)
try:
    client, clientInfo = s.accept()
    while 1:
        data = client.recv(size)
        if data:
            print(data)
            client.send(data) # Echo back to client
except: 
    print("Closing socket")
    client.close()
    s.close()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.