Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Stop bluetooth from turning on everytime the computer wakes up
# Put the .service files in /etc/systemd/system/
# and the btstate file in /usr/local/bin/
# Then run these commands.
sudo chmod +x /usr/local/bin/btstate
sudo systemctl enable btstate.service
sudo systemctl enable btstate-startonwake.service
# You might have to change "hci0" in the /usr/local/bin/btstate file to
# whatever your bluetooth device is called. You can find it by running
# hciconfig. hci0 is what it's called on my system.
#!/bin/bash
if [ ! -d "/usr/local/share/btstate" ] || [ ! -f "/usr/local/share/btstate/state" ] || [ ! -f "/usr/local/share/btstate/log" ]; then
mkdir -p /usr/local/share/btstate
touch /usr/local/share/btstate/state
touch /usr/local/share/btstate/log
fi
if [[ $1 == 'save' ]]; then
hciconfig hci0 | awk '/UP|DOWN/ {$1=$1;print tolower($1)}' > /usr/local/share/btstate/state
#printf "$(date): saved\n" >> /usr/local/share/btstate/log
elif [[ $1 == 'load' ]]; then
hciconfig hci0 `cat /usr/local/share/btstate/state`
#printf "$(date): loaded\n" >> /usr/local/share/btstate/log
else
echo "Usage: btstate <save|load>"
#printf "$(date): run without command\n" >> /usr/local/share/btstate/log
ficomputer
[Unit]
Description=Starts btstate again on wake
After=suspend.target
[Service]
Type=simple
ExecStart=/usr/sbin/service btstate start
[Install]
WantedBy=suspend.target
[Unit]
Description=Save/load bluetooth state
After=multi-user.target bluetooth.service
Conflicts=suspend.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/btstate load
ExecStop=/usr/local/bin/btstate save
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment