Winsock problem (Bluetooth)

  • Thread starter B Vidyadhar Joshi
  • Start date
B

B Vidyadhar Joshi

I'm trying to implement a Bluetooth client application. But I get an error
when I try to "bind" the socket created with "socket" function. Here is the
code I'm using:

public class BTClient
{
internal const int WSADescriptionLength = 256;
internal const int WSASysStatusLen = 128;
internal const int WSAVersionRequested = 0x201;
internal const int AfBth = 32;
internal const int SockStream = 1;
internal const int BthProtoRFComm = 0x0003;
internal const int SolSocket = 0xffff;
internal const uint BtPortAny = unchecked((uint)-1);
internal const int NsBth = 16;

[StructLayout(LayoutKind.Sequential)]
internal struct WSAData
{
internal short wVersion;
internal short wHighVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = WSADescriptionLength
+ 1)]
internal string szDescription;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = WSASysStatusLen +
1)]
internal string szSystemStatus;
internal int iMaxSockets;
internal int iMaxUdpDg;
internal IntPtr lpVenderInfo;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
internal struct SockAddrBth
{
internal ushort addressFamily;
internal ulong btAddr;
internal Guid serviceClassId;
internal uint port;
}

[DllImport("ws2_32.dll", SetLastError=true)]
private static extern int WSAGetLastError();

[DllImport("ws2_32.dll", SetLastError=true)]
private static extern int WSAStartup(int vVersionRequested, ref WSAData
lpWSAData);

[DllImport("ws2_32.dll", SetLastError=true)]
private static extern int WSACleanup();

[DllImport("ws2_32.dll", SetLastError=true)]
private static extern UIntPtr socket(int af, int type, int protocol);

[DllImport("ws2_32.dll", SetLastError=true)]
private static extern int bind(UIntPtr s,ref SockAddrBth name, int
namelen);

public void CreateSocket()
{
int err = 0; //Error code

//Initialize Winsock
WSAData x = new WSAData();
WSAStartup(WSAVersionRequested, ref x); //Works fine

//Open a socket
UIntPtr sock = socket(AfBth, SockStream, BthProtoRFComm); //Works
fine. sock == 3680
err = WSAGetLastError();

//bind the socket
SockAddrBth sab = new SockAddrBth();
sab.addressFamily = AfBth; //Should be AF_BTH == 32
sab.btAddr = 0; //Should be Zero
sab.serviceClassId = new Guid(0x00000000, 0x0000, 0x0000, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); //NULL_GUID
sab.port = 0; //number of service
channel, 0 or BT_PORT_ANY. 0 for client applications

int siz = Marshal.SizeOf(sab); //siz == 40
int z = bind(sock,ref sab, siz); //Returns -1
err = WSAGetLastError(); //Returns 10049 == "The specified
address is not a valid address for this computer."

//Cleanup
.....
}
}

I am unable to understand where I'm going wrong. Please help!

TIA

B Vidyadhar Joshi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top