re-advertise after disconnect (ESP32 & ESP32_NimBLE)

This commit is contained in:
lathoub 2021-09-22 22:42:12 +02:00
parent 927b3ac8be
commit e0afefb9a2
3 changed files with 155 additions and 134 deletions

View File

@ -170,6 +170,7 @@ protected:
{ {
if (_bleMidiTransport->_disconnectedCallback) if (_bleMidiTransport->_disconnectedCallback)
_bleMidiTransport->_disconnectedCallback(); _bleMidiTransport->_disconnectedCallback();
_central = nullptr; _central = nullptr;
} }

View File

@ -32,7 +32,7 @@ public:
void end() void end()
{ {
Serial.println("end");
} }
void write(uint8_t *buffer, size_t length) void write(uint8_t *buffer, size_t length)
@ -69,41 +69,53 @@ protected:
{ {
if (_bleMidiTransport->_disconnectedCallback) if (_bleMidiTransport->_disconnectedCallback)
_bleMidiTransport->_disconnectedCallback(); _bleMidiTransport->_disconnectedCallback();
end();
} }
}; };
class MyServerCallbacks: public BLEServerCallbacks { class MyServerCallbacks : public BLEServerCallbacks
{
public: public:
MyServerCallbacks(BLEMIDI_ESP32 *bluetoothEsp32) MyServerCallbacks(BLEMIDI_ESP32 *bluetoothEsp32)
: _bluetoothEsp32(bluetoothEsp32) { : _bluetoothEsp32(bluetoothEsp32)
{
} }
protected: protected:
BLEMIDI_ESP32 *_bluetoothEsp32 = nullptr; BLEMIDI_ESP32 *_bluetoothEsp32 = nullptr;
void onConnect(BLEServer*) { void onConnect(BLEServer *)
{
if (_bluetoothEsp32) if (_bluetoothEsp32)
_bluetoothEsp32->connected(); _bluetoothEsp32->connected();
}; };
void onDisconnect(BLEServer*) { void onDisconnect(BLEServer *server)
{
if (_bluetoothEsp32) if (_bluetoothEsp32)
_bluetoothEsp32->disconnected(); _bluetoothEsp32->disconnected();
server->getAdvertising()->start();
} }
}; };
class MyCharacteristicCallbacks: public BLECharacteristicCallbacks { class MyCharacteristicCallbacks : public BLECharacteristicCallbacks
{
public: public:
MyCharacteristicCallbacks(BLEMIDI_ESP32 *bluetoothEsp32) MyCharacteristicCallbacks(BLEMIDI_ESP32 *bluetoothEsp32)
: _bluetoothEsp32(bluetoothEsp32 ) { : _bluetoothEsp32(bluetoothEsp32)
{
} }
protected: protected:
BLEMIDI_ESP32 *_bluetoothEsp32 = nullptr; BLEMIDI_ESP32 *_bluetoothEsp32 = nullptr;
void onWrite(BLECharacteristic * characteristic) { void onWrite(BLECharacteristic *characteristic)
{
std::string rxValue = characteristic->getValue(); std::string rxValue = characteristic->getValue();
if (rxValue.length() > 0) { if (rxValue.length() > 0)
{
_bluetoothEsp32->receive((uint8_t *)(rxValue.c_str()), rxValue.length()); _bluetoothEsp32->receive((uint8_t *)(rxValue.c_str()), rxValue.length());
} }
} }
@ -131,8 +143,7 @@ bool BLEMIDI_ESP32::begin(const char* deviceName, BLEMIDI_Transport<class BLEMID
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_WRITE_NR BLECharacteristic::PROPERTY_WRITE_NR);
);
// Add CCCD 0x2902 to allow notify // Add CCCD 0x2902 to allow notify
_characteristic->addDescriptor(new BLE2902()); _characteristic->addDescriptor(new BLE2902());
@ -150,6 +161,8 @@ bool BLEMIDI_ESP32::begin(const char* deviceName, BLEMIDI_Transport<class BLEMID
_advertising->setAppearance(0x00); _advertising->setAppearance(0x00);
_advertising->start(); _advertising->start();
Serial.println("begin");
return true; return true;
} }

View File

@ -29,7 +29,6 @@ public:
void end() void end()
{ {
} }
void write(uint8_t *buffer, size_t length) void write(uint8_t *buffer, size_t length)
@ -70,38 +69,46 @@ protected:
} }
}; };
class MyServerCallbacks: public BLEServerCallbacks { class MyServerCallbacks : public BLEServerCallbacks
{
public: public:
MyServerCallbacks(BLEMIDI_ESP32_NimBLE *bluetoothEsp32) MyServerCallbacks(BLEMIDI_ESP32_NimBLE *bluetoothEsp32)
: _bluetoothEsp32(bluetoothEsp32) { : _bluetoothEsp32(bluetoothEsp32)
{
} }
protected: protected:
BLEMIDI_ESP32_NimBLE *_bluetoothEsp32 = nullptr; BLEMIDI_ESP32_NimBLE *_bluetoothEsp32 = nullptr;
void onConnect(BLEServer*) { void onConnect(BLEServer *)
{
if (_bluetoothEsp32) if (_bluetoothEsp32)
_bluetoothEsp32->connected(); _bluetoothEsp32->connected();
}; };
void onDisconnect(BLEServer*) { void onDisconnect(BLEServer *)
{
if (_bluetoothEsp32) if (_bluetoothEsp32)
_bluetoothEsp32->disconnected(); _bluetoothEsp32->disconnected();
} }
}; };
class MyCharacteristicCallbacks: public BLECharacteristicCallbacks { class MyCharacteristicCallbacks : public BLECharacteristicCallbacks
{
public: public:
MyCharacteristicCallbacks(BLEMIDI_ESP32_NimBLE *bluetoothEsp32) MyCharacteristicCallbacks(BLEMIDI_ESP32_NimBLE *bluetoothEsp32)
: _bluetoothEsp32(bluetoothEsp32 ) { : _bluetoothEsp32(bluetoothEsp32)
{
} }
protected: protected:
BLEMIDI_ESP32_NimBLE *_bluetoothEsp32 = nullptr; BLEMIDI_ESP32_NimBLE *_bluetoothEsp32 = nullptr;
void onWrite(BLECharacteristic * characteristic) { void onWrite(BLECharacteristic *characteristic)
{
std::string rxValue = characteristic->getValue(); std::string rxValue = characteristic->getValue();
if (rxValue.length() > 0) { if (rxValue.length() > 0)
{
_bluetoothEsp32->receive((uint8_t *)(rxValue.c_str()), rxValue.length()); _bluetoothEsp32->receive((uint8_t *)(rxValue.c_str()), rxValue.length());
} }
} }
@ -119,6 +126,7 @@ bool BLEMIDI_ESP32_NimBLE::begin(const char* deviceName, BLEMIDI_Transport<class
_server = BLEDevice::createServer(); _server = BLEDevice::createServer();
_server->setCallbacks(new MyServerCallbacks(this)); _server->setCallbacks(new MyServerCallbacks(this));
_server->advertiseOnDisconnect(true);
// Create the BLE Service // Create the BLE Service
auto service = _server->createService(BLEUUID(SERVICE_UUID)); auto service = _server->createService(BLEUUID(SERVICE_UUID));
@ -129,8 +137,7 @@ bool BLEMIDI_ESP32_NimBLE::begin(const char* deviceName, BLEMIDI_Transport<class
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ |
NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE |
NIMBLE_PROPERTY::NOTIFY | NIMBLE_PROPERTY::NOTIFY |
NIMBLE_PROPERTY::WRITE_NR NIMBLE_PROPERTY::WRITE_NR);
);
_characteristic->setCallbacks(new MyCharacteristicCallbacks(this)); _characteristic->setCallbacks(new MyCharacteristicCallbacks(this));