Ubuntu22.04 蓝牙设备快速自动连接

Ubuntu22.04 蓝牙设备快速自动连接

蓝牙鼠标在多台电脑切换时,可能需要等好久才能自动连接上,特别是ubuntu系统,Windows其实这点做点挺好的。所以在ubuntu系统中,不能光靠系统自动识别使用过的设备,额外加一个自动连接的程序在后台,效果很好,而且不会占用太多资源。

1. 后台蓝牙自动连接程序

1) 先确认蓝牙设备的硬件地址

在系统设置里,可以找到蓝牙设备的MAC地址,如下图:

2)创建.sh文件

先在terminal运行sudo gedit /usr/local/bin/bt-mouse-autoconnect.sh创建.sh文件

然后写入如下内容:

sh 复制代码
#!/usr/bin/env bash
set -euo pipefail

# MAC address
MAC="D5:D7:73:79:2E:15"

# power on bluetootch control
bluetoothctl power on >/dev/null 2>&1 || true

bluetoothctl trust "$MAC" >/dev/null 2>&1 || true

# connect
bluetoothctl connect "$MAC" >/dev/null 2>&1 || exit 1
EOF

要把里面的MAC地址换成需要对应的蓝牙设备

3)修改权限
sudo chmod +x /usr/local/bin/bt-mouse-autoconnect.sh

4)创建 systemd 服务(开机后自动跑,失败自动重试)

在terminal运行sudo gedit /etc/systemd/system/bt-mouse-autoconnect.service

然后输入

sh 复制代码
[Unit]
Description=Auto connect Bluetooth mouse
After=bluetooth.service network.target
Requires=bluetooth.service

[Service]
Type=oneshot
ExecStart=/usr/local/bin/bt-mouse-autoconnect.sh
RemainAfterExit=yes
# 如果没连上就重试
Restart=on-failure
RestartSec=3

[Install]
WantedBy=multi-user.target

最后在terminal运行下面连个指令:
sudo systemctl daemon-reload
sudo systemctl enable --now bt-mouse-autoconnect.service

5)查看运行情况
systemctl status bt-mouse-autoconnect.service
journalctl -u bt-mouse-autoconnect.service -b --no-pager

有输出反馈后,可以试着来回切换蓝牙鼠标的连接设备,看看效果,应该就可以快速连接了,减少等待时间,提高牛马工作效率 :)

Happy new year!

相关推荐
苏三福17 小时前
ubuntu 在文件管理系统中用sftp打开远程文件空白的问题解决
linux·运维·ubuntu
Anesthesia丶18 小时前
llama-server编译部署+opencode验证记录(Ubuntu, GPU)
ubuntu·opencode·llama-server·qwen3.8-9b
Sunqk566519 小时前
将开发板串口连接到Ubuntu后未找到对应的设备文件?
linux·运维·ubuntu
YHHLAI19 小时前
TypeScript 面试题:type 与 interface 的区别与相同点
java·ubuntu·typescript
玖石书3 天前
Ubuntu 更换 USTC 镜像源完整指南(AMD64 / AARCH64)
linux·运维·ubuntu
牢姐与蒯3 天前
Linux进程(五).之环境变量
linux·运维·服务器·ubuntu
黑臂麒麟3 天前
Harmony鸿蒙实战应用4:随手账本——编辑与删除账单
ubuntu·华为·鸿蒙
玖石书3 天前
WSL2 手动安装 Ubuntu 24.04 到指定磁盘位置
linux·运维·ubuntu·wsl
冰雪青松3 天前
Ubuntu 运维命令大全:从入门到精通的实战参考手册
运维·服务器·ubuntu
feilieren4 天前
ubuntu 安装 docker
linux·ubuntu·docker