Implement autonomous pairing on Linux
It is not currently possible, using Bleak, to autonomously pair. That is, the device must first be paired using BlueZ from the CLI.
There is currently an open PR in bleak for this: https://github.com/hbldh/bleak/pull/523
This ticket is blocked until that is merged.
@tcamise-gpsw Here is my code for automatic pairing for Linux. Have tested on Ubuntu 20.04 and RPI4
I just automated the confirmation of the pairing in the CLI
from open_gopro import GoPro
import subprocess
import multiprocessing
def cmd(command):
response = (
subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
.stdout.read()
.decode(errors="ignore")
)
return response
def accept_pairing():
cmd("yes | bluetoothctl")
p = multiprocessing.Process(target=accept_pairing)
p.start()
with GoPro(enable_wifi=False) as g:
print("paired")
p.terminate()
I really wish i knew that before when started learning OpenGoPro API under linux
I missed this somehow. This is pretty clever! I'll try it out and if it works will open a PR to get this into the Python SDK
@pavlosharhan2 I finally got around to trying this today but I must be missing something because it is not working for me.
I tried debugging by calling yes | bluetoothctl from the command line. But this always just returns an error: "Invalid command in menu main: yes"
Any ideas?
@pavlosharhan2 I finally got around to trying this today but I must be missing something because it is not working for me.
I tried debugging by calling
yes | bluetoothctlfrom the command line. But this always just returns an error: "Invalid command in menu main: yes"Any ideas?
@tcamise-gpsw are you running python script with sudo?
Yes I am.
Actually I ended up finding another way to do this with pexpect:
bluetoothctl = pexpect.spawn("bluetoothctl")
# bluetoothctl.logfile = sys.stdout.buffer
bluetoothctl.expect("Agent registered")
bluetoothctl.sendline(f"pair {handle.address}")
bluetoothctl.expect("Accept pairing")
bluetoothctl.sendline("yes")
bluetoothctl.expect("Pairing successful")
I like this and it seems to be working. I'll merge this soon.
Closing this since it is now functionally working. The cleaner solution is still to implement this through bleak. If bleak ever supports this, we will adopt it.