PC为XP SP3,ThinkPad SL400,自带有蓝牙功能,现在我在PC旁边打开手机蓝牙,使用PC的蓝牙搜索,可以搜索到手机蓝牙。
但是我自己编写的程序为什么就搜索不到呢?代码如下:
- C/C++ code
int _tmain(int argc, _TCHAR* argv[]){ WSADATA wsaData; DWORD dwResult; HANDLE hLookup = 0; WSAQUERYSET lpRestrictions; GUID guid = SVCID_HOSTNAME; dwResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (dwResult != 0) { printf("Cannot startup Winsock, error code %d\n", dwResult); exit(1); } else printf("WSAStartup is OK!\n"); ZeroMemory(&lpRestrictions, sizeof(WSAQUERYSET)); lpRestrictions.dwSize = sizeof(WSAQUERYSET); lpRestrictions.lpServiceClassId = &guid; dwResult = WSALookupServiceBegin(&lpRestrictions, LUP_RETURN_NAME, &hLookup); if (dwResult != SOCKET_ERROR) { DWORD dwLength = 0; WSAQUERYSET * pqs = NULL; printf("WSALookupServiceBegin() is OK!\r\n"); pqs = (WSAQUERYSET *) malloc(sizeof(WSAQUERYSET) + 100); dwLength = sizeof(WSAQUERYSET) + 100; do { if (WSALookupServiceNext(hLookup, 0, &dwLength, pqs) != 0) { dwResult = WSAGetLastError(); if((dwResult == WSA_E_NO_MORE) || (dwResult == WSAENOMORE)) { printf("No more record found!\n"); break; } printf("WSALookupServiceNext() failed with error code %d\n", WSAGetLastError()); } else { dwResult = 0; printf("WSALookupServiceNext() looks fine!\r\n"); } if (dwResult == WSAEFAULT) { if (pqs) { printf("Freeing pqs...\r\n"); free(pqs); } // Reallocate pqs = (WSAQUERYSET *) malloc(dwLength); if (!pqs) { printf("Could not allocate memory: %d\n", GetLastError()); exit(2); } else { printf("Memory allocated for pqs successfully!\r\n"); continue; } } // Output it since we have it now if ((dwResult == 0) && (pqs)) { printf(" Service instance name: %S\n", pqs->lpszServiceInstanceName); printf(" Name space num: %d\r\n", pqs->dwNameSpace); printf(" Num of protocols: %d\r\n", pqs->dwNumberOfProtocols); //printf(" Version: %d\r\n", pqs->lpVersion->dwVersion); } } while ((dwResult != WSA_E_NO_MORE) && (dwResult != WSAENOMORE)); // clean-up free(pqs); if(WSALookupServiceEnd(hLookup) == 0){ printf("hLookup handle was released!\n"); } else{ printf("WSALookupServiceEnd(hLookup) failed with error code %d\n", WSAGetLastError()); } } else { printf("Error on WSALookupServiceBegin: %d\n", WSAGetLastError()); exit(3); } if(WSACleanup() == 0){ printf("WSACleanup() is OK!\n"); } else{ printf("WSACleanup() failed with error code %d\n", WSAGetLastError()); } printf("按回车键退出!\n"); char c = getchar(); return 0;}
搜索到的结果一直是:
WSAStartup is OK
WSALookupServiceBegin() is OK!
WSALookupServiceNext() looks fine!
Service instance name:FHJUN2007 //这是该PC的电脑名,应该是手机的呀
Name space num:12 //应该是16(NS_BTH)吧,而不是12(NS_DNS)
Num of protocols:0
No more record found!