WM蓝牙开发--搜索周围的设备
来源:互联网 发布:网络经纪模式案例分析 编辑:程序博客网 时间:2024/04/30 14:53
开发环境: vs2008+wm6.0 sdk
HTC3238+手机两台
1号手机把蓝牙打开,并设为可见模式,2号手机把蓝牙打开
代码如下:
WORD wVersionRequested=0;
WSADATA wsaData;
INT iResult = 0;
LPWSAQUERYSET pwsaResults;
DWORD dwSize = 0;
WSAQUERYSET wsaq;
HCURSOR hCurs;
HANDLE hLookup = 0;
// Initialize Winsock
wVersionRequested = MAKEWORD( 2, 2 );
//初始化
if (WSAStartup( wVersionRequested, &wsaData ) != 0)
{
MessageBox(_T("Could not initialize winsock!"), _T("Fatal Error"), MB_OK);
}
memset (&wsaq, 0, sizeof(wsaq));
wsaq.dwSize = sizeof(wsaq);
wsaq.dwNameSpace = NS_BTH;
wsaq.lpcsaBuffer = NULL;
// initialize searching procedure
iResult = WSALookupServiceBegin(&wsaq,
LUP_CONTAINERS,
&hLookup);
if (iResult != 0)
{
TCHAR tszErr[32];
iResult = WSAGetLastError();
StringCchPrintf(tszErr, 32, _T("Socket Error: %d"), iResult);
MessageBox(tszErr, _T("Error"), MB_OK);
}
union {
CHAR buf[5000]; // returned struct can be quite large
SOCKADDR_BTH __unused; // properly align buffer to BT_ADDR requirements
};
// save the current cursor
hCurs = GetCursor();
for (; ;)
{
// set the wait cursor while searching
SetCursor(LoadCursor(NULL, IDC_WAIT));
pwsaResults = (LPWSAQUERYSET) buf;
dwSize = sizeof(buf);
memset(pwsaResults,0,sizeof(WSAQUERYSET));
pwsaResults->dwSize = sizeof(WSAQUERYSET);
// namespace MUST be NS_BTH for bluetooth queries
pwsaResults->dwNameSpace = NS_BTH;
pwsaResults->lpBlob = NULL;
// iterate through all found devices, returning name and address
// (this sample only uses the name, but address could be used for
// further queries)
iResult = WSALookupServiceNext (hLookup,
LUP_RETURN_NAME | LUP_RETURN_ADDR,
&dwSize,
pwsaResults);
if (iResult != 0)
{
iResult = WSAGetLastError();
if (iResult != WSA_E_NO_MORE)
{
TCHAR tszErr[32];
iResult = WSAGetLastError();
StringCchPrintf(tszErr, 32, _T("Socket Error: %d"), iResult);
MessageBox(tszErr, _T("Error"), MB_OK);
}
// we're finished
break;
}
// add the name to the listbox
if (pwsaResults->lpszServiceInstanceName)
{
m_ctrlDevList.AddString(pwsaResults->lpszServiceInstanceName);
}
}
WSALookupServiceEnd(hLookup);
// restore cursor
SetCursor(hCurs);
注意: 要
#include <winsock2.h>
#include <bt_api.h>
不然说NS_BTH没定义
经过测试,在2号手机上运行后可以发现1号手机
- WM蓝牙开发--搜索周围的设备
- [Android蓝牙]搜索周围蓝牙设备
- 显示周围的蓝牙设备
- android-扫描周围蓝牙设备
- Android开发之蓝牙(Bluetooth)操作(二)--修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备
- Android开发之蓝牙(Bluetooth)操作(二)--修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备(转)
- 扫描周围可用蓝牙设备、以及设置蓝牙设备的可见性
- 蓝牙通信-搜索附近的蓝牙设备
- 搜索附近的蓝牙设备
- Android蓝牙开发——搜索设备
- Android 蓝牙通信开发(一)搜索蓝牙设备
- Android开发蓝牙篇之蓝牙设备开启、搜索周边蓝牙设备
- 如何修改蓝牙搜索设备的时间
- 修改蓝牙搜索设备的时间
- Android蓝牙设备的配对,连接,搜索
- WM蓝牙开发指南 socket版
- android开发之蓝牙初步 扫描已配对蓝牙、更改蓝牙可见性、搜索外部蓝牙设备
- android开发之蓝牙初步 扫描已配对蓝牙、更改蓝牙可见性、搜索外部蓝牙设备
- 北京我的工作网网站
- Verilog HDL常用的行为仿真描述语句(六)
- 在右键菜单中添加“用记事本打开”命令
- 年轻的忠告
- 关于改造动态网站为生成静态页的方案
- WM蓝牙开发--搜索周围的设备
- .net 页面间的传值方法
- BULK INSERT用法详解,文本导入SQL,字段数量不等实例.
- 今天给CSDN的博客附加了【推荐主题】的功能
- 函数式编程魅力:Continuation-Passing Style,说为何 C#、C++ 引入 Lambda 意义重大及 C++ 和 C# 的缺憾
- private 的访问权限
- JAVA开发者最常去的20个英文网站
- 使用批处理命令实现文件共享
- 结束处理程序——Windows核心编程学习手札之二十三