24

I have a Raspberry Pi 2 running Raspbian kernel 4.1.15 and Bluez version 5.23.

I want to be able to connect a device (like an Android phone) to the rapsberry which should accept the connection without user interaction.

With bluetoothct I can make the device discoverable, pair it with another device, but I haven't found the way to set a pin like with bluetooth-agent

bluetooth-agent PIN

Bluetooth-agent is no more available on my system, is there a way I can do that with bluetoothctl?

Subert
  • 630
  • 1
  • 7
  • 14
  • 1
    Why do you want to set a PIN? The pins are supposed to be auto generated during pairing. So you can just start `bluetoothctl`. Then enter `agent on`. Then initiate a pairing from either end. The pin should be auto generated/negotiated by the two ends (as per the bluetooth spec). `bluetoothctl` will provide the correct pin prompts if a pin is required. If you do need a fixed pin for some reason please describe why you need it and I may be able to provide that if it makes sense to do so. – kaylum Jan 10 '16 at 21:48
  • 2
    @kaylum Thanks for your reply. I need set a PIN, because I can't input a pin on the raspberry as there will be no human connected to it. I want to initiate a pairing from a phone or another device which inputs a fixed pin. So I need to set the pin on the raspberry. – Subert Jan 11 '16 at 09:36
  • 1
    That's handled by the agent IO capabilities (again as defined by the bluetooth spec). So in your case you should run `agent NoInputNoOutput`. This will result in "Just Works" pairing where no user interaction is required. So I still don't think you need fixed pins (fixed pins are for legacy paring and is less secure than the new pairing modes). – kaylum Jan 11 '16 at 10:04
  • @kaylum Thanks I will try that. – Subert Jan 11 '16 at 10:29
  • 2
    Please do not change your title or question if you solve your problem; that does not mark the question as solved in the UI. [Edit](http://stackoverflow.com/posts/34709583/edit) your post to remove the answer from the question and put your solution in the field titled “Your Answer” below. After a day or so you can click the checkmark to the left of the answer; that will mark your question as solved in the UI, and you'll also get a shiny new participation badge. – Dour High Arch Jan 11 '16 at 17:50

6 Answers6

19

Here is what works thanks to kaylum :

$bluetoothctl
[bluetooth]# power on
Changing power on succeeded
[bluetooth]# discoverable on
Changing discoverable on succeeded
[bluetooth]# pairable on
Changing pairable on succeeded
[bluetooth]# agent NoInputNoOutput
Agent registered
[bluetooth]# default-agent 
Default agent request successful

Then I pair the raspberry with my phone from the phone.

[NEW] Device XX:XX:XX:XX:XX:XX nameofthedevice
[CHG] Device XX:XX:XX:XX:XX:XX UUIDS:
      --UUIDS--
[CHG] Device XX:XX:XX:XX:XX:XX Paired: yes
Subert
  • 630
  • 1
  • 7
  • 14
  • 4
    I have the same issue. I want to set a fixed pin code on the raspberry side to avoid "anonymous" connections. This answer doesn't point this out isn't it ? – Emmanuel BRUNET Dec 06 '16 at 14:54
  • 7
    I did exactly the same steps, but still all my phone ask me to enter a PIN and then bluetoothctl still ask to enter the same PIN?! agent NoInputNoOutput doens't seem to work or am I missing something? – Thomas Dec 14 '16 at 21:01
14

I just had the same issue. If you press tab twice after agent you should see a list of available agents; KeyboardOnly works fine without notifications:

[bluetooth]# agent on          # accidentally used wrong agent
Agent registered
[bluetooth]# agent 
DisplayOnly      DisplayYesNo     KeyboardDisplay  KeyboardOnly     NoInputNoOutput  off              on               
[bluetooth]# agent KeyboardOnly 
Agent is already registered    # can't use two agents at one
[bluetooth]# agent off         # unregister agent
Agent unregistered
[bluetooth]# agent KeyboardOnly   # register proper agent
Agent registered
[bluetooth]# pair XX:XX:XX:04:F5:7C 
Attempting to pair with XX:XX:XX:04:F5:7C 
[CHG] Device XX:XX:XX:04:F5:7C Connected: yes
Request passkey
[agent] Enter passkey (number in 0-999999): 722504
[MoarBacon]# pair XX:XX:XX:04:F5:7C 
Attempting to pair with XX:XX:XX:04:F5:7C 
[CHG] Device XX:XX:XX:04:F5:7C Paired: yes
Pairing successful
runejuhl
  • 2,147
  • 1
  • 17
  • 17
4

First you have to configurate sspmode 0, for pin request: hciconfig hci0 sspmode 0

And using bt-agent aplicattion (you can run as deamon too):

bt-agent -c NoInputNoOutput -p /root/bluethooth.cfg

Edit the file configuration, you can put tha mac address and the pin: For example:

XX:XX:XX:XX:XX:XX 1234

Or if you want a pin to all the device the same pin code, for example 1234, edit the file like this: * 1234

This work for me!

gregory
  • 10,969
  • 2
  • 30
  • 42
  • 1
    I tried this, but it seems to just ignore the pinfile and allow the pairing with no pin. – plugwash Jul 31 '20 at 22:26
  • This worked, but I missed the "hciconfig hci0 sspmode 0" the first time I looked at it, If this had been formatted as a series of steps, it would have been more obvious. – B.McCready Aug 02 '22 at 11:43
  • Where did bt-agent go? (It's 2022 now) – Phlip Nov 29 '22 at 16:38
2

I already had bluez-5.43 installed. This is how to automate the pairing process on a raspberry pi.

(1) First test a line like this out to make sure bluetooth agent works:

 bluez-5.43/test/simple-agent -c NoInputNoOutput

(2) To automate pairing, put this code into a shell file (I named mine pairbot.sh):

if [ "$(id -un)" != "pi" ]; then
        exec sudo -u pi $0 "$@"
    fi

    export XAUTHORITY=/home/pi/.Xauthority
    export DISPLAY=:0

     lxterminal --command="/bin/bash -c '/home/pi/bluez-5.43/test/simple-agent -c NoInputNoOutput &; read'"

(3) Go to crontab:

sudo cronetab -e

(4) At the bottom add:

@reboot sleep 20 &&  /home/pi/pairbot.sh > /home/pi/blelog.txt 2>&1

(5) Reboot and test if it works.

My recommendation for others facing the same issue would be to look into your bluez folder (or if you don't have one install the latest version of bluez) and search for the folder that says "test" for "simple agent" to locate the file path. From here, you should be able to construct the command line shown above (1). Hopefully it will work for you too.

Ion Flare
  • 31
  • 2
1

https://github.com/nokia/rcm-bluez/blob/master/client/bluez-5.43/test/simple-agent

https://raw.githubusercontent.com/pauloborges/bluez/master/test/bluezutils.py

@Ion Flare idea is perfect for setup auto-pairing Bluetooth for raspberry pi. just little tweak in my answer is I got an error for import bluezutils so in order to bypass that simply I would say download above file that is simple-agent and bluezutils.py. Put in is same location, for me it is /home/pi. And just run command @Ion Flare mentioned python simple-agent -c NoInputNoOutput

In my use case I changed a simple-agent file to accept every connection and services to make things simple for me.

Zeal
  • 13
  • 3
1

This is what worked for me:

bluetoothctl
agent off
power on
discoverable on
pairable on
agent NoInputNoOutput
default-agent
pair
exit
Zoe
  • 27,060
  • 21
  • 118
  • 148
Matthew
  • 51
  • 6