-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Closed
Labels
Area: BLEIssues related to BLEIssues related to BLE
Description
Board
ESP32-S3-dev
Device Description
nothing
Hardware Configuration
nothing
Version
v2.0.9
IDE Name
Arduino IDE
Operating System
windows 10
Flash frequency
80
PSRAM enabled
yes
Upload speed
921600
Description
I write some code to connect a BLE heart rate device.
then I found a problem,here is the issuse code:
#if 1
pBLEScan->start(5, false);
connectToServer();
#else
pBLEScan->start(5, scanEndedCB);
#endif
If use top code it works good ,can connect device success and recieve data from device, here is some log;
BLE Advertised Device found: Name: XOSS_HRM_0010228, Address: f4:6b:70:b2:95:51, manufacturer data: 585aa1, serviceUUID: 0000180d-0000-1000-8000-00805f9b34fb, rssi: -47
Forming a connection to f4:6b:70:b2:95:51
- Created client
- Connected to server
- Found our service
[ 8608][E][BLERemoteCharacteristic.cpp:289] retrieveDescriptors(): esp_ble_gattc_get_all_descr: Unknown
- Found our characteristic
heartRate is: 64
heartRate is: 64
heartRate is: 64
heartRate is: 65
If use below code, In scanEndedCB function I check the device if exist and connect to it, but the code dosen't works, it will stuck
in connect function for a long time and can't connect success.
Sketch
#include "Arduino.h"
#include "BLEDevice.h"
static BLEUUID serviceUUID("180d");
static BLEUUID charUUID("2a37");
static bool isConnected = false;
static BLERemoteCharacteristic *pRemoteCharacteristic;
static BLEAdvertisedDevice *myDevice;
int scanTime = 5;
static void notifyCallback(
BLERemoteCharacteristic *pBLERemoteCharacteristic,
uint8_t *pData,
size_t length,
bool isNotify) {
printf("heartRate is: %d\n", *++pData);
}
void scanEndedCB(BLEScanResults results);
class MyClientCallback : public BLEClientCallbacks {
void onConnect(BLEClient *pclient) {
}
void onDisconnect(BLEClient *pclient) {
Serial.println("onDisconnect");
isConnected = false;
BLEDevice::getScan()->start(scanTime, scanEndedCB, false);
}
};
static inline bool connectToServer() {
Serial.print("Forming a connection to ");
Serial.println(myDevice->getAddress().toString().c_str());
BLEClient *pClient = BLEDevice::createClient();
Serial.println(" - Created client");
pClient->setClientCallbacks(new MyClientCallback());
pClient->connect(myDevice);
Serial.println(" - Connected to server");
BLERemoteService *pRemoteService = pClient->getService(serviceUUID);
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found our service");
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
if (pRemoteCharacteristic == nullptr) {
Serial.print("Failed to find our characteristic UUID: ");
Serial.println(charUUID.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found our characteristic");
if (pRemoteCharacteristic->canNotify())
pRemoteCharacteristic->registerForNotify(notifyCallback);
isConnected = true;
return true;
}
/**
* Scan for BLE servers and find the first one that advertises the service we are looking for.
*/
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) {
Serial.print("BLE Advertised Device found: ");
Serial.println(advertisedDevice.toString().c_str());
myDevice = new BLEAdvertisedDevice(advertisedDevice);
}
}
};
void scanEndedCB(BLEScanResults resultList) {
printf("scanCount is %d\n", resultList.getCount());
Serial.println("Scan Ended");
for (int i = 0; i < resultList.getCount(); i++) {
BLEAdvertisedDevice device = resultList.getDevice(i);
if (device.haveServiceUUID() && device.isAdvertisingService(serviceUUID)) {
myDevice = new BLEAdvertisedDevice(device);
}
}
if (myDevice != nullptr) {
if (!isConnected) {
connectToServer();
}
} else {
BLEDevice::getScan()->start(scanTime, scanEndedCB);
}
}
void setup() {
Serial.begin(115200);
BLEDevice::init("");
BLEScan *pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setInterval(1349);
pBLEScan->setWindow(449);
pBLEScan->setActiveScan(true);
#if 1
pBLEScan->start(5, false);
connectToServer();
#else
pBLEScan->start(5, scanEndedCB);
#endif
}
void loop() {
delay(100);
}Debug Message
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x15 (USB_UART_CHIP_RESET),boot:0x2b (SPI_FAST_FLASH_BOOT)
Saved PC:0x400490cc
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3808,len:0x44c
load:0x403c9700,len:0xbe4
load:0x403cc700,len:0x2a38
entry 0x403c98d4
BLE Advertised Device found: Name: XOSS_HRM_0010228, Address: f4:6b:70:b2:95:51, manufacturer data: 585aa1, serviceUUID: 0000180d-0000-1000-8000-00805f9b34fb, rssi: -58
scanCount is 39
Scan Ended
Forming a connection to f4:6b:70:b2:95:51
- Created client
Other Steps to Reproduce
I run these code in three different esp32-s3 dev board, all the same problem. I can't find the problem's source.
I have checked existing issues, online documentation and the Troubleshooting Guide
- I confirm I have checked existing issues, online documentation and Troubleshooting guide.
Metadata
Metadata
Assignees
Labels
Area: BLEIssues related to BLEIssues related to BLE