Automate Bluetooth Pairing/Trusting in Bluez5

痴心易碎 提交于 2020-01-16 03:29:04

问题


I've been working on making my RPi 2 function like a car bluetooth receiver and all is well, except I have no idea how I could automate the pairing of bluetooth devices in Bluez5. In the past I would've used the bluetooth agent and a simple script, but that seems to have gone out the window with the move from 4 -> 5. The nature of the setup means I have no kb/mouse on the RPi once its in the car, so it needs to be a fully automated setup where anyone can scan for the RPi, and if the probably hard-coded PIN is correct, the trusting of the device needs to be automatically done, no cli input.

I've searched all over the web but everyone seems to say that using bluetoothctl works for them, but in this particular setup where I'd like to be able to have friends pair their own phones, having to trust devices with the RPi out of the car isn't ideal.


回答1:


I'm not sure why you say using a simple script is not possible with bluez5. I think you can do it. Below is one example how.

Download the bluez5 source and edit test/simple-agent. Comment out the lines of code as shown below:

def RequestAuthorization(self, device):
        print("RequestAuthorization (%s)" % (device))
        #auth = ask("Authorize? (yes/no): ")
        #if (auth == "yes"):
        return
        #raise Rejected("Pairing rejected")

What that does it remove the prompt for authorisation and always accepts the pairing request.

Can now start the simple-agent with the NoInputNoOutput capability so that it uses simple pairing and will go through the above code path:

./simple-agent -c NoInputNoOutput

After that you should be able to pair with the RPi without any user prompt or PIN.

Note that this is just one example of what you can do. If say you wanted to have a hard coded PIN instead of simple pairing then edit the appropriate section of the same simple-agent code to do that. I'll leave that as an exercise for you.




回答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.



来源:https://stackoverflow.com/questions/30233442/automate-bluetooth-pairing-trusting-in-bluez5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!