一键搞定!为你的UOS创建蓝牙和WiFi快速开关脚本(附开机自启教程)
UOS系统蓝牙与WiFi一键管理从脚本封装到开机自启全攻略每次在UOS系统中手动打开设置界面切换蓝牙和WiFi状态对于追求效率的用户来说简直是时间黑洞。本文将带你从零开始打造一套完整的无线设备管理方案——通过简单的脚本封装复杂命令创建桌面快捷方式甚至实现开机自启让设备管理变得优雅高效。1. 理解UOS的无线设备管理机制UOS作为国产操作系统的代表其底层采用Deepin桌面环境无线设备管理主要通过D-Bus接口实现。与常见的rfkill或bluetoothctl命令不同UOS选择了一套更符合其架构的通信方式。**D-BusDesktop Bus**是Linux系统中用于进程间通信的机制它允许应用程序向其他程序提供服务并接收其他程序发出的信号。在UOS中蓝牙服务由com.deepin.daemon.Bluetooth提供WiFi服务由com.deepin.daemon.Network管理理解这一点很重要因为我们的脚本将直接与这些服务交互。原始命令虽然有效但存在几个痛点命令冗长难记需要手动识别设备路径如WiFi设备路径中的数字可能变化缺乏状态反馈机制2. 创建基础开关脚本我们先从最基础的脚本开始逐步构建完整解决方案。创建一个名为wireless_control.sh的文件内容如下#!/bin/bash # 蓝牙控制函数 bluetooth_control() { local action$1 if [ $action on ]; then gdbus call --session -d com.deepin.daemon.Bluetooth -o /com/deepin/daemon/Bluetooth -m com.deepin.daemon.Bluetooth.SetAdapterPowered /org/bluez/hci0 true elif [ $action off ]; then gdbus call --session -d com.deepin.daemon.Bluetooth -o /com/deepin/daemon/Bluetooth -m com.deepin.daemon.Bluetooth.SetAdapterPowered /org/bluez/hci0 false fi } # WiFi控制函数 wifi_control() { local action$1 local device_path$(dbus-send --session --destcom.deepin.daemon.Network --print-reply /com/deepin/daemon/Network org.freedesktop.DBus.Properties.Get string:com.deepin.daemon.Network string:Devices | awk /object path/ {print $3} | tr -d | head -1) if [ -z $device_path ]; then echo 无法找到WiFi设备路径 return 1 fi if [ $action on ]; then dbus-send --session --destcom.deepin.daemon.Network --print-reply /com/deepin/daemon/Network com.deepin.daemon.Network.EnableDevice variant:objpath:$device_path variant:boolean:true elif [ $action off ]; then dbus-send --session --destcom.deepin.daemon.Network --print-reply /com/deepin/daemon/Network com.deepin.daemon.Network.EnableDevice variant:objpath:$device_path variant:boolean:false fi } # 主逻辑 case $1 in bluetooth-on) bluetooth_control on ;; bluetooth-off) bluetooth_control off ;; wifi-on) wifi_control on ;; wifi-off) wifi_control off ;; *) echo 用法: $0 [bluetooth-on|bluetooth-off|wifi-on|wifi-off] exit 1 ;; esac这个脚本相比原始命令有几个重要改进自动获取WiFi设备路径不再硬编码/org/freedesktop/NetworkManager/Devices/9提供了清晰的用法说明将蓝牙和WiFi控制逻辑封装为函数便于维护提示给脚本添加执行权限chmod x wireless_control.sh3. 添加状态检测功能基础开关功能已经实现但优秀的工具应该能反馈当前状态。我们扩展脚本添加状态检测功能# 添加在wireless_control.sh中 get_bluetooth_status() { gdbus call --session -d com.deepin.daemon.Bluetooth -o /com/deepin/daemon/Bluetooth -m org.freedesktop.DBus.Properties.Get string:com.deepin.daemon.Bluetooth string:Adapters | grep -q true echo 开启 || echo 关闭 } get_wifi_status() { dbus-send --session --destcom.deepin.daemon.Network --print-reply /com/deepin/daemon/Network org.freedesktop.DBus.Properties.Get string:com.deepin.daemon.Network string:Devices | grep -q true echo 开启 || echo 关闭 } # 更新case语句 case $1 in # ...原有case... bluetooth-status) get_bluetooth_status ;; wifi-status) get_wifi_status ;; status) echo 蓝牙状态: $(get_bluetooth_status) echo WiFi状态: $(get_wifi_status) ;; esac现在你可以使用以下命令获取状态./wireless_control.sh bluetooth-status ./wireless_control.sh wifi-status ./wireless_control.sh status # 同时查看两者状态4. 创建桌面快捷方式终端命令虽然强大但不够直观。我们为UOS桌面创建图形化快捷方式。4.1 创建.desktop文件在~/.local/share/applications/目录下创建以下文件蓝牙开关.desktop[Desktop Entry] Name蓝牙开关 Exec/path/to/wireless_control.sh bluetooth-toggle Iconbluetooth TypeApplication CategoriesUtility;WiFi开关.desktop[Desktop Entry] NameWiFi开关 Exec/path/to/wireless_control.sh wifi-toggle Iconnetwork-wireless TypeApplication CategoriesUtility;注意需要先实现toggle功能见下一节4.2 添加切换(toggle)功能更新脚本添加toggle功能# 在wireless_control.sh中添加 case $1 in # ...原有case... bluetooth-toggle) if [ $(get_bluetooth_status) 开启 ]; then bluetooth_control off else bluetooth_control on fi ;; wifi-toggle) if [ $(get_wifi_status) 开启 ]; then wifi_control off else wifi_control on fi ;; esac现在双击桌面图标就能切换设备状态了5. 实现开机自启动为了让某些设备在开机时自动进入特定状态比如自动开启WiFi我们需要配置开机自启动。5.1 创建自启动脚本创建~/.config/autostart/wireless_startup.desktop[Desktop Entry] Name无线设备初始化 Exec/path/to/wireless_control.sh wifi-on TypeApplication Hiddenfalse X-GNOME-Autostart-enabledtrue5.2 系统级自启动需要管理员权限如果需要为所有用户设置创建/etc/xdg/autostart/wireless_startup.desktop需要sudo权限[Desktop Entry] Name无线设备初始化 Exec/path/to/wireless_control.sh wifi-on /path/to/wireless_control.sh bluetooth-off TypeApplication Hiddenfalse X-GNOME-Autostart-enabledtrue这个例子实现了开机自动开启WiFi并关闭蓝牙。6. 高级功能扩展6.1 添加通知提示使用notify-send命令让操作更有反馈感# 在控制函数中添加通知 bluetooth_control() { # ...原有代码... if [ $action on ]; then gdbus call --session ... true notify-send 蓝牙 已开启 -i bluetooth # ...其余代码... }6.2 创建键盘快捷键在UOS设置中可以添加自定义快捷键打开控制中心 键盘和语言 快捷键添加自定义快捷键名称切换蓝牙命令/path/to/wireless_control.sh bluetooth-toggle设置喜欢的快捷键组合如CtrlAltB6.3 定时控制结合cron实现定时控制比如每天晚上11点自动关闭无线设备# 编辑crontab crontab -e # 添加以下行 0 23 * * * /path/to/wireless_control.sh wifi-off /path/to/wireless_control.sh bluetooth-off7. 错误处理与日志记录完善的脚本应该具备错误处理和日志功能# 在脚本开头添加 LOG_FILE/tmp/wireless_control.log log() { echo [$(date %Y-%m-%d %H:%M:%S)] $ $LOG_FILE } # 在关键操作处添加日志记录 wifi_control() { log 尝试$1 WiFi # ...原有代码... if [ $action on ]; then if dbus-send ...; then log WiFi已开启 else log WiFi开启失败 return 1 fi # ...其余代码... }这样可以在/tmp/wireless_control.log中查看操作历史便于排查问题。