connect (Bluetooth)
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.
int connect(
SOCKET s,
const struct SOCK_ADDR* 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 Values
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
OS Versions: Windows CE .NET 4.0 and later.
Header: Winsock2.h.
Link Library: Ws2.lib.
See Also
Bluetooth Functions | Winsock Extensions | Bluetooth
Last updated on Thursday, April 08, 2004
© 1992-2003 Microsoft Corporation. All rights reserved.