#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, "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() >= MAX_BLUETOOTH_DEVICES) { ofstream file("CheckBT.log"); file << "PASS"; // 向文档中写入"PASS"字符串 } else { ofstream file("CheckBT.log"); file << "FAIL"; // 向文档中写入"FAIL"字符串 } // 清理蓝牙 WSACleanup(); return 0; }改进这段代码,把每个蓝牙设备名称输出到文档中
时间: 2023-06-17 20:08:55 浏览: 123
#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, "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);
}
// 输出蓝牙设备名称到文档
ofstream file("CheckBT.log");
if (devices.size() >= MAX_BLUETOOTH_DEVICES) {
file << "PASS" << endl;
} else {
file << "FAIL" << endl;
}
for (auto device : devices) {
file << device.szName << endl;
}
// 清理蓝牙
WSACleanup();
return 0;
}
阅读全文