#include <Windows.h> #include <iostream> #include <fstream> #include <string> #include <vector> #include <algorithm> #include <cstdlib> #include <ctime> #include <conio.h> #include <winsock.h> #include <ws2bth.h> #include <bluetoothapis.h> #pragma comment(lib, "Bthprops.lib") #pragma comment(lib, "ws2_32.lib") using namespace std; const string CONFIG_FILE = "config.ini"; const int MAX_BLUETOOTH_DEVICES = 10; int main() { // 读取配置文件 ifstream config(CONFIG_FILE); if (!config.is_open()) { cout << "无法打开配置文件!" << endl; return -1; } string line; int search_count = 0; while (getline(config, line)) { if (line.find("search_count") != string::npos) { search_count = stoi(line.substr(line.find("=") + 1)); break; } } config.close(); // 初始化蓝牙 WSAData wsaData; int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != NO_ERROR) { cout << "WSAStartup 失败!" << endl; return -1; } // 枚举蓝牙设备 BLUETOOTH_DEVICE_SEARCH_PARAMS searchParams = { sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS) }; searchParams.fReturnAuthenticated = TRUE; searchParams.fReturnRemembered = TRUE; searchParams.fReturnUnknown = TRUE; searchParams.hRadio = NULL; BLUETOOTH_DEVICE_INFO deviceInfo = { sizeof(BLUETOOTH_DEVICE_INFO) }; HBLUETOOTH_DEVICE_FIND deviceFindHandle; vector<BLUETOOTH_DEVICE_INFO> devices; deviceFindHandle = BluetoothFindFirstDevice(&searchParams, &deviceInfo); if (deviceFindHandle != NULL) { do { devices.push_back(deviceInfo); } while (BluetoothFindNextDevice(deviceFindHandle, &deviceInfo)); BluetoothFindDeviceClose(deviceFindHandle); } // 检查蓝牙设备数量 if (devices.size() >= search_count) { ofstream file("CheckBT.log"); file << "PASS"; // 向文档中写入"PASS"字符串 } else { ofstream file("CheckBT.log"); file << "FAIL"; // 向文档中写入"PASS"字符串 } // 清理蓝牙 WSACleanup(); return 0; } 优化这段代码,循环搜索15秒蓝牙
时间: 2023-06-23 20:08:30 浏览: 82
#include <iostream>
以下是优化后的代码:
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <winsock.h>
#include <ws2bth.h>
#include <bluetoothapis.h>
#pragma comment(lib, "Bthprops.lib")
#pragma comment(lib, "ws2_32.lib")
using namespace std;
const string CONFIG_FILE = "config.ini";
const int MAX_BLUETOOTH_DEVICES = 10;
int main() {
// 读取配置文件
ifstream config(CONFIG_FILE);
if (!config.is_open()) {
cout << "无法打开配置文件!" << endl;
return -1;
}
string line;
int search_time = 0;
while (getline(config, line)) {
if (line.find("search_time") != string::npos) {
search_time = stoi(line.substr(line.find("=") + 1));
break;
}
}
config.close();
// 初始化蓝牙
WSAData wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
cout << "WSAStartup 失败!" << endl;
return -1;
}
// 枚举蓝牙设备
BLUETOOTH_DEVICE_SEARCH_PARAMS searchParams = { sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS) };
searchParams.fReturnAuthenticated = TRUE;
searchParams.fReturnRemembered = TRUE;
searchParams.fReturnUnknown = TRUE;
searchParams.hRadio = NULL;
BLUETOOTH_DEVICE_INFO deviceInfo = { sizeof(BLUETOOTH_DEVICE_INFO) };
HBLUETOOTH_DEVICE_FIND deviceFindHandle;
vector<BLUETOOTH_DEVICE_INFO> devices;
time_t startTime, currentTime;
time(&startTime);
while (true) {
deviceFindHandle = BluetoothFindFirstDevice(&searchParams, &deviceInfo);
if (deviceFindHandle != NULL) {
do {
devices.push_back(deviceInfo);
} while (BluetoothFindNextDevice(deviceFindHandle, &deviceInfo));
BluetoothFindDeviceClose(deviceFindHandle);
}
time(¤tTime);
if (currentTime - startTime >= search_time) {
break;
}
}
// 检查蓝牙设备数量
if (devices.size() >= MAX_BLUETOOTH_DEVICES) {
ofstream file("CheckBT.log");
file << "PASS"; // 向文档中写入"PASS"字符串
} else {
ofstream file("CheckBT.log");
file << "FAIL"; // 向文档中写入"FAIL"字符串
}
// 清理蓝牙
WSACleanup();
return 0;
}
优化后的代码使用了时间循环,通过计算时间来限定循环的次数。同时,将每次搜索蓝牙的结果存储在一个 vector 中,并在循环结束后检查 vector 中蓝牙设备的数量。将搜索时间和最大蓝牙设备数设置为常量,使代码更易于维护和修改。最后,根据检查结果向文档中写入相应的字符串。
阅读全文