#include <Winsock2.h> #include <Windows.h> #include <iostream> #include <fstream> #include <string> #include <sstream> #include <vector> #include <map> #pragma comment(lib, "ws2_32.lib") std::map<std::string, std::string> readIniFile(std::string fileName) { std::map<std::string, std::string> configMap; std::ifstream file(fileName); if (file.is_open()) { std::string line; std::string section = ""; while (getline(file, line)) { if (line.empty()) { continue; } if (line[0] == '[' && line[line.length() - 1] == ']') { section = line.substr(1, line.length() - 2); } else { std::stringstream ss(line); std::string key, value; getline(ss, key, '='); getline(ss, value); configMap[section + "." + key] = value; } } file.close(); } return configMap; } void writeLogFile(std::string fileName, std::string logText) { std::ofstream file(fileName, std::ios_base::app); if (file.is_open()) { file << logText << std::endl; file.close(); } } int main() { std::map<std::string, std::string> config = readIniFile("config.ini"); int bluetoothCount = std::stoi(config["bluetooth.count"]); std::string logFileName = config["log.filename"]; WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { std::cout << "WSAStartup failed: " << iResult << std::endl; return 1; } WSAQUERYSET service; memset(&service, 0, sizeof(service)); service.dwSize = sizeof(service); service.dwNameSpace = NS_BTH; HANDLE lookupHandle = NULL; iResult = WSALookupServiceBegin(&service, LUP_CONTAINERS, &lookupHandle); if (iResult != 0) { std::cout << "WSALookupServiceBegin failed: " << iResult << std::endl; WSACleanup(); return 1; } int count = 0; WSAQUERYSET* pResult = (WSAQUERYSET*)LocalAlloc(LPTR, sizeof(WSAQUERYSET)); while (count < bluetoothCount) { DWORD dwSize = sizeof(WSAQUERYSET); iResult = WSALookupServiceNext(lookupHandle, LUP_RETURN_NAME | LUP_RETURN_ADDR, &dwSize, pResult); if (iResult != 0) { break; } count++; } LocalFree(pResult); WSALookupServiceEnd(lookupHandle); WSACleanup(); if (count >= bluetoothCount) { std::string logText = "Bluetooth count is " + std::to_string(count) + ", reached the target count of " + std::to_string(bluetoothCount); writeLogFile(logFileName, logText); } else { std::string logText = "Bluetooth count is " + std::to_string(count) + ", did not reach the target count of " + std::to_string(bluetoothCount); writeLogFile(logFileName, logText); } return 0; } 给这段代码搜索蓝牙设备加上搜索时间
时间: 2023-06-17 14:07:53 浏览: 168
C语言头文件 WINSOCK2
5星 · 资源好评率100%
可以在搜索蓝牙设备的while循环中添加一个计时器,当计时器达到设定的搜索时间时,跳出循环。
例如,在main函数中添加一个变量searchTime,表示搜索蓝牙设备的最大时间,单位为秒。然后在while循环中添加一个计时器,每次循环结束将已经搜索的时间加上循环间隔(例如1秒),如果搜索时间超过了searchTime,就跳出循环。
修改后的代码如下:
int main() {
std::map<std::string, std::string> config = readIniFile("config.ini");
int bluetoothCount = std::stoi(config["bluetooth.count"]);
std::string logFileName = config["log.filename"];
int searchTime = std::stoi(config["bluetooth.searchtime"]); // 添加搜索时间配置项
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
std::cout << "WSAStartup failed: " << iResult << std::endl;
return 1;
}
WSAQUERYSET service;
memset(&service, 0, sizeof(service));
service.dwSize = sizeof(service);
service.dwNameSpace = NS_BTH;
HANDLE lookupHandle = NULL;
iResult = WSALookupServiceBegin(&service, LUP_CONTAINERS, &lookupHandle);
if (iResult != 0) {
std::cout << "WSALookupServiceBegin failed: " << iResult << std::endl;
WSACleanup();
return 1;
}
int count = 0;
WSAQUERYSET* pResult = (WSAQUERYSET*)LocalAlloc(LPTR, sizeof(WSAQUERYSET));
int searchCount = 0; // 添加搜索计数器
while (searchCount < searchTime) { // 添加计时器
DWORD dwSize = sizeof(WSAQUERYSET);
iResult = WSALookupServiceNext(lookupHandle, LUP_RETURN_NAME | LUP_RETURN_ADDR, &dwSize, pResult);
if (iResult != 0) {
break;
}
count++;
searchCount++; // 每次循环增加已搜索时间
Sleep(1000); // 等待1秒,再进行下一次搜索
if (searchCount >= searchTime) { // 如果已经搜索了设定的时间,跳出循环
break;
}
}
LocalFree(pResult);
WSALookupServiceEnd(lookupHandle);
WSACleanup();
if (count >= bluetoothCount) {
std::string logText = "Bluetooth count is " + std::to_string(count) + ", reached the target count of " + std::to_string(bluetoothCount);
writeLogFile(logFileName, logText);
}
else {
std::string logText = "Bluetooth count is " + std::to_string(count) + ", did not reach the target count of " + std::to_string(bluetoothCount);
writeLogFile(logFileName, logText);
}
return 0;
}
阅读全文