re-advertise after disconnect (ESP32 & ESP32_NimBLE)
This commit is contained in:
parent
927b3ac8be
commit
e0afefb9a2
|
|
@ -170,6 +170,7 @@ protected:
|
|||
{
|
||||
if (_bleMidiTransport->_disconnectedCallback)
|
||||
_bleMidiTransport->_disconnectedCallback();
|
||||
|
||||
_central = nullptr;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,11 +11,11 @@ BEGIN_BLEMIDI_NAMESPACE
|
|||
class BLEMIDI_ESP32
|
||||
{
|
||||
private:
|
||||
BLEServer* _server = nullptr;
|
||||
BLEAdvertising* _advertising = nullptr;
|
||||
BLECharacteristic* _characteristic = nullptr;
|
||||
BLEServer *_server = nullptr;
|
||||
BLEAdvertising *_advertising = nullptr;
|
||||
BLECharacteristic *_characteristic = nullptr;
|
||||
|
||||
BLEMIDI_Transport<class BLEMIDI_ESP32>* _bleMidiTransport = nullptr;
|
||||
BLEMIDI_Transport<class BLEMIDI_ESP32> *_bleMidiTransport = nullptr;
|
||||
|
||||
friend class MyServerCallbacks;
|
||||
friend class MyCharacteristicCallbacks;
|
||||
|
|
@ -24,24 +24,24 @@ protected:
|
|||
QueueHandle_t mRxQueue;
|
||||
|
||||
public:
|
||||
BLEMIDI_ESP32()
|
||||
BLEMIDI_ESP32()
|
||||
{
|
||||
}
|
||||
|
||||
bool begin(const char*, BLEMIDI_Transport<class BLEMIDI_ESP32>*);
|
||||
bool begin(const char *, BLEMIDI_Transport<class BLEMIDI_ESP32> *);
|
||||
|
||||
void end()
|
||||
{
|
||||
|
||||
Serial.println("end");
|
||||
}
|
||||
|
||||
void write(uint8_t* buffer, size_t length)
|
||||
void write(uint8_t *buffer, size_t length)
|
||||
{
|
||||
_characteristic->setValue(buffer, length);
|
||||
_characteristic->notify();
|
||||
}
|
||||
|
||||
bool available(byte* pvBuffer)
|
||||
bool available(byte *pvBuffer)
|
||||
{
|
||||
return xQueueReceive(mRxQueue, pvBuffer, 0); // return immediately when the queue is empty
|
||||
}
|
||||
|
|
@ -53,65 +53,77 @@ public:
|
|||
}
|
||||
|
||||
protected:
|
||||
void receive(uint8_t* buffer, size_t length)
|
||||
{
|
||||
void receive(uint8_t *buffer, size_t length)
|
||||
{
|
||||
// parse the incoming buffer
|
||||
_bleMidiTransport->receive(buffer, length);
|
||||
}
|
||||
}
|
||||
|
||||
void connected()
|
||||
{
|
||||
if (_bleMidiTransport->_connectedCallback)
|
||||
_bleMidiTransport->_connectedCallback();
|
||||
}
|
||||
void connected()
|
||||
{
|
||||
if (_bleMidiTransport->_connectedCallback)
|
||||
_bleMidiTransport->_connectedCallback();
|
||||
}
|
||||
|
||||
void disconnected()
|
||||
{
|
||||
if (_bleMidiTransport->_disconnectedCallback)
|
||||
_bleMidiTransport->_disconnectedCallback();
|
||||
}
|
||||
void disconnected()
|
||||
{
|
||||
if (_bleMidiTransport->_disconnectedCallback)
|
||||
_bleMidiTransport->_disconnectedCallback();
|
||||
|
||||
end();
|
||||
}
|
||||
};
|
||||
|
||||
class MyServerCallbacks: public BLEServerCallbacks {
|
||||
class MyServerCallbacks : public BLEServerCallbacks
|
||||
{
|
||||
public:
|
||||
MyServerCallbacks(BLEMIDI_ESP32* bluetoothEsp32)
|
||||
: _bluetoothEsp32(bluetoothEsp32) {
|
||||
MyServerCallbacks(BLEMIDI_ESP32 *bluetoothEsp32)
|
||||
: _bluetoothEsp32(bluetoothEsp32)
|
||||
{
|
||||
}
|
||||
|
||||
protected:
|
||||
BLEMIDI_ESP32* _bluetoothEsp32 = nullptr;
|
||||
BLEMIDI_ESP32 *_bluetoothEsp32 = nullptr;
|
||||
|
||||
void onConnect(BLEServer*) {
|
||||
void onConnect(BLEServer *)
|
||||
{
|
||||
if (_bluetoothEsp32)
|
||||
_bluetoothEsp32->connected();
|
||||
};
|
||||
|
||||
void onDisconnect(BLEServer*) {
|
||||
void onDisconnect(BLEServer *server)
|
||||
{
|
||||
if (_bluetoothEsp32)
|
||||
_bluetoothEsp32->disconnected();
|
||||
}
|
||||
|
||||
server->getAdvertising()->start();
|
||||
}
|
||||
};
|
||||
|
||||
class MyCharacteristicCallbacks: public BLECharacteristicCallbacks {
|
||||
class MyCharacteristicCallbacks : public BLECharacteristicCallbacks
|
||||
{
|
||||
public:
|
||||
MyCharacteristicCallbacks(BLEMIDI_ESP32* bluetoothEsp32)
|
||||
: _bluetoothEsp32(bluetoothEsp32 ) {
|
||||
MyCharacteristicCallbacks(BLEMIDI_ESP32 *bluetoothEsp32)
|
||||
: _bluetoothEsp32(bluetoothEsp32)
|
||||
{
|
||||
}
|
||||
|
||||
protected:
|
||||
BLEMIDI_ESP32* _bluetoothEsp32 = nullptr;
|
||||
BLEMIDI_ESP32 *_bluetoothEsp32 = nullptr;
|
||||
|
||||
void onWrite(BLECharacteristic * characteristic) {
|
||||
void onWrite(BLECharacteristic *characteristic)
|
||||
{
|
||||
std::string rxValue = characteristic->getValue();
|
||||
if (rxValue.length() > 0) {
|
||||
_bluetoothEsp32->receive((uint8_t *)(rxValue.c_str()), rxValue.length());
|
||||
if (rxValue.length() > 0)
|
||||
{
|
||||
_bluetoothEsp32->receive((uint8_t *)(rxValue.c_str()), rxValue.length());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
bool BLEMIDI_ESP32::begin(const char* deviceName, BLEMIDI_Transport<class BLEMIDI_ESP32>* bleMidiTransport)
|
||||
bool BLEMIDI_ESP32::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_ESP32> *bleMidiTransport)
|
||||
{
|
||||
_bleMidiTransport = bleMidiTransport;
|
||||
_bleMidiTransport = bleMidiTransport;
|
||||
|
||||
BLEDevice::init(deviceName);
|
||||
|
||||
|
|
@ -127,12 +139,11 @@ bool BLEMIDI_ESP32::begin(const char* deviceName, BLEMIDI_Transport<class BLEMID
|
|||
|
||||
// Create a BLE Characteristic
|
||||
_characteristic = service->createCharacteristic(
|
||||
BLEUUID(CHARACTERISTIC_UUID),
|
||||
BLECharacteristic::PROPERTY_READ |
|
||||
BLECharacteristic::PROPERTY_WRITE |
|
||||
BLECharacteristic::PROPERTY_NOTIFY |
|
||||
BLECharacteristic::PROPERTY_WRITE_NR
|
||||
);
|
||||
BLEUUID(CHARACTERISTIC_UUID),
|
||||
BLECharacteristic::PROPERTY_READ |
|
||||
BLECharacteristic::PROPERTY_WRITE |
|
||||
BLECharacteristic::PROPERTY_NOTIFY |
|
||||
BLECharacteristic::PROPERTY_WRITE_NR);
|
||||
// Add CCCD 0x2902 to allow notify
|
||||
_characteristic->addDescriptor(new BLE2902());
|
||||
|
||||
|
|
@ -150,18 +161,20 @@ bool BLEMIDI_ESP32::begin(const char* deviceName, BLEMIDI_Transport<class BLEMID
|
|||
_advertising->setAppearance(0x00);
|
||||
_advertising->start();
|
||||
|
||||
Serial.println("begin");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*! \brief Create an instance for ESP32 named <DeviceName>
|
||||
/*! \brief Create an instance for ESP32 named <DeviceName>
|
||||
*/
|
||||
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
|
||||
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32> BLE##Name(DeviceName); \
|
||||
MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32> &)BLE##Name);
|
||||
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
|
||||
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32> BLE##Name(DeviceName); \
|
||||
MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32> &)BLE##Name);
|
||||
|
||||
/*! \brief Create a default instance for ESP32 named BLE-MIDI
|
||||
/*! \brief Create a default instance for ESP32 named BLE-MIDI
|
||||
*/
|
||||
#define BLEMIDI_CREATE_DEFAULT_INSTANCE() \
|
||||
BLEMIDI_CREATE_INSTANCE("Esp32-BLE-MIDI", MIDI)
|
||||
BLEMIDI_CREATE_INSTANCE("Esp32-BLE-MIDI", MIDI)
|
||||
|
||||
END_BLEMIDI_NAMESPACE
|
||||
|
|
|
|||
|
|
@ -8,11 +8,11 @@ BEGIN_BLEMIDI_NAMESPACE
|
|||
class BLEMIDI_ESP32_NimBLE
|
||||
{
|
||||
private:
|
||||
BLEServer* _server = nullptr;
|
||||
BLEAdvertising* _advertising = nullptr;
|
||||
BLECharacteristic* _characteristic = nullptr;
|
||||
BLEServer *_server = nullptr;
|
||||
BLEAdvertising *_advertising = nullptr;
|
||||
BLECharacteristic *_characteristic = nullptr;
|
||||
|
||||
BLEMIDI_Transport<class BLEMIDI_ESP32_NimBLE>* _bleMidiTransport = nullptr;
|
||||
BLEMIDI_Transport<class BLEMIDI_ESP32_NimBLE> *_bleMidiTransport = nullptr;
|
||||
|
||||
friend class MyServerCallbacks;
|
||||
friend class MyCharacteristicCallbacks;
|
||||
|
|
@ -21,27 +21,26 @@ protected:
|
|||
QueueHandle_t mRxQueue;
|
||||
|
||||
public:
|
||||
BLEMIDI_ESP32_NimBLE()
|
||||
BLEMIDI_ESP32_NimBLE()
|
||||
{
|
||||
}
|
||||
|
||||
bool begin(const char*, BLEMIDI_Transport<class BLEMIDI_ESP32_NimBLE>*);
|
||||
bool begin(const char *, BLEMIDI_Transport<class BLEMIDI_ESP32_NimBLE> *);
|
||||
|
||||
void end()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void write(uint8_t* buffer, size_t length)
|
||||
void write(uint8_t *buffer, size_t length)
|
||||
{
|
||||
_characteristic->setValue(buffer, length);
|
||||
_characteristic->notify();
|
||||
}
|
||||
|
||||
bool available(byte* pvBuffer)
|
||||
bool available(byte *pvBuffer)
|
||||
{
|
||||
// return 1 byte from the Queue
|
||||
return xQueueReceive(mRxQueue, (void*)pvBuffer, 0); // return immediately when the queue is empty
|
||||
return xQueueReceive(mRxQueue, (void *)pvBuffer, 0); // return immediately when the queue is empty
|
||||
}
|
||||
|
||||
void add(byte value)
|
||||
|
|
@ -51,65 +50,73 @@ public:
|
|||
}
|
||||
|
||||
protected:
|
||||
void receive(uint8_t* buffer, size_t length)
|
||||
{
|
||||
void receive(uint8_t *buffer, size_t length)
|
||||
{
|
||||
// forward the buffer so it can be parsed
|
||||
_bleMidiTransport->receive(buffer, length);
|
||||
}
|
||||
}
|
||||
|
||||
void connected()
|
||||
{
|
||||
if (_bleMidiTransport->_connectedCallback)
|
||||
_bleMidiTransport->_connectedCallback();
|
||||
}
|
||||
void connected()
|
||||
{
|
||||
if (_bleMidiTransport->_connectedCallback)
|
||||
_bleMidiTransport->_connectedCallback();
|
||||
}
|
||||
|
||||
void disconnected()
|
||||
{
|
||||
if (_bleMidiTransport->_disconnectedCallback)
|
||||
_bleMidiTransport->_disconnectedCallback();
|
||||
}
|
||||
void disconnected()
|
||||
{
|
||||
if (_bleMidiTransport->_disconnectedCallback)
|
||||
_bleMidiTransport->_disconnectedCallback();
|
||||
}
|
||||
};
|
||||
|
||||
class MyServerCallbacks: public BLEServerCallbacks {
|
||||
class MyServerCallbacks : public BLEServerCallbacks
|
||||
{
|
||||
public:
|
||||
MyServerCallbacks(BLEMIDI_ESP32_NimBLE* bluetoothEsp32)
|
||||
: _bluetoothEsp32(bluetoothEsp32) {
|
||||
MyServerCallbacks(BLEMIDI_ESP32_NimBLE *bluetoothEsp32)
|
||||
: _bluetoothEsp32(bluetoothEsp32)
|
||||
{
|
||||
}
|
||||
|
||||
protected:
|
||||
BLEMIDI_ESP32_NimBLE* _bluetoothEsp32 = nullptr;
|
||||
BLEMIDI_ESP32_NimBLE *_bluetoothEsp32 = nullptr;
|
||||
|
||||
void onConnect(BLEServer*) {
|
||||
void onConnect(BLEServer *)
|
||||
{
|
||||
if (_bluetoothEsp32)
|
||||
_bluetoothEsp32->connected();
|
||||
};
|
||||
|
||||
void onDisconnect(BLEServer*) {
|
||||
void onDisconnect(BLEServer *)
|
||||
{
|
||||
if (_bluetoothEsp32)
|
||||
_bluetoothEsp32->disconnected();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class MyCharacteristicCallbacks: public BLECharacteristicCallbacks {
|
||||
class MyCharacteristicCallbacks : public BLECharacteristicCallbacks
|
||||
{
|
||||
public:
|
||||
MyCharacteristicCallbacks(BLEMIDI_ESP32_NimBLE* bluetoothEsp32)
|
||||
: _bluetoothEsp32(bluetoothEsp32 ) {
|
||||
MyCharacteristicCallbacks(BLEMIDI_ESP32_NimBLE *bluetoothEsp32)
|
||||
: _bluetoothEsp32(bluetoothEsp32)
|
||||
{
|
||||
}
|
||||
|
||||
protected:
|
||||
BLEMIDI_ESP32_NimBLE* _bluetoothEsp32 = nullptr;
|
||||
BLEMIDI_ESP32_NimBLE *_bluetoothEsp32 = nullptr;
|
||||
|
||||
void onWrite(BLECharacteristic * characteristic) {
|
||||
void onWrite(BLECharacteristic *characteristic)
|
||||
{
|
||||
std::string rxValue = characteristic->getValue();
|
||||
if (rxValue.length() > 0) {
|
||||
_bluetoothEsp32->receive((uint8_t *)(rxValue.c_str()), rxValue.length());
|
||||
if (rxValue.length() > 0)
|
||||
{
|
||||
_bluetoothEsp32->receive((uint8_t *)(rxValue.c_str()), rxValue.length());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
bool BLEMIDI_ESP32_NimBLE::begin(const char* deviceName, BLEMIDI_Transport<class BLEMIDI_ESP32_NimBLE>* bleMidiTransport)
|
||||
bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_ESP32_NimBLE> *bleMidiTransport)
|
||||
{
|
||||
_bleMidiTransport = bleMidiTransport;
|
||||
_bleMidiTransport = bleMidiTransport;
|
||||
|
||||
BLEDevice::init(deviceName);
|
||||
|
||||
|
|
@ -119,18 +126,18 @@ bool BLEMIDI_ESP32_NimBLE::begin(const char* deviceName, BLEMIDI_Transport<class
|
|||
|
||||
_server = BLEDevice::createServer();
|
||||
_server->setCallbacks(new MyServerCallbacks(this));
|
||||
_server->advertiseOnDisconnect(true);
|
||||
|
||||
// Create the BLE Service
|
||||
auto service = _server->createService(BLEUUID(SERVICE_UUID));
|
||||
|
||||
// Create a BLE Characteristic
|
||||
_characteristic = service->createCharacteristic(
|
||||
BLEUUID(CHARACTERISTIC_UUID),
|
||||
NIMBLE_PROPERTY::READ |
|
||||
NIMBLE_PROPERTY::WRITE |
|
||||
NIMBLE_PROPERTY::NOTIFY |
|
||||
NIMBLE_PROPERTY::WRITE_NR
|
||||
);
|
||||
BLEUUID(CHARACTERISTIC_UUID),
|
||||
NIMBLE_PROPERTY::READ |
|
||||
NIMBLE_PROPERTY::WRITE |
|
||||
NIMBLE_PROPERTY::NOTIFY |
|
||||
NIMBLE_PROPERTY::WRITE_NR);
|
||||
|
||||
_characteristic->setCallbacks(new MyCharacteristicCallbacks(this));
|
||||
|
||||
|
|
@ -149,15 +156,15 @@ bool BLEMIDI_ESP32_NimBLE::begin(const char* deviceName, BLEMIDI_Transport<class
|
|||
return true;
|
||||
}
|
||||
|
||||
/*! \brief Create an instance for ESP32 named <DeviceName>
|
||||
/*! \brief Create an instance for ESP32 named <DeviceName>
|
||||
*/
|
||||
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
|
||||
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE> BLE##Name(DeviceName); \
|
||||
MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE> &)BLE##Name);
|
||||
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
|
||||
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE> BLE##Name(DeviceName); \
|
||||
MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE> &)BLE##Name);
|
||||
|
||||
/*! \brief Create a default instance for ESP32 named BLE-MIDI
|
||||
/*! \brief Create a default instance for ESP32 named BLE-MIDI
|
||||
*/
|
||||
#define BLEMIDI_CREATE_DEFAULT_INSTANCE() \
|
||||
BLEMIDI_CREATE_INSTANCE("Esp32-NimBLE-MIDI", MIDI)
|
||||
BLEMIDI_CREATE_INSTANCE("Esp32-NimBLE-MIDI", MIDI)
|
||||
|
||||
END_BLEMIDI_NAMESPACE
|
||||
|
|
|
|||
Loading…
Reference in New Issue