Skip to content

Instantly share code, notes, and snippets.

@matteobarbieri
Created March 31, 2021 12:26
Show Gist options
  • Save matteobarbieri/cd0cf3318ceda8dc77d588ca6be926ee to your computer and use it in GitHub Desktop.
Save matteobarbieri/cd0cf3318ceda8dc77d588ca6be926ee to your computer and use it in GitHub Desktop.
Keychron K4 pairing script
#!/bin/bash
####################
### INSTRUCTIONS ###
####################
# 1. To speed up things, switch to bt mode and select the right channel (it will make things easier in the next steps)
# 2. Switch back to USB mode (so that you can actually enter commands)
# 3. Run this script and then quickly switch to bt mode and once the blinking stops hold FN + the corresponding bt channel;
# for instance, I have paired it with Windows on channel 1, and on Linux on channel 2, so in my case it's FN + 2. Release it
# as soon as the channel key starts slowly blinking (the device is in pair mode).
# 4. Wait until the process has completed, it will take like 20 seconds.
# 5. Done!
# Put your keychron's MAC address here
# (it might change based on which channel it's using, not sure)
K4_ADDRESS="AA:BB:CC:11:22:33"
# Countdown to give time to users to switch to bluetooth and start the pairing
for (( i = 5; i > 0; i-- )); do
echo $i
sleep 1
done
coproc bluetoothctl
echo -e "remove ${K4_ADDRESS}\n" >&${COPROC[1]}
# Initiate scan
echo -e "scan on\n" >&${COPROC[1]}
sleep 10
echo -e "pair ${K4_ADDRESS}\n" >&${COPROC[1]}
sleep 5
echo -e "connect ${K4_ADDRESS}\n" >&${COPROC[1]}
sleep 3
echo -e "info ${K4_ADDRESS}\nexit" >&${COPROC[1]}
output=$(cat <&${COPROC[0]})
echo $output | sed 's/\x1B\[[0-9;]*[JKmsu]//g; s/\r/\n/g; s/Paired/\nPaired/g;
s/Trusted/\nTrusted/g; s/Connected/\nConnected/g; s/Legacy/\nLegacy/g '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment