OpenGoPro icon indicating copy to clipboard operation
OpenGoPro copied to clipboard

Implement autonomous pairing on Linux

Open tcamise-gpsw opened this issue 4 years ago • 3 comments

It is not currently possible, using Bleak, to autonomously pair. That is, the device must first be paired using BlueZ from the CLI.

tcamise-gpsw avatar Jul 30 '21 22:07 tcamise-gpsw

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 avatar Aug 04 '21 20:08 tcamise-gpsw

@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

pavlosharhan2 avatar Aug 01 '22 18:08 pavlosharhan2

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

tcamise-gpsw avatar Sep 15 '22 19:09 tcamise-gpsw

@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?

tcamise-gpsw avatar Oct 19 '22 17:10 tcamise-gpsw

@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?

@tcamise-gpsw are you running python script with sudo?

pavlosharhan2 avatar Oct 19 '22 17:10 pavlosharhan2

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.

tcamise-gpsw avatar Oct 19 '22 18:10 tcamise-gpsw

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.

tcamise-gpsw avatar Oct 26 '22 17:10 tcamise-gpsw