WinCE5下查询蓝牙服务,每次都出现错误:WSASERVICE_NOT_FOUND
[复制链接]
使用WSALookupServiceBegin、WSALookupServiceNext和WSALookupServiceEnd,可以搜索到周围的蓝牙设备,可以正确获取设备名称和地址。
现在想获取设备提供的服务,照搬MSDN中《Querying Service Capability on Remote Bluetooth Devices》这一节的代码:
static int PerformServiceSearch (BT_ADDR *pb)
{
CoInitializeEx (0, COINIT_MULTITHREADED);
int iResult = 0;
BTHNS_RESTRICTIONBLOB RBlob;
memset (&RBlob, 0, sizeof(RBlob));
RBlob.type = SDP_SERVICE_SEARCH_ATTRIBUTE_REQUEST;
RBlob.numRange = 1;
RBlob.pRange[0].minAttribute = SDP_ATTRIB_PROTOCOL_DESCRIPTOR_LIST;
RBlob.pRange[0].maxAttribute = SDP_ATTRIB_PROTOCOL_DESCRIPTOR_LIST;
RBlob.uuids[0].uuidType = SDP_ST_UUID16;
RBlob.uuids[0].u.uuid16 = SerialPortServiceClassID_UUID16;
BLOB blob;
blob.cbSize = sizeof(RBlob);
blob.pBlobData = (BYTE *)&RBlob;
SOCKADDR_BTH sa;
memset (&sa, 0, sizeof(sa));
*(BT_ADDR *)(&sa.btAddr) = *pb;
sa.addressFamily = AF_BT;
CSADDR_INFO csai;
memset (&csai, 0, sizeof(csai));
csai.RemoteAddr.lpSockaddr = (sockaddr *)&sa;
csai.RemoteAddr.iSockaddrLength = sizeof(sa);
WSAQUERYSET wsaq;
memset (&wsaq, 0, sizeof(wsaq));
wsaq.dwSize = sizeof(wsaq);
wsaq.dwNameSpace = NS_BTH;
wsaq.lpBlob = &blob;
wsaq.lpcsaBuffer = &csai;
HANDLE hLookup;
int iRet = WSALookupServiceBegin (&wsaq, 0, &hLookup);
if (ERROR_SUCCESS == iRet)
{
CHAR buf[5000];
LPWSAQUERYSET pwsaResults = (LPWSAQUERYSET) buf;
DWORD dwSize = sizeof(buf);
memset(pwsaResults,0,sizeof(WSAQUERYSET));
pwsaResults->dwSize = sizeof(WSAQUERYSET);
pwsaResults->dwNameSpace = NS_BTH;
pwsaResults->lpBlob = NULL;
iRet = WSALookupServiceNext (hLookup, 0, &dwSize, pwsaResults);
if (iRet == ERROR_SUCCESS) // Success - got the stream
{
//Parse the SDP result set by using the FindRFCOMMChannel function.
if (ERROR_SUCCESS == FindRFCOMMChannel (pwsaResults->lpBlob->pBlobData, pwsaResults->lpBlob->cbSize, &cChannel))
iResult = cChannel;
}
WSALookupServiceEnd(hLookup);
}
CoUninitialize ();
return iResult;
}
每次运行到iRet = WSALookupServiceNext (hLookup, 0, &dwSize, pwsaResults)返回-1,调用WSAGetLastError(),返回WSASERVICE_NOT_FOUND。
调试了两天,按照MSDN的说明修改BTHNS_RESTRICTIONBLOB、WSAQUERYSET等结构体,但怎么改都是返回失败……
没理由MSDN提供的代码有问题吧?我的OS是自己定制的,蓝牙部分选上Bluetooth Stack with Universal Loadable Driver,不知是否还要配置其他东西?