PC程序为什么搜索不到蓝牙设备?

dog357 2012-07-11 09:42:22
PC为XP SP3,ThinkPad SL400,自带有蓝牙功能,现在我在PC旁边打开手机蓝牙,使用PC的蓝牙搜索,可以搜索到手机蓝牙。
但是我自己编写的程序为什么就搜索不到呢?代码如下:

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!
hLookup handle was released!
...全文
845 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
nonono1111 2013-03-18
  • 打赏
  • 举报
回复
请问你这个问题解决了没有,我也和你一样的问题啊!
pig357 2012-07-27
  • 打赏
  • 举报
回复
买块蓝牙模块试试。。。
dog357 2012-07-12
  • 打赏
  • 举报
回复
WSALookupServiceBegin提示错误10108,但使用电脑自带的蓝牙搜索,可以搜索到周围的蓝牙模块啊
dog357 2012-07-11
  • 打赏
  • 举报
回复
搜索到的一直是自己的电脑。。。蓝牙设备无法搜索到

3,849

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 无线
社区管理员
  • 无线
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧