0

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.

2
  • This appears to be a general Linux question. It may be better asked on a general Linux site.
    – joan
    Aug 6, 2021 at 9:28
  • Oh, I thought it was specific to RPi, so maybe yeah I could look on general or Linux related stack exchange. Aug 10, 2021 at 5:13

2 Answers 2

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

5
  • Thanks for your answer and sorry for the late reply. Thanks for the explanation. I suppose there is a reason that bt-agent has been removed by default, or is it ok to install it? I also tried the example code, unfortunately, it doesn't work. Even if the script goes in the function to trust the device automatically, on the RPi I still have the prompt to ask for trust (and that still failed when I click ok). Aug 10, 2021 at 5:13
  • To be exact, the prompt asks me if I want to accept the request. I also tried to display the parameter "Trusted" to check the value, but I failed, I am not familiar with dbus. For the moment I couldn't connect my phone, even using the screen. I have the "protocol not available" error each time. Aug 10, 2021 at 7:55
  • Could you be more specific on what the prompt says. Maybe update your question with full transcript of what you are getting?
    – ukBaz
    Aug 10, 2021 at 8:42
  • Sorry I think used the wrong word, by prompt, I wanted to say "a message windows" (on the GUI). I'll update my post with the info I gathered (I managed to get the value of the device). Aug 10, 2021 at 8:50
  • I tried also with two other phones. One Android and I have had the same behaviour with the previous one (can connect but with a message windows asking to accept and then an error). The second one is an iPhone, and it can't even see the RPi in the list of available devices (I don't know if there are restrictions on the iPhone side). Aug 11, 2021 at 8:26
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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