Loading

解决-黑苹果-睡眠唤醒-蓝牙不正常工作、睡死

黑苹果睡眠没问题,出现唤醒之后蓝牙不正常工作现象,比如可以连接蓝牙键盘但是键盘快捷键很多失效,连接无线蓝牙耳机一直转圈,表现为蓝牙睡死症状
临时解决方法:重新打开蓝牙可以暂时解决问题
一劳永逸方法:使用sleepwatcher+blueutil,实现睡眠之前关闭蓝牙,睡醒之后打开蓝牙

准备工作

  1. 下载sleepwatcher
    推荐官网直接下载,地址: https://www.bernhard-baehr.de/
  2. 安装blueutil
    blueutil是一个蓝牙工具,可以通过命令打开和关闭蓝牙
    推荐使用homebrew安装:brew install blueutil

安装sleepwatcher

cd sleepwatcher_2.2.1
sudo cp ./sleepwatcher /usr/local/sbin
# 如果/usr/local/share/man下没有man8,手动创建一个man8文件夹
sudo cp ./sleepwatcher.8 /usr/local/share/man/man8

#创建数据文件夹,这个可以自定义,如果选择不同的路径,下面的路径也必须跟着修改
mkdir ~/.sleep

启动蓝牙和关闭蓝牙脚本

关闭蓝牙脚本,用于睡眠时关闭蓝牙:vim ~/.sleep/rc.sleep

#!/bin/bash
# rc.sleep
# Stop Bluetooth Module on Mac OS X
#
# Requires Blueutil to be installed: http://brewformulas.org/blueutil

BT="/usr/local/bin/blueutil"

log() {
	# logger -p notice -t bt_restarter "$@"
	echo "$@" >> ~/.sleep/sleepwatcher.log
}

err() {
	echo "$@" >&2
	echo "$@" >> ~/.sleep/sleepwatcher.log
	# logger -p error -t bt_restarter "$@"
}

log ""
log "sleep at $(date +"%Y-%m-%dT%H:%M:%S")"
if [ -f "$BT" ]; then
	if [[ $("$BT" -p) == 0 ]]; then
		log "Bluetooth is off, nothing to do."
	else
		log "Bluetooth on, stopping ..."
		($("$BT" -p 0) &> /dev/null && echo "Bluetooth Module stopped") || (err "Couldn't stop Bluetooth Module" && exit 1) 
		log "Successfully stoped Bluetooth" && exit 0
	fi
else
	err "Couldn't find blueutil, please install http://brewformulas.org/blueutil" && exit 1
fi

开启蓝牙脚本,用于唤醒时打开蓝牙:vim ~/.sleep/rc.wakeup

#!/bin/bash
# rc.wakeup
# Restart Bluetooth Module on Mac OS X
#
# Requires Blueutil to be installed: http://brewformulas.org/blueutil

BT="/usr/local/bin/blueutil"

log() {
	# logger -p notice -t bt_restarter "$@"
	echo "$@" >> ~/.sleep/sleepwatcher.log
}

err() {
	echo "$@" >&2
	echo "$@" >> ~/.sleep/sleepwatcher.log
	# logger -p error -t bt_restarter "$@"
}

log "wakeup at $(date +"%Y-%m-%dT%H:%M:%S")"
if [ -f "$BT" ]; then
	if [[ $("$BT" -p) == 0 ]]; then
		log "Bluetooth is off, Starting Bluetooth..."
		($("$BT" -p 1) &> /dev/null && echo "Bluetooth Module started") || (err "Couldn't start Bluetooth Module" && exit 1) 
	else
		log "Bluetooth on, restarting ..."
		($("$BT" -p 0) &> /dev/null && echo "Bluetooth Module stopped") || (err "Couldn't stop Bluetooth Module" && exit 1)
		($("$BT" -p 1) &> /dev/null && echo "Bluetooth Module started") || (err "Couldn't start Bluetooth Module" && exit 1) 
		log "Successfully restarted Bluetooth" && exit 0
	fi
else
	err "Couldn't find blueutil, please install http://brewformulas.org/blueutil" && exit 1
fi

给脚本添加可执行权限

cd ~/.sleep
chmod u+x rc.sleep rc.wakeup

创建plist启动文件:vim ~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>de.bernhard-baehr.sleepwatcher</string>
	<key>ProgramArguments</key>
	<array>
		<string>/usr/local/sbin/sleepwatcher</string>
		<string>-V</string>
		<string>-s ~/.sleep/rc.sleep</string>
		<string>-w ~/.sleep/rc.wakeup</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
	<key>KeepAlive</key>
	<true/>
</dict>
</plist>

最后,执行命令激活启动plist

执行完后,可能会弹窗请求授权,这时正常通过即可,这时你可以直接直接选择睡眠和唤醒,查看效果。

launchctl load ~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher.plist

可以通过cat ~/.sleep/sleepwatcher.log查看睡眠和唤醒时启动与关闭蓝牙的日志

本文转载至:https://wiyi.org/fixed-sleep.html
特此记录,以作备忘

posted @ 2023-03-23 22:49  MrSu  阅读(2897)  评论(2编辑  收藏  举报