Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
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 Specificationat 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_ADDRtype, as defined in Ws2bth.h:

    Copy Code
    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 WSAStartupfunction as the following example code shows.

    Copy Code
    WSADATA wsd;
    WSAStartup (MAKEWORD(1,0), &wsd);
    
  2. Create a Bluetooth socket by calling the socket (Bluetooth)function, as the following example code shows.

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

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

  3. Store information about the remote Bluetooth device that the client is connecting to, by configuring a SOCKADDR_BTHstructure.

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

      Copy Code
      SOCKADDR_BTH sa;
      memset (&sa, 0, sizeof(sa));
      
    2. Set the btAddrmember to a BT_ADDRvariable that contains the address of the target device.

      Copy Code
      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 serviceClassIdmember of SOCKADDR_BTHto 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 portmember of SOCKADDR_BTHto the server channel number as the following example code shows.

      Copy Code
      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.

    Copy Code
    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 closesocketfunction to close the Bluetooth socket. Also, ensure that you release the socket by calling the CloseHandlefunction, as the following example code shows.

    Copy Code
    closesocket(client_socket);
    CloseHandle ((LPVOID)client_socket);
    
  6. To terminate the use of Winsock services, call the WSACleanupfunction. There must be a call to WSACleanupfor every successful call to WSAStartupmade 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 WSAStartupfunction as the following example code shows.

    Copy Code
    WSADATA wsd;
    WSAStartup (MAKEWORD(1,0), &wsd);
    
  2. Create a Bluetooth socket by calling the socket (Bluetooth)function, as the following example code shows.

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

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

  3. Configure a SOCKADDR_BTHstructure to store information about the server Bluetooth device. The following example code shows the values to set in SOCKADDR_BTHmembers.

    Copy Code
    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 channelto 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.

    Copy Code
    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 listenfunction as the following example code shows.

    Copy Code
    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.

    Copy Code
    SOCKADDR_BTH sa2;
    int size = sizeof(sa2);
    SOCKET s2 = accept (server_socket, (SOCKADDR *)&sa2,
    &size);
    

    A call to acceptfrom the server returns the address of the client in a SOCKADDR_BTHvariable.

  7. Close the Bluetooth socket, by calling the closesocketas the following example code shows.

    Copy Code
    closesocket(server_socket);
    
  8. To terminate the use of Winsock services, call the WSACleanupfunction. There must be a call to WSACleanupfor every successful call to WSAStartupmade by an application.

See Also