tmi
Posts: 5
Joined: Tue Mar 07, 2006 10:35 pm

need help converting pin for use in Bond command

Mon Mar 13, 2006 11:43 pm

Hello,
I am having trouble figuring out how to do this. It may be a problem in the BlueTools Bond function or most likely an error in my conversion.

The bluetooth device I am trying to bond with has a factory default pin code of the bytes 0x3F 0x8A 0x45 which converts to a question mark, a square, and a capital E. I have a feeling that the middle character is the problem.

This is how I convert the bytes from a byte array to a string (i am using C#):
strPin = (char)factorypin[0] +""+ (char)factorypin[1] +""+ (char)factorypin[2];

The bond fails with Authentication failure.

I am able to bond with other devices succesfully that have already had their pin codes changed via our old software.

Thanks

User avatar
johan
GpsGate
Posts: 15403
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: need help converting pin for use in Bond command

Tue Mar 14, 2006 6:34 pm

The problem is most likely the 0x8A value, that value is probably changed when casted to a .NET (unicode) char.

Try this:

Code: Select all

// Converts a byte array to a unicode string
// 
string ByteArrayToString(byte[] byte_arr, int bytes_len)
{
	char[] char_arr = new char[bytes_len];
	byte b = 0;

	for(int inx = 0; inx < bytes_len; inx++)
	{
		b = byte_arr[inx];
		char_arr[inx] = (char) (b != 0 ? b : 256);
	}
	return new string(char_arr);
}

// How to use the method:
void test()
{
    byte arrayPin[3];
    arrayPin[0] = 0x3F;
    arrayPin[1] = 0x8A;
    arrayPin[2] = 0x45;

    string strPin = ByteArrayToString(pinArray, pinArray.Length);

    // Use strPin...
}
I realize we should add a version of Bond() and BondAsync() that takes a byte array as argument as well. We'll probably include that in the next version of BlueTools.

Regards,
Johan

Franson Support

tmi
Posts: 5
Joined: Tue Mar 07, 2006 10:35 pm

RE: need help converting pin for use in Bond command

Tue Mar 14, 2006 6:58 pm

Thank you Johan, but your code still results in an Authentication failure.

Here is a little more backstory:
-First I do a regular DiscoverDevices which returns a Device array.
-Then I loop through the array and use the Bonded property to test if a device is bonded.
-If it is not bonded, I try to bond with the factory pin using Bond.
This is where I get my error.

Am I missing something? Is there an issue because I am not using async versions?

User avatar
johan
GpsGate
Posts: 15403
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: need help converting pin for use in Bond command

Tue Mar 14, 2006 7:04 pm

Maybe the pin code is incorrect in the first place. Or maybe the device you try to bond with is not working in some way.

It is very unusual to have a pin code that is not writeable from a keyboard, which makes me think you might have misunderstood which pin code to really use.

Some things to test:
-> Try to bond to your device by some other meens to see if it works
-> Check the PIN code
-> Ask the manufacturer of the device what the reason behind a none writable pin charachter is.

Regards,
Johan

Franson Support

tmi
Posts: 5
Joined: Tue Mar 07, 2006 10:35 pm

RE: need help converting pin for use in Bond command

Wed Mar 15, 2006 6:55 pm

I am certain that the 3 byte pincode is correct. We are also the manufacturer of the device. We picked this pincode so that nobody could connect to our units without our software. We manufacture GPS vehicle tracking devices.

The device does successfully bond using our old software. The software was written in C++ and is a bit over my head. I am unable to track down specifically in the code where the passing of our factory pin takes place.

User avatar
johan
GpsGate
Posts: 15403
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: need help converting pin for use in Bond command

Wed Mar 15, 2006 7:01 pm

Maybe you have made your device a little bit to secure [:)]

Since it is so uncommon to use none printable charachters as pin, I can imagine that not many Bluetooth stacks are tested to work with them. I am not even sure if the BT standard allows (or encourage) the use of non printable chars.

since you have control of the device, you have at least the chance to test if that is the case or not.

Regards,
Johan

Franson Support

tmi
Posts: 5
Joined: Tue Mar 07, 2006 10:35 pm

RE: need help converting pin for use in Bond command

Wed Mar 15, 2006 7:14 pm

Johan,
I'm afraid that our hardware guys will not change our pin code to printable characters. They insist that your Bond function may be the problem. Could you please have your code checked and verify that you are using datatypes that won't lose the precision of our 0x8A byte?

Thanks

User avatar
johan
GpsGate
Posts: 15403
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: need help converting pin for use in Bond command

Wed Mar 15, 2006 7:37 pm

BlueTools passes on any data between 0 and 255 if you use the conversion algorithm I posted above. And it passes on any values from 1 to 127 if normal .NET unicode strings are used.

Regards,
Johan

Franson Support

tmi
Posts: 5
Joined: Tue Mar 07, 2006 10:35 pm

RE: need help converting pin for use in Bond command

Thu Mar 16, 2006 6:16 pm

Johan,

There is an opensource package for working with bluetooth in C# that has similar features to your package. I implemented it to test out bonding with my factory pin. I used my original conversion: strPin = (char)factorypin[0] +""+ (char)factorypin[1] +""+ (char)factorypin[2];
It works. I have no choice but to think that the issue is in how your bluetools handles the string I pass if I can pass it the same way to the bond function of this other package which works.
If this opensource package can get it right, I am hopeful that your paid package can get it to work in the next version.

I do like your package better, and paying for licensing is not an issue, but this other one works now and I am going to have to go with it now, unless you can give me some kind of timeframe when you will have a new version ready.

Message me and I will email you the URL to the opensource package. I avoid posting it here so that you don't lose any potential customers.

Regards,
Matt

Return to “BlueTools”