Use WinSock to search for Bluetooth devices)

Source: Internet
Author: User
You can use the following components to create a query to search for a range of remote Bluetooth devices:
    • WsaquerysetStruct
    • WsalookupservicebeginFunction
    • WsalookupservicenextFunction
    • WsalookupserviceendFunction
Note: For clarity, the error handling is ignored. Search and return the remote device address
    1. Provides the Winsock version and detailed implementation data to initialize the caller application. You can callWsastartupFunction to obtain the data.
Wsadata WSD; Wsastartup (makeword (1, 0), & WSD );
    1. Create and initializeWsaquerysetVariables are used to specify search parameters and setDwnamespaceThe Member isNs_bthThe limit is to query Bluetooth devices.
Wsaqueryset wsaq; Zeromemory (& wsaq, sizeof (wsaq )); Wsaq. dwsize = sizeof (wsaq ); Wsaq. dwnamespace = ns_bth; Wsaq. lpcsabuffer = NULL;
    1. CallWsalookupservicebeginFunction to execute a query.
Int iret = wsalookupservicebegin (& wsaq, lup_containers, & hlookup ); SetLup_containersAssigned DwflagsParameter, start SDP to search for the bluetooth device. Note: Set DwflagsIf the parameter is set to zero, a service search is executed. Wsalookupservicebegin The function returns a handle HlookupParameter.
    1. To enumerate calls in the previous stepWsalookupservicebeginYou must useWsalookupservicenexFunction. The function returns a query result pointingWsaquerysetAnd the body.
      1. SetWsaquerysetStruct to storeWsalookupservicenextThe device data returned by the function.
Char Buf [4096]; Lpwsaqueryset pwsaresults = (lpwsaqueryset) BUF; Zeromemory (pwsaresults, sizeof (wsaqueryset )); Pwsaresults-> dwsize = sizeof (wsaqueryset ); Pwsaresults-> dwnamespace = ns_bth; Pwsaresults-> lpblob = NULL;
      1. CallWsalookupservicenextAnd setWsalookupservicebeginReturnedHlookupThe parameter is passed to it. To improve performanceWsalookupservicebegin Only the device address is returned and stored in the memory. To return the device name and addressLup_return_name | lup_return_addrAssignedDwflagsParameters.
DWORD dwsize = sizeof (BUF ); Int iret = wsalookupservicenext (hlookup, lup_return_name | lup_return_addr, & dwsize, pwsaresults) It can be called cyclically. WsalookupservicenextTo enumerate all devices.
    1. CallWsalookupserviceendFunction to end the device search. This function will be releasedWsalookupservicebeginThe created lookup handle.
Wsalookupserviceend (hlookup );
    1. To end the use of the Winsock service, callWsacleanupFunction. InProgramFor each successfully calledWsastartupMust be called accordinglyWsacleanup.
The following code example performs a device inquiry displays the device name in a user interface. The followingCodeA device query is executed and the device name is displayed on the user interface. //------------------------------------------------------------------------ // Function: performinquiry // Purpose: performs a device inquiry displays the device name in a user interface. //------------------------------------------------------------------------ # Deprecision max_name 248 Static bool initialize 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 ()); Return false; } 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 ""); } Wsalookupserviceend (hlookup ); Return true; }

from : http://blog.csdn.net/jdcb2001/archive/2006/06/21/819643.aspx

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.