Ubuntu 20.04.2来自bash脚本的Bluetoothctl SCAN



Ubuntu 20.04.2 LTSbluetoothctl 5.53版

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

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

我尝试过各种迭代:

#!/bin/bash
#remove old connection 
echo -e 'power onndisconnect 88:88:E1:21:52:BEnremove 88:88:E1:21:52:BEnquit' | bluetoothctl
sudo systemctl restart bluetooth 
sleep 1
echo -e 'power on' | bluetoothctl
echo -e 'default-agent' | bluetoothctl
echo -e 'discoverable onndiscoverable-timeout 100nscan on' | bluetoothctl
sleep 10
echo -e 'pairable on' | bluetoothctl
# Re-Add our device
echo -e 'trust 88:88:E1:21:52:BEnpair 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

"打开扫描"命令没有达到所需的效果。蓝牙控制器没有发现任何设备,因此无法连接到所述设备。

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

我不认为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()

相关内容

  • 没有找到相关文章

最新更新