Escolar Documentos
Profissional Documentos
Cultura Documentos
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:
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.
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:
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.
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.
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
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.
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.
When the client starts, it creates a socket that is associated to the Bluetooth stack. A socket stream is set in the following manner.