1

I am working on a project to communicate between a raspberry pi & a series of Arduinos, using the HC-05 bluetooth modules, over a bluetooth connection. I am able to pair the arduinos using bluetoothctl and the communicating using python scripts, but I would like to include the pairing process in my scripts as well but I have not found a solution that includes the bluetooth pairing pin in the scripts.

What I've tried:

  1. The PyBluez library, but it is incapable of pairing.
  2. Subprocess, but I can't respond to the pin request (code below), but this results in an error of too many arguments (for bluetoothctl).
import subprocess, shlex
addr = "00:14:03:06:12:84"
pinCode = "1234"

args = ["bluetoothctl", f"pair {addr}", pinCode]
args = shlex(args)
subprocess.Popen(args)
  1. I also tried using the bluetoothctl wrapper, but there's is no pin option here either.

Is pairing via python possible?

1QuickQuestion
  • 729
  • 2
  • 9
  • 25

1 Answers1

1

The way Bluez expects this to be done is with the D-Bus Agent API which is documented at https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/agent-api.txt

There is also a Python example in the Bluez source tree: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test/simple-agent

As pairing is normally a one-off provisioning/security step where keys are exchanged and a device is established as trusted, I question the value of automating the pairing process. Do you really want to pair with devices that randomly turn up and are within range?

Subsequent connections between the RPi and HC-05 don't need to have the pairing step happen first. The Raspberry Pi would only be required to call the connection command because the two devices are already paired and trusted.

ukBaz
  • 6,985
  • 2
  • 8
  • 31
  • the goal is to create a remote farm of microcontrollers & raspberry Pi's. As the farm will grow new arduino's (equipped with hc-05's) will be added over time, I'd prefer that we can pair new devices as they come in. If this isn't possible, I guess we'd be limited to the "manual" process. The majority of the interaction is being handled via a web app that we've developed where the users, for the most part, can just use the UI to control different devices. – 1QuickQuestion Oct 04 '20 at 04:36
  • 1
    The Agent API gives you all the flexibility that you need to achieve what you want. If I was to speculate, I would expect your RPi not to be constantly scanning for new nearby devices. I am assuming that scanning is controlled from your web app so there is no reason you couldn't enter the pin through the UI also. Searching for "BlueZ agent examples" found some interesting links such as: https://www.kynetics.com/docs/2018/pairing_agents_bluez/ and https://www.raspberrypi.org/forums/viewtopic.php?t=170271 – ukBaz Oct 04 '20 at 07:24
  • 1
    thank you @ukBaz. You lead me down the right path but I eventually found another solution that didn't require as many variables. Your links provided the correct verbiage to search for, and I found pexpect works great for this very scenario. My solution: https://github.com/NerdboyQ/2020_Arduino_Pi_IOT_Project/blob/master/flask_webapp/tests/nonBlePairing.py – 1QuickQuestion Oct 06 '20 at 06:31