我如何从 bluethootctl 中获取 debug/return 字符串

How do i get the debug/return strings out of bluethootctl

如何检索这两行:

Changing discoverable off succeeded [CHG] Controller 64:6E:69:F4:9E:72 Discoverable: no

基本上抓取输出或其他可能的错误输出并丢弃其余的垃圾

我正在尝试创建一个非常简单的蓝牙 dmenu bash 脚本的分支,我在这方面取得了一定的成功,但是因为我考虑过将它发布到 public , 它必须有错误处理并且是一个通常圆形的脚本等...

我现在要做的是当我回应“关闭电源”时 | bluethothctl 我想获取它在内部发送的消息,例如:更改可发现关闭成功,org.bluez.something 被阻止,等等...

我尝试过读取 -r 调试,将其放入 $( ),然后使用 notify 或 echo 对其进行调试,但我总是得到外部垃圾(如 Agent 已注册,[IFROGZ] 可发现等..)

有没有一种方法可以在不使用 boogaloo 策略的情况下获取那些美味的内部消息的 bluetoothctl 在内部发送到一个变量(比如将所有内容输出到一个文件然后读取它)

我认为我上面所说的是你唯一希望为我解决这个问题的事情,但只是因为之前我因为没有显示代码而被否决,这里是它的一部分

    finalArgs[inc++]=" "
    finalArgs[inc++]=$(echo show | _bluetoothctl | grep -m 1 "Powered:" | cut -c2- | sed -e "s/Powered/Power/")
    finalArgs[inc++]=$(echo show | _bluetoothctl | grep -m 1 "Discoverable:"| cut -c2-)
    finalArgs[inc++]=$(echo show | _bluetoothctl | awk "/Pairable:/{i++}i==2{print; exit}"| cut -c2-)
    name=$(printf '%s\n' "${finalArgs[@]}" | dmenu -l ${#finalArgs[@]} -p "btmenu")
    [[ $name ]] || return
    mac=${name:format+2:-1}
    if [[ ${name:1:1} != " " ]]; then
        #avoiding a if here
        #echo $name
        #echo $name
        notify "Changing $name"
        mac=$(echo "$name" | sed -e "s/: yes/ off/")
        if [[ $mac =~ "$name" ]]; then 
            mac=$(echo "$name" | sed -e "s/: no/ on/")
        fi
        debug=$(echo ${mac,,} | _bluetoothctl)
        echo $debug 
        
        #notify $debug
        
        #notify ""
        exit 
    else
    (...)

奇怪的是,我使用相同的策略来获取显示元素,但它不适用于那些特定的“调试消息”

我不确定 bluetoothctl 是否打算以这种方式使用。 BlueZ 有一个文档化的 API,它使用可用于大多数语言的 DBus 绑定,网址为:

https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/

这可以通过下面的 bash 命令行完成:

pi@raspberry:~ $ busctl get-property org.bluez /org/bluez/hci0 org.bluez.Adapter1 Discoverable 
b false
pi@raspberry:~ $ busctl set-property org.bluez /org/bluez/hci0 org.bluez.Adapter1 Discoverable b true
pi@raspberry:~ $ busctl get-property org.bluez /org/bluez/hci0 org.bluez.Adapter1 Discoverable 
b true
pi@raspberry:~ $

这也可以通过语言绑定来完成。例如在 Python 中使用 pydbus:

pi@raspberry:~ $ python3
Python 3.7.3 (default, Jul 25 2020, 13:03:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pydbus
>>> bus = pydbus.SystemBus()
>>> dongle = bus.get('org.bluez', '/org/bluez/hci0')
>>> dongle.Discoverable
True
>>> dongle.Discoverable = False
>>> dongle.Discoverable
False
>>> 

如果你想捕捉变化,那么你需要连接到事件循环。例如:

from time import sleep
import pydbus
from gi.repository import GLib
bus = pydbus.SystemBus()
dongle = bus.get('org.bluez', '/org/bluez/hci0')

def print_change(iface, prop_changed, prop_removed):
    print('Adapter prop changed:', iface, prop_changed, prop_removed)

def toggle_discoverable(state):
    dongle.Discoverable = state


def toggle_power(state):
    dongle.Powered = state

dongle.onPropertiesChanged = print_change

mainloop = GLib.MainLoop()
GLib.timeout_add_seconds(3, toggle_discoverable, True)
GLib.timeout_add_seconds(5, toggle_discoverable, False)
GLib.timeout_add_seconds(8, toggle_power, False)
GLib.timeout_add_seconds(10, toggle_power, True)

try:
    mainloop.run()
except KeyboardInterrupt:
    mainloop.quit()

给出输出:

$ python3 print_bluez_change.py 
Adapter prop changed: org.bluez.Adapter1 {'Discoverable': True} []
Adapter prop changed: org.bluez.Adapter1 {'Discoverable': False} []
Adapter prop changed: org.bluez.Adapter1 {'Powered': False, 'Discovering': False, 'Class': 0} []
Adapter prop changed: org.bluez.Adapter1 {'Class': 4980736} []
Adapter prop changed: org.bluez.Adapter1 {'Powered': True} []