Share via


Winsock Interface Sample (Windows Embedded CE 6.0)

1/6/2010

The Ssa and the SockTest sample applications illustrate the use of the Windows Embedded CEā€“based Bluetooth stack over the Winsock interface.

The compiled samples generate executables. These applications can be started as server or client. When connected, users can exchange text messages or files.

Note

Either the port name or service UUID can be specified. If the UUID is specified, the server registers the SDP entry and the client connects with the service UUID instead of the server channel.

For more information, see Creating a Connection to a Remote Device Using Winsock.

To view supported command line options, use SSA /?

Usage

ssa server {-g <GUID> | -c <rfcomm_chnl> }
ssa client <server_bt_addr> { -g <GUID> | -c <rfcomm_chnl> }

Remarks

The following table describes the parameters needed when using the SSA sample application.

Parameters Description

GUID

SDP Service Class ID.

rfcomm_chnl

RFCOMM channel (between 1 and 31).

server_bt_addr

Bluetooth address of the server.

Server

When the server starts, it creates a socket that is associated to the Bluetooth stack. A socket stream is set in the following manner.

SOCKET s = socket (AF_BT, SOCK_STREAM, BTHPROTO_RFCOMM);

To set the socket up for binding, designate the address family, in the SOCKADDR_BTH data structure, as Bluetooth.

sa.addressFamily = AF_BT;

Connections can be set to reference an RFCOMM channel or an SDP service GUID. To connect through an RFCOMM channel, register the channel number in the data structure in the following manner.

sa.port = channel & 0xff;

If you specified a GUID argument in the command line, register that GUID through the data field in the following manner.

GetGUID (arg3, &serviceClassId);

The following line of code binds the socket.

bind (gServerSocket, (SOCKADDR *)&sa, sizeof(sa));

If the binding is successful, the SDP service connection is initiated with the WSASetService (Bluetooth) function.

BthNsSetService(&Service, RNRSERVICE_REGISTER, 0);

The next step is to set the server up to listen to the socket stream for instructions.

listen (gServerSocket, 5);

Client

When the client starts, it creates a socket that is associated to the Bluetooth stack. A socket stream is set in the following manner.

SOCKET s = socket (AF_BT, SOCK_STREAM, BTHPROTO_RFCOMM);

To set the socket up, designate the address family, in the SOCKADDR_BTH data structure, as Bluetooth. Register the Bluetooth server address through the btAddr member.

sa.addressFamily = AF_BT;
sa.btAddr = b;

Connections can be set to reference an RFCOMM channel or an SDP service GUID. To connect through an RFCOMM channel, register the channel number in the data structure in the following manner.

sa.port = channel & 0xff;

If you specified a GUID argument in the command line, register that GUID through the data field in the following manner.

GetGUID (arg4, &sa.serviceClassId);

The following line of code connects the socket to the stream.

connect (s, (SOCKADDR *)&sa, sizeof(sa));

If the connection is successful, write and read worker threads are created.

CloseHandle (CreateThread(NULL, 0, ReadThread, (LPVOID)s, 0, NULL));
WriteThread ((LPVOID)s);

The WriteThread sends data, entered by the user, over the socket stream, while the ReadThread, on the listening device, displays the received data on the screen.

Sample Location

%_WINCEROOT%\Public\Common\Sdk\Samples\Bluetooth\

Note

This sample application has not been thoroughly tested and is not intended for production use.

See Also

Concepts

Bluetooth Application Development Samples
Bluetooth Samples

Other Resources

Bluetooth Application Development