WINCE APP之搜索蓝牙设备

对wince应用程序的编写,以用来搜索到周边的蓝牙设备,需要用到下面3个API函数:

(1)
INT WSALookupServiceBegin(
  LPWSAQUERYSET pQuerySet,
  DWORD dwFlags,
  LPHANDLE lphLookup
);

这个函数主要用途是填充一个返回参数lphLookup,这是一个HANDLE,是给以后的两个函数用的。我们知道WSA开头的一般都是winsock函数,这个也不例外,只不过是它的第一个参数也就是搜索集pQuerySet设置成了蓝牙相关的。WSAQUERYSET结构如下:
typedef struct _WSAQuerySet {
  DWORD dwSize;
  LPTSTR lpszServiceInstanceName;
  LPGUID lpServiceClassId;
  LPWSAVERSION lpVersion;
  LPTSTR lpszComment;
  DWORD dwNameSpace;
  LPGUID lpNSProviderId;
  LPTSTR lpszContext;
  DWORD dwNumberOfProtocols;
  LPAFPROTOCOLS lpafpProtocols;
  LPTSTR lpszQueryString;
  DWORD dwNumberOfCsAddrs;
  LPCSADDR_INFO lpcsaBuffer;
  DWORD dwOutputFlags;
  LPBLOB lpBlob;
} WSAQUERYSET, *PWSAQUERYSETW;

不过对于蓝牙,我们一般这么用:
WSAQUERYSET btQuerySet;
memset(&btQuerySet;, 0,sizeof(btQuerySet;));
btQuerySet.dwSize = sizeof(btQuerySet;);
btQuerySet.dwNameSpace = NS_BTH;
btQuerySet.lpcsaBuffer = NULL;

再接着看下第二个参数dwFlags,它的取值如下:
LUP_CONTAINERS: 
Specifies that device discovery is to be performed. If this flag is not set, service discovery will be performed instead. 
LUP_RES_SERVICE: 
Searches the local SDP database. Clear this flag to search for services on a peer device.
这里用的是LUP_CONTAINERS
If LUP_CONTAINERS is set, SDP performs a device inquiry to find other Bluetooth devices in the area. This function performs the query. The WSALookupServiceNext function retrieves the results one device at a time.
 
(2)
INT WSALookupServiceNext(
  HANDLE hLookup,
  DWORD dwFlags,
  LPDWORD lpdwBufferLength,
  LPWSAQUERYSET pResults
);

这个函数就是用上一个函数的hLookup来开始进行实际的搜索,那么到底搜索些什么呢,也就是我们对那些东西感兴趣呢,这就是通过第二个参数dwFlags指定的。其取值如下:
LUP_RETURN_NAME: 
Performs a name inquiry and sets the results in the pResults lpszServiceInstanceName element.
 
LUP_RETURN_ADDR:
Returns the address of the enumerated remote device in pResults->lpcsaBuffer->RemoteAddr.lpSockaddr.
 
LUP_RETURN_BLOB:
Returns a BthInquiryResult structure in pResults->lpBlob->pBlobData.
 
BTHNS_LUP_RESET_ITERATOR:
Causes the list enumeration to be reset. No data is returned during this call. The next call to this function retrieves information on the first device in the enumeration list.
 
BTHNS_LUP_NO_ADVANCE:
If this flag is not set, the next item in the list is enumerated on the next call to this function. If this flag is set, the next call to this function returns information on the same remote device.
对于BT,我们主要关心的是NAME用以显示,ADDR用于连接设备并传输数据。
 
(3)
INT WSALookupServiceEnd(
  HANDLE hLookup
);
这个函数就显而易见了,关闭搜索句柄。
 
以上3个函数的位置:
Header: Winsock2.h.
Link Library: Ws2.lib.

你可能感兴趣的:(职场,休闲)