Description
I got most of the code from nkolban with minimal changes for reading an analog value and relaying to a cordova app.
I can connect and get values; however, what seems like after 5 seconds to 2 minutes, I get an error on the cordova app that "The connection has timed out". I similarly get the nRF Connect app to connect and read values but all of a sudden it disconnects in a similar way so I don't think it's the phone software.
I have tried updating the arduino library as stated by replacing this repository (nkolban cpp_utils), restarting arduino, but it didn't work.
Any ideas? Did I do something stupid or should I test anything? I would like to avoid constantly reconnecting if possible.
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLECharacteristic* pCharacteristic = NULL;
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "F8D61858-DD19-4CED-B921-6E6CCE526D48"
#define CHARACTERISTIC_UUID "9F576249-8BE9-40B0-B009-C31FB8781207"
#define DESCRIPTOR_UUID "a928d2aa-d48e-45aa-a8c1-51e2b44a7c45"
bool deviceConnected = false;
bool oldDeviceConnected = false;
//uint8_t value = 0;
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void setup() {
BLEServer* pServer = NULL;
BLEDescriptor* pDescriptor = NULL;
// Create the BLE Device
BLEDevice::init("ESP32");
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_NOTIFY
);
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
// Create a BLE Descriptor
pCharacteristic->addDescriptor(new BLE2902());
// Start the service
pService->start();
// Start advertising
pServer->getAdvertising()->addServiceUUID(pService->getUUID());
pServer->getAdvertising()->start();
}
void loop() {
int analogValue = 0;
// notify changed value
if (deviceConnected) {
pCharacteristic->setValue(analogValue);
pCharacteristic->notify();
delay(250); // bluetooth stack will go into congestion, if too many packets are sent
}
....
}
Here is the verbose log from arduino
[D][BLECharacteristic.cpp:664] setValue(): >> setValue: length=4, data=ae070000, characteristic UUID=9f576249-8be9-40b0-b009-c31fb8781207
[D][BLECharacteristic.cpp:671] setValue(): << setValue
[D][BLECharacteristic.cpp:524] notify(): >> notify: length: 4
[D][BLEDevice.cpp:96] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... ESP_GATTS_CONF_EVT
[D][BLEUtils.cpp:1647] dumpGattServerEvent(): GATT ServerEvent: ESP_GATTS_CONF_EVT
[D][BLEUtils.cpp:1692] dumpGattServerEvent(): [status: ESP_GATT_OK, conn_id: 0x00]
[D][BLEServer.cpp:177] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONF_EVT
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONF_EVT
[D][BLECharacteristic.cpp:566] notify(): << notify
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLEServer.cpp:295] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLEDevice.cpp:96] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... ESP_GATTS_DISCONNECT_EVT
[D][BLEUtils.cpp:1647] dumpGattServerEvent(): GATT ServerEvent: ESP_GATTS_DISCONNECT_EVT
[D][BLEUtils.cpp:1723] dumpGattServerEvent(): [conn_id: 0, remote_bda: 65:c3:41:74:59:c6]
[D][BLEServer.cpp:177] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_DISCONNECT_EVT
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_DISCONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent