首页 > 其他分享 >解决-黑苹果-睡眠唤醒-蓝牙不正常工作、睡死

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

时间:2023-03-23 23:01:55浏览次数:66  
标签:log 蓝牙 Bluetooth sleep blueutil 睡死 sleepwatcher 唤醒

黑苹果睡眠没问题,出现唤醒之后蓝牙不正常工作现象,比如可以连接蓝牙键盘但是键盘快捷键很多失效,连接无线蓝牙耳机一直转圈,表现为蓝牙睡死症状
临时解决方法:重新打开蓝牙可以暂时解决问题
一劳永逸方法:使用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
特此记录,以作备忘

标签:log,蓝牙,Bluetooth,sleep,blueutil,睡死,sleepwatcher,唤醒
From: https://www.cnblogs.com/suguangti/p/17249839.html

相关文章

  • Java远程唤醒mac
    关机的,大家都会……想比我就不说了吧!通过IP,使用shutdown命令,外加参数……附上地址,OK。packagecom.iaiai.test;importjava.net.DatagramPacket;i......
  • 简析开源鸿蒙蓝牙能力
    作者:王石蓝牙功能是无线短距的重要能力,在工作、生活中有很多蓝牙设备,比如车载蓝牙设备,蓝牙耳机,蓝牙键盘。1994年由电信商爱立信发展出这个技术,最初蓝牙的设计是系统创建出......
  • 306-Air724UG(4G全网通GPRS)开发-闹钟关机,定时唤醒,引脚唤醒
    <p><iframename="ifd"src="https://mnifdv.cn/resource/cnblogs/LearnAir724UG"frameborder="0"scrolling="auto"width="100%"height="1500"></iframe></p>  1......
  • Python3使用WOL唤醒群晖NAS
    1.pip3installwakeonlanpip3installwakeonlanLookinginindexes:https://pypi.tuna.tsinghua.edu.cn/simpleCollectingwakeonlanDownloadinghttps://pypi.t......
  • 在Mac OS X中使用WOL命令唤醒群晖NAS
    1.安装wakeonlan(base)➜~brewinstallwakeonlanUpdatingHomebrew...==>Downloadinghttps://ghcr.io/v2/homebrew/core/wakeonlan/manifests/0.41###########......
  • arduino 蓝牙模块测试
    今天在chatgpt的高效查询下顺利地测试了arduino的蓝牙功能: 蓝牙模块(JDY-31):原材料ArduinoUNO开发板x1蓝牙模块JDY-31x1子母杜邦线若干马达/LED灯x1(本次用的......
  • 蓝牙-低功耗中心设备
    //11.开启蓝牙适配器openBluetoothAdapter//21.开始搜索蓝牙设备startBluetoothDevicesDiscovery//31.开启监听搜索蓝牙设备onBluetoothDeviceFound//30.......
  • 收到微信视频邀请时唤醒屏幕-AHK
    loop{ifWinExist("ahk_classVoipTrayWnd"){send,{space}send,{enter}loop{If!WinExist("ahk_classVoipTrayWnd"){......
  • “ZETA+蓝牙BEACON”的资产定位产品方案,解决固定资产管理难题
    资产管理是企业管理中的一个重要组成部分。在未来商业竞争中,企业不仅要知道资产的位置、运行状况,还要提升资产的运行效率,这些都催生了企业对资产定位和利用科技提升管理效率......
  • C# 在PC上的通过蓝牙(bluetooth)发送数据到手机
    C#在PC上的通过蓝牙(bluetooth)发送数据到手机2023-01-2709:32·opendotnet概述在PC端用.NET开发一个蓝牙下载的程序。实现在PC上查找周围的蓝牙设备(主要是手机),并将......