2

I will use my Raspberry Pi headlessly, that's why I would like to be able to connect to it with my smartphone in Bluetooth. Hence I need it to pair and trust automatically the device (with or without asking for a PIN).

Because it's not safe to accept any devices continuously (even with PIN, I guess brute-force search can easily be used), I will add a physical button to activate the discoverability of the Pi for a specific time with Python script (I managed to do that).

But to accept and trust any devices, I could not find a solution. There are either obsolete or still require to trust the device from the RPi. I am running on "Raspbian GNU/Linux 10 (buster)" hence bluetooth-agent function is not available anymore.

I tried the solution on this post, but the RPi still ask me to trust the device. Moreover, if I click on accept I got an error:

Connection failed - 
GDBus.Error.org.bluez.Error.Failed: Protocol not available.
Try to connect manually

(the RPi ask me to trust the device after I click on Pair on my phone).

I then tried to set a PIN for any devices following this post, but unfortunately, it uses the bt-agent command.

So how to achieve:

When the RPi is discoverable, accepting AND trusting any device without asking on the RPi. The RPi can (or not) ask for a PIN to the device (should be activated from startup).

I am not really familiar with Bluetooth, so I don't understand everything (and I don't like that ^^'), for example, what are agent, bluetoothctl, bluez...?

PS: for my tests, I am using a phone on Android 11.

[EDIT]
I followed the infos and code given by @ukBaz on that post, unfortunately, it doesn't work as expected. When I run the script and try to connect, I have a message on my phone asking for pairing with a 6-digit PIN. If I accept, then a Message window (GUI) appears on the RPi asking me to accept the connection. That I don't want because, in future, I'll run the RPi headlessly. Moreover, if I click on accept on that window, I have the previous error (Protocol not available).

I added some displays in the set_trusted function. So the Trusted property is indeed set to one, but my device is not paired.

RequestConfirmation (/org/bluez/hci0/dev_MAC_ADDR, 985379)
Set trusted function
Trusted before: 0
Trusted: 1
Paired: 0
Connected: 1
Set trusted function -- END

Moreover, I don't know if it's normal, but sometimes I have difficulties seeing my RPi when I can with my phone. I have to rescan several times before it appears.

Dark Patate
  • 211
  • 2
  • 10

2 Answers2

3

Let's start with your last question first.... What are the definitions of the terms used:

  • BlueZ - The official Bluetooth stack used on Linux
  • bluetoothctl - The command line tool from BlueZ to control Bluetooth
  • agent - The BlueZ interface that handles pairing requests

bt-agent is not on RPi OS by default but can be installed with sudo apt install bluez-tools

The majority of BlueZ APIs are based around D-Bus which many find has a steep learning curve if they haven't come across it before.

There is some information about the org.bluez.Agent1 api at: https://stackoverflow.com/a/66403748/7721752

ukBaz
  • 1,548
  • 1
  • 7
  • 23
1

sudo hciconfig hci0 noauth might do the trick for the trust issue

user3817445
  • 111
  • 2