Bluetooth Serial Communication (lower) under wince -- Client

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.