You are on page 1of 4

Creating a Connection to a Remote Device Using Winsock

From: MSDN page.

To enable communication between two Bluetooth devices, you can create a connection by creating client and server sockets. The Bluetooth server socket must be configured to listen for incoming connection and accept a client socket. The Bluetooth client socket must know the address of the Bluetooth device to connect to, before it sends a connection request. Microsoft Windows CE implementation of Bluetooth allows you to create a piconet. As per the Bluetooth specification, a master device can connect with seven active slave devices. For more information about piconet, see the Bluetooth Core Specification at this Official Bluetooth Wireless Info Web site; http://www.bluetooth.com/bluetooth/ Before you create a connection between two Bluetooth devices, you must have the following information:
Before you create a connection between two Bluetooth devices, you must have the following information:

The address of the remote Bluetooth device to query, as a BT_ADDR type, as defined in Ws2bth.h:

typedef ULONGLONG bt_addr, *pbt_addr, BT_ADDR, *PBT_ADDR;


Note This requirement is for client ports only.

Service identifier as a GUID type variable. - or RFCOMM channel (between 1 and 31).

The Ssa sample that ships with Windows CE, contains source code for creating a Bluetooth connection by using Winsock. For more information about this sample, see Winsock Interface Sample at the bottom of the page. To create a client socket 1.Prepare the caller application by providing data about Winsock such as the version and implementation details. This data can be retrieved by calling the WSAStartup function as the following example code shows.

WSADATA wsd; WSAStartup (MAKEWORD(1,0), &wsd);


2.Create a Bluetooth socket by calling the socket function, as the following example code shows.

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


The parameter values for the socket function configures this socket for Bluetooth services.

3.

Store information about the remote Bluetooth device that the client is connecting to, by configuring a SOCKADDR_BTH structure. a. Create and initialize a SOCKADDR_BTH variable as the following example code shows:

SOCKADDR_BTH sa; memset (&sa, 0, sizeof(sa));


b. Set the btAddr member to a BT_ADDR variable that contains the address of the target device.

sa.btAddr = b; //b is a BT_ADDR variable


Your application can accept the device address as a string but must convert the address and store it in a variable of type BT_ADDR.

c. If the service identifier is available, then set the serviceClassId member of SOCKADDR_BTH to the GUID of the RFCOMM-based service. In this case, the client performs an SDP query and then uses the resulting server channel. - orIf you want to use a hard-coded channel number, set the port member of SOCKADDR_BTH to the server channel number as the following example code shows.

sa.port = channel & 0xff;


4.Connect to the Bluetooth socket, created in step 2, by calling the connect function, as the following example code shows.

if (connect (client_socket, (SOCKADDR *)&sa, sizeof(sa))) { //Perform error handling. closesocket (client_socket); return 0; }
Specify the attributes of the target device by passing a SOCKADDR_BTH, configured in step 3. After the connection is established, you can communicate with the target device by sending and receiving data. 5.To close the connection to the target device, call the closesocket function to close the Bluetooth socket. Also, ensure that you release the socket by calling the CloseHandle function, as the following example code shows.

closesocket(client_socket); CloseHandle ((LPVOID)client_socket);


6.To terminate the use of Winsock services, call the WSACleanup function. There must be a call to WSACleanup for every successful call to WSAStartup made by an application. To create a server socket 1.Prepare the caller application by providing Winsock-related data such as the version and implementation details. This data can be retrieved by calling the WSAStartup function as the following example code shows.

WSADATA wsd; WSAStartup (MAKEWORD(1,0), &wsd);


2.Create a Bluetooth socket by calling the socket function, as the following example code shows.

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


The parameter values for the socket function configures this socket for Bluetooth services. 3.Configure a SOCKADDR_BTH structure to store information about the server Bluetooth device. The following example code shows the values to set in SOCKADDR_BTH members.

SOCKADDR_BTH sa; memset (&sa, 0, sizeof(sa)); sa.addressFamily = AF_BT; sa.port = channel & 0xff;
Note To avoid conflicts, when you are selecting the server channel, it is recommended that you set channel to 0. This configures RFCOMM to use the next available channel. The information that is stored in this structure is used to bind a Bluetooth socket to the local address of the server device. 4.Bind the socket created in step 2, by calling the bind function, as the following example code shows. Pass a reference to SOCKADDR_BTH, created in step 3, to specify the device information.

if (bind (server_socket, (SOCKADDR *)&sa, sizeof(sa))) { ... //Perform error handling closesocket (server_socket); return 0; }
5.Listen for incoming connections from remote Bluetooth client devices, by calling the listen function as the following example code shows.

if (listen (server_socket, 5)) { ... //Perform error handling closesocket (server_socket); return 0; }
6.Accept incoming connections, by calling the accept function as the following example shows.

SOCKADDR_BTH sa2; int size = sizeof(sa2); SOCKET s2 = accept (server_socket, (SOCKADDR *)&sa2, &size);
A call to accept from the server returns the address of the client in a SOCKADDR_BTH variable. 7.Close the Bluetooth socket, by calling the closesocket as the following example code shows.

closesocket(server_socket);
8.To terminate the use of Winsock services, call the WSACleanup function. There must be a call to WSACleanup for every successful call to WSAStartup made by an application.

WINSOCK SAMPLE/EXAMPLE
The Ssa sample applications illustrate the use of the Windows CEbased Bluetooth stack over the Winsock interface. The compiled samples generates 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.

Usage

ssa server {-g <GUID> | -c <rfcomm_chnl> } ssa client <server_bt_addr> { -g <GUID> | -c <rfcomm_chnl> }
The following table describes the parameters needed when using the SSA sample application.
Parameters Description

GUID rfcomm_chnl server_bt_addr

SDP Service Class ID. RFCOMM channel (between 1 and 31). 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 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.

You might also like