搜索蓝牙设备

grooving 2017-03-05 09:44:57
我写了两套c++和.net的代码用于搜索蓝牙设备,但对于图中画红圈图标类型的蓝牙设备始终无法获得,其他类型蓝牙设备可以。请教为何?


.net代码如下:

BluetoothRadio[] BuleRadios = BluetoothRadio.AllRadios;

//BuleRadio.Mode = RadioMode.Connectable;

foreach (var item in BuleRadios)

{

//item.Mode = RadioMode.Connectable;

//item.Mode = RadioMode.PowerOff;

item.Mode = RadioMode.Discoverable;

}



BluetoothDeviceInfo[] Devices = Blueclient.DiscoverDevices();

lsbDevices.Items.Clear();

deviceAddresses.Clear();

foreach (BluetoothDeviceInfo device in Devices)

{

lsbDevices.Items.Add(device.DeviceName);

deviceAddresses[device.DeviceName] = device.DeviceAddress;

}







c++代码如下:

WSADATA wsd;

WSAStartup(MAKEWORD(1, 0), &wsd);



HANDLE hLookup;



union {

CHAR buf[5000];

double __unused;

};



LPWSAQUERYSET pwsaResults = (LPWSAQUERYSET)buf;

DWORD dwSize = sizeof(buf);

BOOL bHaveName;





WSAQUERYSET wsaq;

ZeroMemory(&wsaq, sizeof(wsaq));

wsaq.dwSize = sizeof(wsaq);

wsaq.dwNameSpace = NS_BTH;

wsaq.lpcsaBuffer = NULL;

if (ERROR_SUCCESS != WSALookupServiceBegin(&wsaq, LUP_DEEP | LUP_CONTAINERS | LUP_CONTAINERS, &hLookup))

{

wsprintf(devicesList, L"WSALookupServiceBegin failed %d\r\n", GetLastError());

return -1;

}

ZeroMemory(pwsaResults, sizeof(WSAQUERYSET));

pwsaResults->dwSize = sizeof(WSAQUERYSET);

pwsaResults->dwNameSpace = NS_BTH;

pwsaResults->lpBlob = NULL;

int numberRetrivedDevices = 0;

while (ERROR_SUCCESS == WSALookupServiceNext(hLookup, LUP_DEEP | LUP_RETURN_NAME | LUP_RETURN_ADDR | LUP_CONTAINERS, &dwSize, pwsaResults))

{

wchar_t currentDev[1024];

if (pwsaResults->dwNumberOfCsAddrs != 1)

{

wsprintf(devicesList, L"WSALookupServiceNext failed %d\r\n", GetLastError());

return -1;

}

if (numberRetrivedDevices > 0)

wcscat(devicesList, L",");

pwsaResults->lpcsaBuffer->RemoteAddr.lpSockaddr)->btAddr;

bHaveName = pwsaResults->lpszServiceInstanceName && *(pwsaResults->lpszServiceInstanceName);

wcscat(devicesList, currentDev);

numberRetrivedDevices++;

}

WSALookupServiceEnd(hLookup);

...全文
136 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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