Share via


Creating a Connection to a Remote Device Using Winsock

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

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.

Windows Mobile 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.

You can also create a connection by using the COM Port emulator facility that ships with Windows Mobile. For more information about using this facility to create connections, see Creating a Connection to a Remote Device Using a Virtual COM Port.

Note

Error handling has been omitted in this topic for clarity.

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 sample that ships with Windows Mobile, contains source code for creating a Bluetooth connection by using Winsock.

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 (Bluetooth) 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.

    1. Create and initialize a SOCKADDR_BTH variable as the following example code shows:

      SOCKADDR_BTH sa;
      memset (&sa, 0, sizeof(sa));
      
    2. Set the btAddr member to a BT_ADDR variable that contains the address of the target device.

      sa.btAddr = b; //b is a 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.

    3. 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.
      - or-
      If 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 (Bluetooth) 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 (Bluetooth) 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 (Bluetooth) 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 (Bluetooth) 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.

See Also

Concepts

Creating a Connection to a Remote Device Using a Virtual COM Port

Other Resources

Bluetooth Application Development