Skip to content

Commit e0afefb

Browse files
committed
re-advertise after disconnect (ESP32 & ESP32_NimBLE)
1 parent 927b3ac commit e0afefb

File tree

3 files changed

+153
-132
lines changed

3 files changed

+153
-132
lines changed

src/hardware/BLEMIDI_ArduinoBLE.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ class BLEMIDI_ArduinoBLE
170170
{
171171
if (_bleMidiTransport->_disconnectedCallback)
172172
_bleMidiTransport->_disconnectedCallback();
173+
173174
_central = nullptr;
174175
}
175176

src/hardware/BLEMIDI_ESP32.h

Lines changed: 79 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,37 @@ BEGIN_BLEMIDI_NAMESPACE
1111
class BLEMIDI_ESP32
1212
{
1313
private:
14-
BLEServer* _server = nullptr;
15-
BLEAdvertising* _advertising = nullptr;
16-
BLECharacteristic* _characteristic = nullptr;
17-
18-
BLEMIDI_Transport<class BLEMIDI_ESP32>* _bleMidiTransport = nullptr;
14+
BLEServer *_server = nullptr;
15+
BLEAdvertising *_advertising = nullptr;
16+
BLECharacteristic *_characteristic = nullptr;
1917

20-
friend class MyServerCallbacks;
21-
friend class MyCharacteristicCallbacks;
18+
BLEMIDI_Transport<class BLEMIDI_ESP32> *_bleMidiTransport = nullptr;
19+
20+
friend class MyServerCallbacks;
21+
friend class MyCharacteristicCallbacks;
2222

2323
protected:
2424
QueueHandle_t mRxQueue;
2525

2626
public:
27-
BLEMIDI_ESP32()
27+
BLEMIDI_ESP32()
2828
{
2929
}
30-
31-
bool begin(const char*, BLEMIDI_Transport<class BLEMIDI_ESP32>*);
32-
33-
void end()
30+
31+
bool begin(const char *, BLEMIDI_Transport<class BLEMIDI_ESP32> *);
32+
33+
void end()
3434
{
35-
35+
Serial.println("end");
3636
}
3737

38-
void write(uint8_t* buffer, size_t length)
38+
void write(uint8_t *buffer, size_t length)
3939
{
4040
_characteristic->setValue(buffer, length);
4141
_characteristic->notify();
4242
}
43-
44-
bool available(byte* pvBuffer)
43+
44+
bool available(byte *pvBuffer)
4545
{
4646
return xQueueReceive(mRxQueue, pvBuffer, 0); // return immediately when the queue is empty
4747
}
@@ -53,86 +53,97 @@ class BLEMIDI_ESP32
5353
}
5454

5555
protected:
56-
void receive(uint8_t* buffer, size_t length)
57-
{
56+
void receive(uint8_t *buffer, size_t length)
57+
{
5858
// parse the incoming buffer
5959
_bleMidiTransport->receive(buffer, length);
60-
}
61-
62-
void connected()
63-
{
64-
if (_bleMidiTransport->_connectedCallback)
65-
_bleMidiTransport->_connectedCallback();
66-
}
67-
68-
void disconnected()
69-
{
70-
if (_bleMidiTransport->_disconnectedCallback)
71-
_bleMidiTransport->_disconnectedCallback();
72-
}
60+
}
61+
62+
void connected()
63+
{
64+
if (_bleMidiTransport->_connectedCallback)
65+
_bleMidiTransport->_connectedCallback();
66+
}
67+
68+
void disconnected()
69+
{
70+
if (_bleMidiTransport->_disconnectedCallback)
71+
_bleMidiTransport->_disconnectedCallback();
72+
73+
end();
74+
}
7375
};
7476

75-
class MyServerCallbacks: public BLEServerCallbacks {
77+
class MyServerCallbacks : public BLEServerCallbacks
78+
{
7679
public:
77-
MyServerCallbacks(BLEMIDI_ESP32* bluetoothEsp32)
78-
: _bluetoothEsp32(bluetoothEsp32) {
80+
MyServerCallbacks(BLEMIDI_ESP32 *bluetoothEsp32)
81+
: _bluetoothEsp32(bluetoothEsp32)
82+
{
7983
}
8084

8185
protected:
82-
BLEMIDI_ESP32* _bluetoothEsp32 = nullptr;
86+
BLEMIDI_ESP32 *_bluetoothEsp32 = nullptr;
8387

84-
void onConnect(BLEServer*) {
88+
void onConnect(BLEServer *)
89+
{
8590
if (_bluetoothEsp32)
8691
_bluetoothEsp32->connected();
8792
};
88-
89-
void onDisconnect(BLEServer*) {
93+
94+
void onDisconnect(BLEServer *server)
95+
{
9096
if (_bluetoothEsp32)
9197
_bluetoothEsp32->disconnected();
92-
}
98+
99+
server->getAdvertising()->start();
100+
}
93101
};
94102

95-
class MyCharacteristicCallbacks: public BLECharacteristicCallbacks {
103+
class MyCharacteristicCallbacks : public BLECharacteristicCallbacks
104+
{
96105
public:
97-
MyCharacteristicCallbacks(BLEMIDI_ESP32* bluetoothEsp32)
98-
: _bluetoothEsp32(bluetoothEsp32 ) {
106+
MyCharacteristicCallbacks(BLEMIDI_ESP32 *bluetoothEsp32)
107+
: _bluetoothEsp32(bluetoothEsp32)
108+
{
99109
}
100-
110+
101111
protected:
102-
BLEMIDI_ESP32* _bluetoothEsp32 = nullptr;
112+
BLEMIDI_ESP32 *_bluetoothEsp32 = nullptr;
103113

104-
void onWrite(BLECharacteristic * characteristic) {
114+
void onWrite(BLECharacteristic *characteristic)
115+
{
105116
std::string rxValue = characteristic->getValue();
106-
if (rxValue.length() > 0) {
107-
_bluetoothEsp32->receive((uint8_t *)(rxValue.c_str()), rxValue.length());
117+
if (rxValue.length() > 0)
118+
{
119+
_bluetoothEsp32->receive((uint8_t *)(rxValue.c_str()), rxValue.length());
108120
}
109121
}
110122
};
111123

112-
bool BLEMIDI_ESP32::begin(const char* deviceName, BLEMIDI_Transport<class BLEMIDI_ESP32>* bleMidiTransport)
124+
bool BLEMIDI_ESP32::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_ESP32> *bleMidiTransport)
113125
{
114-
_bleMidiTransport = bleMidiTransport;
126+
_bleMidiTransport = bleMidiTransport;
115127

116128
BLEDevice::init(deviceName);
117-
129+
118130
// To communicate between the 2 cores.
119131
// Core_0 runs here, core_1 runs the BLE stack
120132
mRxQueue = xQueueCreate(64, sizeof(uint8_t)); // TODO Settings::MaxBufferSize
121133

122134
_server = BLEDevice::createServer();
123135
_server->setCallbacks(new MyServerCallbacks(this));
124-
136+
125137
// Create the BLE Service
126138
auto service = _server->createService(BLEUUID(SERVICE_UUID));
127-
139+
128140
// Create a BLE Characteristic
129141
_characteristic = service->createCharacteristic(
130-
BLEUUID(CHARACTERISTIC_UUID),
131-
BLECharacteristic::PROPERTY_READ |
132-
BLECharacteristic::PROPERTY_WRITE |
133-
BLECharacteristic::PROPERTY_NOTIFY |
134-
BLECharacteristic::PROPERTY_WRITE_NR
135-
);
142+
BLEUUID(CHARACTERISTIC_UUID),
143+
BLECharacteristic::PROPERTY_READ |
144+
BLECharacteristic::PROPERTY_WRITE |
145+
BLECharacteristic::PROPERTY_NOTIFY |
146+
BLECharacteristic::PROPERTY_WRITE_NR);
136147
// Add CCCD 0x2902 to allow notify
137148
_characteristic->addDescriptor(new BLE2902());
138149

@@ -143,25 +154,27 @@ bool BLEMIDI_ESP32::begin(const char* deviceName, BLEMIDI_Transport<class BLEMID
143154

144155
// Start the service
145156
service->start();
146-
157+
147158
// Start advertising
148159
_advertising = _server->getAdvertising();
149160
_advertising->addServiceUUID(service->getUUID());
150161
_advertising->setAppearance(0x00);
151162
_advertising->start();
152-
163+
164+
Serial.println("begin");
165+
153166
return true;
154167
}
155168

156-
/*! \brief Create an instance for ESP32 named <DeviceName>
169+
/*! \brief Create an instance for ESP32 named <DeviceName>
157170
*/
158-
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
159-
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32> BLE##Name(DeviceName); \
160-
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);
171+
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
172+
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32> BLE##Name(DeviceName); \
173+
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);
161174

162-
/*! \brief Create a default instance for ESP32 named BLE-MIDI
175+
/*! \brief Create a default instance for ESP32 named BLE-MIDI
163176
*/
164177
#define BLEMIDI_CREATE_DEFAULT_INSTANCE() \
165-
BLEMIDI_CREATE_INSTANCE("Esp32-BLE-MIDI", MIDI)
178+
BLEMIDI_CREATE_INSTANCE("Esp32-BLE-MIDI", MIDI)
166179

167180
END_BLEMIDI_NAMESPACE

0 commit comments

Comments
 (0)