【问题标题】:Ubuntu 20.04.2 Bluetoothctl SCAN from bash script来自 bash 脚本的 Ubuntu 20.04.2 Bluetoothctl SCAN
【发布时间】:2021-04-29 17:40:31
【问题描述】:

Ubuntu 20.04.2 LTS bluetoothctl 版本 5.53

我遇到的问题是蓝牙耳机在双启动机器上的 Ubuntu/Windows 之间切换时无法直接配对而不出错(我相信是由于蓝牙问题/存储公钥的过程)

我正在尝试运行一个简单的脚本来删除特定的蓝牙地址(设备),刷新其信息并重新配对。

我尝试了以下各种迭代:

#!/bin/bash

#remove old connection 
echo -e 'power on\ndisconnect 88:88:E1:21:52:BE\nremove 88:88:E1:21:52:BE\nquit' | bluetoothctl

sudo systemctl restart bluetooth 

sleep 1
echo -e 'power on' | bluetoothctl
echo -e 'default-agent' | bluetoothctl
echo -e 'discoverable on\ndiscoverable-timeout 100\nscan on' | bluetoothctl
sleep 10
echo -e 'pairable on' | bluetoothctl

# Re-Add our device
echo -e 'trust 88:88:E1:21:52:BE\npair 88:88:E1:21:52:BE' | bluetoothctl
sleep 4
echo -e 'connect 88:88:E1:21:52:BE' | bluetoothctl
echo -e 'discoverable off' | bluetoothctl
echo -e 'quit' | bluetoothctl

输出如下:

Agent registered
[bluetooth]# power on
[bluetooth]# disconnect 88:88:E1:21:52:BE
Attempting to disconnect from 88:88:E1:21:52:BE
[bluetooth]# remove 88:88:E1:21:52:BE
[bluetooth]# quit
Agent registered
[bluetooth]# power on
Agent registered
[bluetooth]# default-agent
Agent registered
[bluetooth]# discoverable on
[bluetooth]# discoverable-timeout 100
[bluetooth]# scan on
Agent registered
[bluetooth]# pairable on
Agent registered
[bluetooth]# trust 88:88:E1:21:52:BE
Device 88:88:E1:21:52:BE not available
[bluetooth]# pair 88:88:E1:21:52:BE
Device 88:88:E1:21:52:BE not available
Agent registered
[bluetooth]# connect 88:88:E1:21:52:BE
Device 88:88:E1:21:52:BE not available
Agent registered
[bluetooth]# discoverable off
Agent registered
[bluetooth]# quit

但是,在单独的窗口中关注 bluetoothctl 命令输出时

[CHG] Controller C8:58:C0:C4:41:F8 Name: [ControllerID]
[CHG] Controller C8:58:C0:C4:41:F8 Alias: BlueZ 5.53
[CHG] Controller C8:58:C0:C4:41:F8 Alias: [ControllerID]
**[CHG] Controller C8:58:C0:C4:41:F8 Discovering: no**
[CHG] Controller C8:58:C0:C4:41:F8 Discoverable: yes

Scan on 命令没有达到预期的效果。蓝牙控制器未发现并且未发现任何设备,因此无法与所述设备建立连接。

当直接进入 bluetoothctl 命令行时,scan on 命令具有预期效果,控制器开始扫描。为什么 ubuntu 终端/bash 脚本中的 scan ON 命令没有达到预期的效果?

【问题讨论】:

  • SO 仅用于编程问题,因此操作系统支持为off-topic。请改用Ask Ubuntu 询问。
  • ` | bluetoothctl` 你正在调用这么多 bluetoothctl 进程。只需调用它一次,而不是多次。研究 shell 重定向和exec 文件描述符生成与进程替换和研究 bash 中的 coproc。

标签: bash ubuntu bluetooth


【解决方案1】:

我认为 bluetoothctl 从来没有打算以这种方式使用。

BlueZ 提供了一个 DBus API,记录在: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc

作为可以使用 API 完成的操作的示例,以下是 Python 中执行与您的脚本类似的操作的示例。

from gi.repository import GLib
from pydbus import SystemBus
from pprint import pprint

SCAN_TIME = 15
DEVICE_INTERFACE = 'org.bluez.Device1'
adapter_path = '/org/bluez/hci0'
device_address = '88:88:E1:21:52:BE'
dev_path = f'{adapter_path}/dev_{device_address.replace(":", "_").upper()}'


remove_list = set()

def stop_scan():
    adapter.StopDiscovery()
    mainloop.quit()


def clean_device(rm_dev):
    try:
        adapter.RemoveDevice(rm_dev)
    except GLib.Error as err:
        pass

def on_iface_added(path, interfaces):
    if DEVICE_INTERFACE in interfaces:
        on_device_found(path, interfaces[DEVICE_INTERFACE])

def on_device_found(device_path, device_props):
    address = device_props.get('Address')
    if address == device_address:
        stop_scan()

def pair_device():
    dev = bus.get('org.bluez', dev_path)
    dev.Trusted = True
    dev.Pair()


bus = SystemBus()
adapter = bus.get('org.bluez', adapter_path)
mngr = bus.get('org.bluez', '/')
mngr.onInterfacesAdded = on_iface_added

clean_device(dev_path)

mainloop = GLib.MainLoop()

GLib.timeout_add_seconds(SCAN_TIME, stop_scan)
adapter.SetDiscoveryFilter({'DuplicateData': GLib.Variant.new_boolean(True)})
adapter.StartDiscovery()

mainloop.run()

pair_device()

【讨论】:

  • python 脚本在 pair Command 上遇到问题。有点混乱,因为我们似乎为 bus.get() 提供了对象路径? --- 文件“bt_hs.py”,第 55 行,在 pair_device() 文件“bt_hs.py”,第 35 行,在 pair_device dev = bus.get('org.bluez', dev_path) 文件“/home /l/Desktop/scripts/bt_venv/lib/python3.8/site-packages/pydbus/proxy.py",第 59 行,在 get return CompositeInterface(introspection)(self, bus_name, object_path) KeyError: 'object does not export任何接口;您可能需要将对象路径作为 get()' 的第二个参数传递
  • 会翻阅该文档,也许我会意识到一些有用的概念。感谢您的链接。
猜你喜欢
  • 2021-09-12
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 2012-01-28
  • 2012-01-11
  • 1970-01-01
  • 1970-01-01
  • 2013-05-02
相关资源
最近更新 更多