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
- 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 );
- 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;
- 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.
- To enumerate calls in the previous stepWsalookupservicebeginYou must useWsalookupservicenexFunction. The function returns a query result pointingWsaquerysetAnd the body.
- 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;
- 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.
- CallWsalookupserviceendFunction to end the device search. This function will be releasedWsalookupservicebeginThe created lookup handle.
Wsalookupserviceend (hlookup );
- 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