我有 ESP32 的以下代码
#include <BLEDevice.h> //Header file for BLE
static BLEUUID serviceUUID("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); //Service UUID фитнес браслета, полученный с помощью приложения nRF connect
static BLEUUID charUUID("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); //Characteristic UUID фитнес браслета, полученный с помощью приложения nRF connect
String My_BLE_Address = "ff:ff:ff:ff:ff:ff"; //аппаратный Bluetooth MAC нашего фитнес браслета, полученный с помощью приложения nRF connect. Будет отличаться для каждого браслета
static BLERemoteCharacteristic* pRemoteCharacteristic;
BLEScan* pBLEScan; //Name the scanning device as pBLEScan
BLEScanResults foundDevices;
static BLEAddress *Server_BLE_Address;
String Scaned_BLE_Address;
boolean paired = false; //boolean variable to togge light (логическая переменная для включения/выключения света)
bool connectToserver (BLEAddress pAddress){
BLEClient* pClient = BLEDevice::createClient();
Serial.println(" - Created client");
// Connect to the BLE Server. (соединение с BLE сервером)
pClient->connect(pAddress);
Serial.println(" - Connected to fitnessband");
// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
if (pRemoteService != nullptr)
{
Serial.println(" - Found our service");
return true;
}
else
return false;
// Obtain a reference to the characteristic in the service of the remote BLE server.
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
if (pRemoteCharacteristic != nullptr)
Serial.println(" - Found our characteristic");
return true;
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
{
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Scan Result: %s \n", advertisedDevice.toString().c_str());
Server_BLE_Address = new BLEAddress(advertisedDevice.getAddress());
Scaned_BLE_Address = Server_BLE_Address->toString().c_str();
}
};
void setup() {
Serial.begin(115200); //инициализируем последовательную связь
Serial.println("ESP32 BLE Server program"); //печатаем приветственное сообщение
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //начинаем новое сканирование
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); //Call the class that is defined above
pBLEScan->setActiveScan(true); // активное сканирование потребляет больше мощности, но быстрее получает результат
pinMode (13,OUTPUT); //задаем режим работы для контакта на вывод данных
}
void loop() {
foundDevices = pBLEScan->start(3); // сканируем в течение 3-х секунд чтобы найти фитнес браслет
while (foundDevices.getCount() >= 1)
{
if (Scaned_BLE_Address == My_BLE_Address && paired == false)
{
Serial.println("Found Device :-)... connecting to Server as client");
if (connectToserver(*Server_BLE_Address))
{
paired = true;
Serial.println("********************LED turned ON************************");
digitalWrite (13,HIGH);
break;
}
else
{
Serial.println("Pairing failed");
break;
}
}
if (Scaned_BLE_Address == My_BLE_Address && paired == true)
{
Serial.println("Our device went out of range"); // устройство вне диапазона действия
paired = false;
Serial.println("********************LED OOOFFFFF************************");
digitalWrite (13,LOW);
ESP.restart();
break;
}
else
{
Serial.println("We have some other BLe device in range"); //некоторые BLe устройства в зоне нашего действия
break;
}
}
}
编译时,程序会产生以下错误日志:
AppData\Local\Temp\.arduinoIDE-unsaved2024817-13824-28fsi2.1i74u\sketch_sep17a\sketch_sep17a.ino: In function 'void loop()':
AppData\Local\Temp\.arduinoIDE-unsaved2024817-13824-28fsi2.1i74u\sketch_sep17a\sketch_sep17a.ino:54:35: error: no match for 'operator=' (operand types are 'BLEScanResults' and 'BLEScanResults*')
**54 | foundDevices = pBLEScan->start(5); // сканируем в течение 5-х секунд чтобы найти фитнес браслет**
| ^
In file included from AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.4\libraries\BLE\src/BLEAdvertisedDevice.h:20,
from AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.4\libraries\BLE\src/BLEClient.h:25,
from AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.4\libraries\BLE\src/BLEDevice.h:22,
from AppData\Local\Temp\.arduinoIDE-unsaved2024817-13824-28fsi2.1i74u\sketch_sep17a\sketch_sep17a.ino:1:
AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.4\libraries\BLE\src/BLEScan.h:48:7: note: candidate: 'BLEScanResults& BLEScanResults::operator=(const BLEScanResults&)'
**48 | class BLEScanResults {
| ^~~~~~~~~~~~~~**
AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.4\libraries\BLE\src/BLEScan.h:48:7: note: no known conversion for argument 1 from 'BLEScanResults*' to 'const BLEScanResults&'
AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.4\libraries\BLE\src/BLEScan.h:48:7: note: candidate: 'BLEScanResults& BLEScanResults::operator=(BLEScanResults&&)'
AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.4\libraries\BLE\src/BLEScan.h:48:7: note: no known conversion for argument 1 from 'BLEScanResults*' to 'BLEScanResults&&'
Используем библиотеку BLE версии 3.0.4 из папки: AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.4\libraries\BLE
exit status 1
**Compilation error: no match for 'operator=' (operand types are 'BLEScanResults' and 'BLEScanResults*')**