Partager via


connect (Bluetooth) (Windows Embedded CE 6.0)

1/6/2010

This function establishes a connection to a specified socket.

Note

This function is actually a Winsock function. However, the information that is presented in it is specific to Bluetooth.

Syntax

int connect(
  SOCKET s,
  const struct sockaddr FAR* name,
  int namelen
);

Parameters

  • s
    [in] Descriptor identifying an unconnected socket.
  • name
    [in] Name of the socket to which the connection should be established.
  • namelen
    [in] Length of the name.

Return Value

If no error occurs, this function returns zero. If an error occurs, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.

Remarks

Use this function to connect to a target device. Socket s must be created as a Bluetooth socket. The parameter name must specify the target Bluetooth device. If name.port is 0, then name.serviceClassId should contain the UUID of an RFCOMM-based service. Winsock performs an SDP call to find out the server channel number.

The following example code shows how to use the connect function to connect to a target device.

SOCKET s = socket (AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
if (s == INVALID_SOCKET) {
wprintf (L"Could not create socket: error %d\n", WSAGetLastError ());
return;
}
SOCKADDR_BTH sab;
memset (&sab, 0, sizeof(sab));
sab.addressFamily = AF_BTH;
sab.serviceClassId = FaxServiceClass_UUID;
sab.btAddr = target_bluetooth_device;
if (0 != connect (s, &sab, sizeof(sab)) {
wprintf (L"Could not connect socket: error %d\n", WSAGetLastError ());
return;
}

Note

RFCOMM supports only one connection for a particular server channel between two devices. Which means that if application A on device X is connected to server P on device Y, application B on device X will not be able to connect to the same server on device Y. However, device Y will be able to accept an incoming connection on the same server channel from a different device, as well as an incoming connection from device X to a different server channel.

For more information about the connect function, see connect (Windows Sockets) in the Winsock reference.

Requirements

Header winsock2.h
Library Ws2.lib
Windows Embedded CE Windows CE .NET 4.0 and later

See Also

Reference

Bluetooth Application Development Functions

Concepts

Winsock Extensions