[12.07已解决]大家好,我是iceshadow,最近我在做室内导航系统,以10个ESP32串接而成的低功耗蓝牙(Bluetooth Low Energy, BLE)感应系统加上iOS app介面,用iBeacon当作导航器协助导航。其中感应系统是用Arduino去撰写的,但一直有语法过不了的问题。
这边是全部的程式码:
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEClient.h>
#include <unordered_map>
#include <string>
#include <iostream>
#define TARGET_DEVICES 10
// 目标设备的名称和对应的 MAC 地址
const char *targetDeviceNames[TARGET_DEVICES] = {
"ESP32_Device_1", "ESP32_Device_2", "ESP32_Device_3", "ESP32_Device_4",
"ESP32_Device_5", "ESP32_Device_6", "ESP32_Device_7", "ESP32_Device_8",
"ESP32_Device_9", "ESP32_Device_10"};
// 目标设备的 MAC 地址
const char *targetMacAddresses[TARGET_DEVICES] = {
"CC:AA:00:33:66:77", "CC:AA:00:33:66:78", "CC:AA:00:33:66:79", "CC:AA:00:33:66:7A",
"CC:AA:00:33:66:7B", "CC:AA:00:33:66:7C", "CC:AA:00:33:66:7D", "CC:AA:00:33:66:7E",
"CC:AA:00:33:66:7F", "CC:AA:00:33:66:80"};
// 储存目标设备是否已找到
std::unordered_map<std::string, bool> foundDevices;
// BLE 扫描物件
BLEScan *pBLEScan;
int scanTime = 5; // 扫描时间(秒)
// 初始化目标设备的导航状态
void initializeFoundDevices() {
for (int i = 0; i < TARGET_DEVICES; i++) {
foundDevices[targetMacAddresses[i]] = false; // 设定初始状态为未找到
}
}
// 封装的扫描函数,带有 MAC 地址筛选逻辑
BLEScanResults someFunctionThatReturnsBLEScanResults() {
pBLEScan->setActiveScan(true); // 启用主动扫描
Serial.println("开始扫描 BLE 装置...");
// 使用非指标类型接收结果
BLEScanResults* results = pBLEScan->start(scanTime, false); // 使用指标类型
Serial.printf("找到 %d 个装置\\n", results->getCount());
for (int i = 0; i < results->getCount(); i++) {
BLEAdvertisedDevice device = results->getDevice(i);
std::string macAddressStd = std::string(device.getAddress().toString().c_str());
// 检查是否为目标设备
if (foundDevices.find(macAddressStd) != foundDevices.end() && !foundDevices[macAddressStd]) {
foundDevices[macAddressStd] = true; // 标记为已找到
Serial.printf("找到目标设备: Name=%s, Address=%s\\n", device.getName().c_str(),
macAddressStd.c_str());
}
}
return results; // 返回扫描结果
}
// 依照 MAC 地址顺序导航设备
void navigateDevices() {
for (int i = 0; i < TARGET_DEVICES; i++) {
std::string macAddress = targetMacAddresses[i];
// 如果设备已找到,则执行导航逻辑
if (foundDevices[macAddress]) {
Serial.printf("导航至设备: Name=%s, Address=%s\\n", targetDeviceNames[i], macAddress.c_str());
// 执行导航操作或其他逻辑
} else {
Serial.printf("设备未找到: Name=%s, Address=%s\\n", targetDeviceNames[i], macAddress.c_str());
}
}
}
void setup() {
Serial.begin(115200);
BLEDevice::init("");
pBLEScan = BLEDevice::getScan();
initializeFoundDevices(); // 初始化目标设备的导航状态
}
void loop() {
// 扫描设备
BLEScanResults* scanResults = someFunctionThatReturnsBLEScanResults();
// 列出所有扫描到的设备
for (int i = 0; i < scanResults->getCount(); i++) {
BLEAdvertisedDevice device = scanResults->getDevice(i);
Serial.printf("Device found: Name=%s, Address=%s\\n",
device.getName().c_str(),
device.getAddress().toString().c_str());
}
// 按顺序导航目标设备
navigateDevices();
// 清除扫描结果,準备下一轮
pBLEScan->clearResults();
Serial.println("等待 7 秒后再次扫描...");
delay(7000); // 等待 7 秒
}
会有上面的写法是因为,我想要以物件的方式直接以一个中控去感应其他9个ESP32去维持这个系统的畅通,然后再跟自己写好的iOS导航应用程式与iBeacon串接,不过常常在指标的地方翻车。
出错的Code如下:
/Users/lilywang/Library/CloudStorage/OneDrive-台北医学大学/论文研究方向/学位考试/project/Arduino/Central/Central.ino: In function \'BLEScanResults someFunctionThatReturnsBLEScanResults()\':
/Users/lilywang/Library/CloudStorage/OneDrive-台北医学大学/论文研究方向/学位考试/project/Arduino/Central/Central.ino:58:12: error: could not convert \'results\' from \'BLEScanResults*\' to \'BLEScanResults\'
58 | return results; // 返回扫描结果
| ^~~~~~~
| |
| BLEScanResults*
/Users/lilywang/Library/CloudStorage/OneDrive-台北医学大学/论文研究方向/学位考试/project/Arduino/Central/Central.ino: In function \'void loop()\':
/Users/lilywang/Library/CloudStorage/OneDrive-台北医学大学/论文研究方向/学位考试/project/Arduino/Central/Central.ino:86:72: error: cannot convert \'BLEScanResults\' to \'BLEScanResults*\' in initialization
86 | BLEScanResults* scanResults = someFunctionThatReturnsBLEScanResults();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
| |
| BLEScanResults
exit status 1
Compilation error: could not convert \'results\' from \'BLEScanResults*\' to \'BLEScanResults\'
问过ChatGPT、查过Stackoverflow跟Arduino的论坛,仍然不断地循环出错,不知道是我的环境问题还是语法问题,这边想要请教各路大神,解救硕士生脱离硕论苦海感谢看到这篇文章的大家。
以下是我查过的资源,提供大家参考:https://forum.arduino.cc/t/error-no-match-for-operator-operand-types-are-eeref-and-string/875704https://forum.arduino.cc/t/ble-scan-issue-blescan-does-not-name-a-type/1062924/7https://blog.csdn.net/qq_35630119/article/details/122628094