1

I am trying to write a simple python script that connects any bluetooth audio device to the raspberry pi 4. I keep getting "bluetooth.btcommon.BluetoothError: (111, 'Connection refused')"

Here is what I currently have: (uses python2)

import bluetooth

devices = bluetooth.discover_devices(lookup_names = False, flush_cache = True, duration = 20)
print devices

print bluetooth.lookup_name(devices[0])

for services in bluetooth.find_service(address = devices[0]):
    print " Name: %s" % (services["name"])
    print " Description: %s" % (services["description"])
    print " Protocol: %s" % (services["protocol"])
    print " Provider: %s" % (services["provider"])
    print " Port: %s" % (services["port"])
    print " Service id: %s" % (services["service-id"])
    print ""
    print ""

s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.connect((devices[0],1))

Output:

['E8:07:BF:35:2E:39']
TAOTRONICS SoundSurge 46
Traceback (most recent call last):
  File "connectTest.py", line 19, in <module>
    s.connect((devices[0],1))
  File "<string>", line 5, in connect
bluetooth.btcommon.BluetoothError: (111, 'Connection refused')

Occasionally when I execute the script it doesn't return any errors, but it still doesn't connect to the device. It also never prints any services.

I should also mention I am using a raspberry pi 4 and PyBluez for bluetooth control. I'm also new to raspberry pis, especially bluetooth programming so any input would be well appreciated.

21
  • 1
    When you run your script, is a code request displayed on the device you are trying to connect to? – Ephemeral Dec 29 '19 at 7:06
  • 1
    @Ephemeral No, I am currently trying to connect to headphones – Titan Dec 29 '19 at 7:09
  • 1
    I also don't know if I am using the correct port or not. – Titan Dec 29 '19 at 7:11
  • 1
    ha ok sorry. First try to connect with bluetoothctl[bluetooth]# connect AA:BB:CC:DD:EE:FF then [bluetooth]# pair AA:BB:CC:DD:EE:FF and [bluetooth]# trust AA:BB:CC:DD:EE:FF. [bluetooth]# list , what is the status ? (change with your BT addr) – Ephemeral Dec 29 '19 at 7:16
  • 1
    And use sdptool browse AA:BB:CC:DD:EE:FF for checking services and ports. – Ephemeral Dec 29 '19 at 7:20
0

The problem is likely to be that you are trying to connect with the RFCOMM profile that your headphones are unlikely to support.

s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)

With the pydbus library it is fairly straightforward to use the BlueZ documented DBus API. The documentation for these are available at:

https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt

https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt

Some useful things to know about DBus to get you started:

  1. The Dbus service for bluez is called 'org.bluez'
  2. The default Bluetooth adapter normally has '/org/bluez/hci0' as its DBus object path.
  3. The DBus object path for a device is the adapter path appending with dev_ and the mac address with the : replaced with _

This example connects to my Bluetooth speaker. It does assume it has already been paired with the Raspberry Pi

import pydbus

audio_dev = '50:C9:71:67:41:CD'
adapter_path = '/org/bluez/hci0'
device_path = f'{adapter_path}/dev_{audio_dev.replace(":", "_")}'

bluez_service = 'org.bluez'
bus = pydbus.SystemBus()
adapter = bus.get(bluez_service, adapter_path)
device = bus.get(bluez_service, device_path)

device.Connect()

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.