Page 1 of 1

wiimote address to PIN

Posted: Sat Sep 15, 2018 8:24 am
by devnull
This is not directly related to micropython, but I am hoping someone can help.

I am trying to pair a wimmote on a linux box, bu the problem is bluetoothctl insists that I enter a pin number and the device is not provided with one.

So I understand that it can be constructed from reversing the device address, now I know nothing at all about C++ and assume that the following code is in C++.

Any chance someone can help me to convert this into python ? - I guess this is a bytearray:

Code: Select all

a = bytearray(b'\x6D\x7E\x3B\x35\x1E\x00')
but how do I convert that into a printable string, given that these hex values seem to be too high for printable chars

This is the code I found:

Code: Select all

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.
I also found this, but attempting to run it in repl.it just throws loads of errors that I simply don't understand :-(

Code: Select all

_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;
}

Re: wiimote address to PIN

Posted: Sat Sep 15, 2018 10:27 am
by OutoftheBOTS_
I would be interested in hearing others answers on this as I am a bit of a noob myself.

As far as I can tell the code you posted is python code not C++ code.

As far as I am aware all 3 of the following lines do the same thing, make an array of bytes in memory with these values stored in it.

Code: Select all

a = bytearray(b'\x6D\x7E\x3B\x35\x1E\x00')
a = bytearray([0x6D, 0x7E, 0x3B, 0x35, 0x1E, 0x00])
a = b'\x6D\x7E\x3B\x35\x1E\x00'
As far as I am aware C++ code would look like this

Code: Select all

unsigned char a[6] = {0x6D, 0x7E, 0x3B, 0x35, 0x1E, '\0'};

Re: wiimote address to PIN

Posted: Sat Sep 15, 2018 10:59 am
by devnull
Thanks so much for helping but I have obviously not explained very well.

This is the code I found in C++ that I would like to convert to Python:

Code: Select all

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.


_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;
}

And this is my code, where I am trying to emulate in python what I have found in C++

Code: Select all

a = bytearray(b'\x6D\x7E\x3B\x35\x1E\x00')
But I cannot fathom out what the C++ code snippets are doing as I know nothing about C++

Re: wiimote address to PIN

Posted: Sat Sep 15, 2018 6:08 pm
by jickster
You want to know what that C++ function is doing?


Sent from my iPhone using Tapatalk Pro

Re: wiimote address to PIN

Posted: Sat Sep 15, 2018 8:39 pm
by OutoftheBOTS_
I am not sure if in python you will need the null char at the end of the string. In C you have to have this as string handing fuctions usually go through each byte in a string till the find the null char and that signals to stop, I assume python will have it's own way of doing this automatically and you shouldn't need to manually add the null char.

Re: wiimote address to PIN

Posted: Sat Sep 15, 2018 9:42 pm
by pagano.paganino
try this:

Code: Select all

a = bytearray(b'\x6D\x7E\x3B\x35\x1E\x00')
":".join("%02x" % b for b in reversed(a))