I have been busy with my work and I still have my papers. Some time ago, I got herpes again.
This article describes how to create a virtual serial port on the server and enable the listening thread. Today I will talk about how the client requests the server and establishes a connection.
Prerequisites: the client must know the Bluetooth address and channel number of the server to request a connection from the server.
1. You can use WinSock programming APIs to discover a device's Bluetooth address. The APIs are as follows:
Wsaqueryset Structure
Wsalookupservicebegin Function
Wsalookupservicenext Function
Wsalookupserviceend Function
2: To obtain the channel number of the server, you need to access the SDP of the server. (thanks for the help of this channel, he quickly implemented this part ), this part must be familiar with the Bluetooth protocol.
First, the client needs to search for a bluetooth device. Obtain the Bluetooth address and device name. Here is the source code of the instance provided in Yamazaki.
# Define # Max_name 248
Static Bool sort minquiry ()
{
Wsaqueryset wsaq;
Handle hlookup;
Union {
Char Buf [5000];
Double_ Unused;//Ensure proper alignment
} ;
Lpwsaqueryset pwsaresults = (Lpwsaqueryset) BUF;
DWORD dwsize = Sizeof (BUF );
Bool bhavename;
Zeromemory ( & Wsaq, Sizeof (Wsaq ));
Wsaq. dwsize = Sizeof (Wsaq );
Wsaq. dwnamespace = Ns_bth;
Wsaq. lpcsabuffer = NULL;
If (Error_success ! = Wsalookupservicebegin ( & Wsaq, lup_containers, & Hlookup ))
{
Wprintf (L"Wsalookupservicebegin failed % d \ r \ n", Getlasterror ());
ReturnFalse;
}
Zeromemory (pwsaresults, Sizeof (Wsaqueryset ));
Pwsaresults -> Dwsize = Sizeof (Wsaqueryset );
Pwsaresults -> Dwnamespace = Ns_bth;
Pwsaresults -> Lpblob = NULL;
While (Error_success = Wsalookupservicenext (hlookup, lup_return_name | Lup_return_addr, & Dwsize, pwsaresults ))
{
Assert (pwsaresults -> Dwnumberofcsaddrs = 1 );
Bt_addr B = (Sockaddr_bth * ) Pwsaresults -> Lpcsabuffer -> Remoteaddr. lpsockaddr) -> Btaddr;
Bhavename = Pwsaresults -> Lpszserviceinstancename && * (Pwsaresults -> Lpszserviceinstancename );
Wprintf (L " % S % 04x % 08x % s \ n " , Bhavename ? Pwsaresults -> Lpszserviceinstancename: l "" ,
Bhavename ? L " ( " : L "" , Get_nap (B), get_sap (B), bhavename ? L " ) " : L "" );
Channel = 0 ; // 1 ~ 31
Byte * Pservicename = New Byte [max_name];
If (Elastic mservicesearch ( & B, & Channel, pservicename, bt_service_name )) // This function is used to query SDP. Its implementation will be discussed below
{
//Locate the service and add the processing code//Bt_service_name defines a string in the server SDP, which is equivalent to a flag and is also called its service name.
} // Channel is the channel number of the remote device to be obtained.
}
Wsalookupserviceend (hlookup );
Return True;
}