Skip to content

Instantly share code, notes, and snippets.

@gingerbeardman
Last active October 23, 2023 23:37
Show Gist options
  • Save gingerbeardman/05030c73714b3aa3202aeee7f21c3b1e to your computer and use it in GitHub Desktop.
Save gingerbeardman/05030c73714b3aa3202aeee7f21c3b1e to your computer and use it in GitHub Desktop.
Automatically pair a Wii Remote to macOS using blueutil command-line tool (Intel-only? fails on Apple silicon)
#!/usr/bin/env bash
# requirements: blueutil
echo "Hold both 1+2 buttons on Wii Remote and wait for 30 seconds"
# get MAC Address of Wii Remote
MAC=`$(which blueutil) --inquiry 5 2>&1 | grep 'address' | awk '{print substr($2,1,17)}'`
(killall -m 'blueutil*' 2>&1) >/dev/null
echo "MAC: $MAC"
# prepare HEX
HEX=`echo $MAC | tr -d '-'`
# PIN is MAC Address HEX pairs in reverse order
PIN="\x${HEX:10:2}\x${HEX:8:2}\x${HEX:6:2}\x${HEX:4:2}\x${HEX:2:2}\x${HEX:0:2}"
# try to PAIR with PIN
echo "ATTEMPTING PAIR"
echo "$PIN" | $(which blueutil) --pair $MAC >/dev/null 2>&1
# fail?
if [ $? -ne 0 ]; then
echo "PAIR FAILED"
else
# now we can CONNECT
echo "ATTEMPTING CONNECT"
$(which blueutil) --connect $MAC >/dev/null 2>&1
fi
@gingerbeardman
Copy link
Author

gingerbeardman commented Mar 20, 2023

macOS 12 Monterey

  • works on Intel
  • fails on Apple silicon (example output below)
$ ./pair.sh
Hold 1+2 on Wii Remote and wait 30 seconds
MAC: 00-19-1d-92-4b-c0
ATTEMPTING PAIR
PAIR FAILED

@lin12777
Copy link

lin12777 commented Jun 11, 2023

Monterey 12.6.6 Intel
Hold both 1+2 buttons on Wii Remote and wait for 30 seconds
MAC: ...
ATTEMPTING PAIR
PAIR FAILED
🥲

@gingerbeardman
Copy link
Author

gingerbeardman commented Jun 11, 2023

Try again @lin12777

Also, if you want to try a GUI for it there is this: https://stemed.online/wiimote/

@lin12777
Copy link

Got this error using blueutil with correct MAC Address and PIN
blueutil --pair ID [PIN]
2023-06-11 14:20:33.502 blueutil[4612:235878] -[IOBluetoothDevicePair peerPairingCompleted:withError:]: Calling connectionComplete on IOBluetoothDevicePair. Status: 31
Failed to pair "00-27-09-36-b3-57" with error 0x1f (Unspecified Error)

@gingerbeardman
Copy link
Author

gingerbeardman commented Jun 11, 2023

Sorry, @lin12777, there was a small error in the script that meant the PIN was not sent correctly.

Also, the pin cannot be passed the way you are doing, blueutil --pair ID [PIN]. It needs to be echo'd because it contains characters that would be interpreted as system control characters. It is not ASCII or UTF8 so it cannot be copy and pasted either.

Please try again.

@Confuzzer
Copy link

image
I'm having this problem, is there any way to fix it? I'm on Monetary 12.6.9 on an Intel mac. Is there any place to see an error output?

@gingerbeardman
Copy link
Author

gingerbeardman commented Sep 20, 2023

I'm not sure why you are seeing 4 addresses rather than just one.

You can run blueutil manually, but entering the pin (which consists of characters that cannot be typed) requires a script or program. If it failed, it's a macOS thing.

You can also try the GUI at https://stemed.online/wiimote/

@Confuzzer
Copy link

Confuzzer commented Sep 22, 2023

Would there be a way to just get the pin without having to go through blueutil with the MAC address? Specifically this part of the script: PIN="\x${HEX:10:2}\x${HEX:8:2}\x${HEX:6:2}\x${HEX:4:2}\x${HEX:2:2}\x${HEX:0:2}"
I had some security concerns when working with the STEM GUI (I'm fairly certain the utility downloaded some files to my computer without me knowing), so I'm going to stick with this tool for now. I might be a bit paranoid but I'd just prefer to use this one so I know what's going on under the hood.

@gingerbeardman
Copy link
Author

gingerbeardman commented Sep 22, 2023

The pin is each pair of the MAC address in reverse. But the resulting information is not human readable and cannot be typed using a keyboard because it's out of the range of the alphabet. Which is why it's managed here by a script.

It's a shame Nintendo picked such a pin. And it's a shame macOS has removed most support for Nintendo controllers.

@Noah40
Copy link

Noah40 commented Oct 23, 2023

I by no means know shell script well but i tried to hardcode in the MAC address here because blueutil was acting weirdly. Anyways I get the error Failed to pair "d8-6b-f7-db-a2-5e" with error 0x1f (Unspecified Error)

Here is the code:
#!/usr/bin/env bash

MAC="d8-6b-f7-db-a2-5e"

(killall -m 'blueutil*' 2>&1) >/dev/null
echo "MAC: $MAC

PIN="\xe5\x2a\xbd\x7f\xb6\x8d"

echo "ATTEMPTING PAIR"
echo -n -e "$PIN" | $(which blueutil) --pair $MAC

if [ $? -ne 0 ]; then
echo "PAIR FAILED"
else
echo "ATTEMPTING CONNECT"
$(which blueutil) --connect $MAC
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment