Windows原生蓝牙编程 第三章 配对后进行蓝牙通信【C++】 您所在的位置:网站首页 蓝牙连接成功文件传输失败 Windows原生蓝牙编程 第三章 配对后进行蓝牙通信【C++】

Windows原生蓝牙编程 第三章 配对后进行蓝牙通信【C++】

#Windows原生蓝牙编程 第三章 配对后进行蓝牙通信【C++】| 来源: 网络整理| 查看: 265

蓝牙系列文章目录 第一章 获取本地蓝牙并扫描周围蓝牙信息并输出 第二章 选取设备输入配对码并配对 第三章 配对后进行蓝牙通信

文章目录 前言头文件一、建立连接套接字二、设置发送信息函数三、全部代码四、测试服务端选择及蓝牙通信总结

前言

接着第二章,我们已经配对完成,接下来就是通过winsock进行通信了。

头文件 #define _CRT_SECURE_NO_WARNINGS #pragma warning(disable : 4995) #include #include #include #include #include #include #include #include #include #include #include #include #pragma comment(lib, "wsock32.lib") #pragma comment(lib, "bthprops.lib") #pragma comment(lib,"ws2_32.lib") using namespace std; string PassKey; HANDLE btdi; vector devices; BLUETOOTH_DEVICE_INFO device; SOCKET sockClient; WSADATA wsaData;

我又加了个SOCKET全局变量来保存套接字和WSADATA wsaData用于初始化套接字库。

一、建立连接套接字 bool buildConnect(BLUETOOTH_DEVICE_INFO device, SOCKET& sockClient) { if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { cout } else { cout cout message; int iCount = 0; if (sendMessage(client_socket, message, iCount)) { cout break; } else { cout wstring_convert converter; string utf8String = converter.to_bytes(converter.from_bytes(message)); iCount = send(sockClient, utf8String.data() + sum, utf8String.size() - sum, 0); if (iCount == SOCKET_ERROR) { cout string curLocale = setlocale(LC_ALL, NULL); setlocale(LC_ALL, "chs"); const wchar_t* _Source = ws.c_str(); size_t _Dsize = 2 * ws.size() + 1; char* _Dest = new char[_Dsize]; memset(_Dest, 0, _Dsize); wcstombs(_Dest, _Source, _Dsize); string result = _Dest; delete[]_Dest; setlocale(LC_ALL, curLocale.c_str()); return result; } string String_To_UTF8(const std::string& str) { int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0); wchar_t* pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴 ZeroMemory(pwBuf, nwLen * 2 + 2); ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen); int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL); char* pBuf = new char[nLen + 1]; ZeroMemory(pBuf, nLen + 1); ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL); std::string retStr(pBuf); delete[]pwBuf; delete[]pBuf; pwBuf = NULL; pBuf = NULL; return retStr; } string getMAC(BLUETOOTH_ADDRESS Daddress) { /*string addr; addr = addr.sprintf("%02x:%02x:%02x:%02x:%02x:%02x", Daddress.rgBytes[5], Daddress.rgBytes[4], Daddress.rgBytes[3], Daddress.rgBytes[2], Daddress.rgBytes[1], Daddress.rgBytes[0]); return addr;*/ ostringstream oss; oss oss sizeof(BLUETOOTH_FIND_RADIO_PARAMS) }; //调用BluetoothFindFirstDevice搜索本机蓝牙收发器所需要的搜索参数对象 BLUETOOTH_RADIO_INFO bri = { sizeof(BLUETOOTH_RADIO_INFO) }; //初始化一个储存蓝牙收发器信息(BLUETOOTH_RADIO_INFO)的对象bri BLUETOOTH_DEVICE_SEARCH_PARAMS btsp = { sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS) };//调用BluetoothFindFirstDevice搜索本所需要的搜索参数对象 BLUETOOTH_DEVICE_INFO btdi = { sizeof(BLUETOOTH_DEVICE_INFO) }; //初始化一个远程蓝牙设备信息(BLUETOOTH_DEVICE_INFO)对象btdi,以储存搜索到的蓝牙设备信息 hbf = BluetoothFindFirstRadio(&btfrp, &hbr); //得到第一个被枚举的蓝牙收发器的句柄hbf可用于BluetoothFindNextRadio,hbr可用于BluetoothFindFirstDevice。若没有找到本机的蓝牙收发器,则得到的句柄hbf=NULL vector res; bool brfind = hbf != NULL; while (brfind) { if (BluetoothGetRadioInfo(hbr, &bri) == ERROR_SUCCESS)//获取蓝牙收发器的信息,储存在bri中 { cout cout PassKey; cout wss switch (result) { case ERROR_CANCELLED: cout cout cout } else { cout cout message; int iCount = 0; if (sendMessage(client_socket, message, iCount)) { cout break; } else { cout buildConnect(device, sockClient); } return; } 四、测试服务端选择及蓝牙通信

随便在网上下载了个蓝牙测试的app 在这里插入图片描述 测试一下,选择要连接的设备并且输入配对码 在这里插入图片描述 连接成功的同时手机显示连接成功 在这里插入图片描述 发送两条信息 在这里插入图片描述

同时手机收到 在这里插入图片描述 返回发送字符数那有点问题,因为转成utf-8格式,一个中文占3个字符,我懒得改了,不过测试结果还是很成功的,完美实现蓝牙通信。

总结

如果您觉得有用的话,务必点赞评论收藏一波,万分感谢咯!=v=



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有