Convert Wiimote MAC Address to PIN from C++ to Python or Javascript

5.4k Views Asked by At

In have found the following snippet posted here: http://wiibrew.org/wiki/Wiimote#Bluetooth_Pairing

There is also another code snippet to convert an Address to pin number here: https://www.richlynch.com/2009/11/18/permanently-pair-wiimotewii-balance-board/

What this is attempting to do is to convert the mac address of a nintendo wiimote into a pin number that is requested during bluetooth pairing.

I am guessing that this is C or C++ but know nothing at all about C / C++, but I do know javascript and python.

I would like to convert this code into javascript or python so I can generate a valid STRING pin number that can be entered when prompted during bluetooth pairing, or even do it manually once I understand what exactly this code does.

This is the original snippit:

Lets assume the Wiimote has the bluetooth address "00:1E:35:3B:7E:6D". If you want the PIN for bluetooth pairing in a simple string, do the following:

char pin[6];
pin[0] = 0x6D;
pin[1] = 0x7E;
pin[2] = 0x3B;
pin[3] = 0x35;
pin[4] = 0x1E;
pin[5] = 0x00;

Now "pin" contains your bluetooth pin that should be used for pairing your devices.

Another snippit using a function:

_TCHAR * FormatBTAddress(BLUETOOTH_ADDRESS address)
{
   static _TCHAR ret[20];
   _stprintf(ret, _T("%02x:%02x:%02x:%02x:%02x:%02x"),
      address.rgBytes[5],
      address.rgBytes[4],
      address.rgBytes[3],
      address.rgBytes[2],
      address.rgBytes[1],
      address.rgBytes[0]
      );
   return ret;
}

Now although I know nothing about C++, I know that this is using some kind of (reversed) 6 byte array, and when I run this code using the MAC address in the sample on repl.it it does produce what appears to be a human readable 4 character string which could be entered as a bluetooth Pin:

m~;5

However, when I replace the original mac address with my actual mac address, it generates unreadable characters and it seems to me that this code cannot actually work and there must be something omitted by the original poster on wiibrew.org.

This is using my actual MAC Address:

  char xpin[6];
  xpin[0] = 0x03;
  xpin[1] = 0xF4;
  xpin[2] = 0xB9;
  xpin[3] = 0x6E;
  xpin[4] = 0xAE;
  xpin[5] = 0xB8;
  printf(xpin);

And this is the result, which includes unprintable characters that definitely not could not be entered when prompted as a valid Bluetooth PIN number:

��n�� @ 

Can anyone throw any light on this, as I cannot possibly see how the code provided could create a human readable string that can be used as a bluetooth pin number ??

Ultimately I would like to code this in Javascript or Python, but even being able to do this manually and generate valid strings from mac addresses would be helpful.

1

There are 1 best solutions below

0
On

Referring to https://wiibrew.org/wiki/Wiimote:

The PIN-Code is the binary bluetooth address of the wiimote backwards.

Below seems to be an updated version of what was said above (same source):

If connecting by holding down the 1+2 buttons, the PIN is the bluetooth address of the wiimote backwards, if connecting by pressing the "sync" button on the back of the wiimote, then the PIN is the bluetooth address of the host backwards.

So both original C and your Python code is correct. In fact, resulting string is not supposed to be readable at all (and always have 6 characters, printable or not). It's up to WiiMote driver to invert MAC string and send it back, as is everything described on that page. So if you are not writing WiiMote driver, find one for your system (on Linux it is already included as BlueZ plugin and should work out-of-box).