简体   繁体   English

Ubuntu设备特定的蓝牙小部件

[英]Ubuntu device-specific bluetooth widget

我想看看是否有一种方法可以使用命令bluetoothctl对外观非常简单的GUI(打开/关闭开关按钮)进行编程以连接到特定的蓝牙设备。

Here is how I've done it. 这是我的方法。

  1. A script to toggle connection 切换连接的脚本
  2. A menu shortcut to call the script 调用脚本的菜单快捷方式

The script, which I placed in ~/.local/bin (any place is good): 我放置在〜/ .local / bin中的脚本(任何地方都可以):

#!/usr/bin/env python3
from subprocess import check_output, call


def is_connected(device_mac):
    return b'Connected: yes\n' in check_output(['bluetoothctl', 'info', device_mac])

def connect(device_mac):
    print('connecting to bluetooth device {}'.format(device_mac))
    call(['bluetoothctl', 'connect', device_mac])

def disconnect(device_mac):
    print('disconnecting from bluetooth device {}'.format(device_mac))
    call(['bluetoothctl', 'disconnect', device_mac])

def main(device_mac):
    if is_connected(device_mac):
        disconnect(device_mac)
    else:
        connect(device_mac)

if __name__ == '__main__':
    main('04:5D:4B:E9:29:D4')

Find your device MAC address in the list of your added devices: 在添加的设备列表中找到设备的MAC地址:

$ bluetoothctl devices

And replace the MAC address '04:5D:4B:E9:29:D4' to your device's MAC address. 并将MAC地址'04:5D:4B:E9:29:D4'替换为设备的MAC地址。

Then I created a menu shortcut by creating a .desktop file. 然后,我通过创建.desktop文件来创建菜单快捷方式。 You can put it on your ~/Desktop to see it on your... desktop or/and in ~/.local/share/applications to have it available from the launcher menu. 您可以将其放在〜/ Desktop上,以在桌面上或/和〜/ .local / share / applications中查看它,以从启动器菜单中使用它。

[Desktop Entry]
Categories=utilitary
Comment=Connect or disconnect my headset Sony MDR 1000X
Exec="/home/aho/.local/bin/toggle-mdr-1000x.py"
Icon=/home/aho/.local/share/toggle-mdr-1000x/sony-mdr-1000x.png
Name=MDR-1000X
Terminal=false
Type=Application
Version=1.0

Now, when I open the dash menu with the super key / windows key and type 'mdr', the entry shows up, I press enter and BAM! 现在,当我使用超级键/ Windows键打开仪表板菜单并键入'mdr'时,将显示该条目,然后按Enter和BAM! , it connects / disconnects. ,它会连接/断开连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM