@@ -73,12 +73,13 @@ class BLEMIDI_ArduinoBLE
7373
7474 Fifo<byte, _Settings::MaxBufferSize> mRxBuffer ;
7575
76- template < class > friend class MyServerCallbacks ;
76+ static BLEMIDI_ArduinoBLE<_Settings>* self ;
7777
7878public:
7979 BLEMIDI_ArduinoBLE () : _midiService(SERVICE_UUID),
8080 _midiChar (CHARACTERISTIC_UUID, BLERead | BLEWrite | BLENotify | BLEWriteWithoutResponse, _Settings::MaxBufferSize)
8181 {
82+ self = this ;
8283 }
8384
8485 bool begin (const char *, BLEMIDI_Transport<class BLEMIDI_ArduinoBLE <_Settings>, _Settings> *);
@@ -139,59 +140,26 @@ class BLEMIDI_ArduinoBLE
139140
140141 end ();
141142 }
142- };
143143
144- template <class _Settings >
145- class MyServerCallbacks : public BLEDeviceCallbacks
146- {
147- public:
148- MyServerCallbacks (BLEMIDI_ArduinoBLE<_Settings> *bluetooth)
149- : _bluetooth(bluetooth)
150- {
144+ static void blePeripheralConnectedHandler (BLEDevice central) {
145+ self->connected ();
151146 }
152-
153- protected:
154- BLEMIDI_ArduinoBLE<_Settings> *_bluetooth = nullptr ;
155-
156- void onConnect (BLEDevice device)
157- {
158- if (_bluetooth)
159- _bluetooth->connected ();
160- };
161-
162- void onDisconnect (BLEDevice device)
163- {
164- if (_bluetooth)
165- _bluetooth->disconnected ();
147+
148+ static void blePeripheralDisconnectedHandler (BLEDevice central) {
149+ self->disconnected ();
166150 }
167- };
168-
169- template <class _Settings >
170- class MyCharacteristicCallbacks : public BLECharacteristicCallbacks
171- {
172- public:
173- MyCharacteristicCallbacks (BLEMIDI_ArduinoBLE<_Settings> *bluetooth)
174- : _bluetooth(bluetooth)
175- {
176- }
177-
178- protected:
179- BLEMIDI_ArduinoBLE<_Settings> *_bluetooth = nullptr ;
180-
181- void onWrite (BLECharacteristic characteristic)
182- {
183- // std::string rxValue = characteristic->getValue();
184- // if (rxValue.length() > 0)
185- // {
186- // _bluetooth->receive((uint8_t *)(rxValue.c_str()), rxValue.length());
187- // }
188- }
189-
190- void onRead (BLECharacteristic characteristic)
191- {
151+
152+ static void switchCharacteristicWritten (BLEDevice central, BLECharacteristic characteristic) {
153+ // std::string rxValue = characteristic->value();
154+ // if (rxValue.length() > 0)
155+ // receive((uint8_t *)(rxValue.c_str()), rxValue.length());
192156 }
193157};
194158
159+ // this pattern of static to instance variable is limited to 1 instance of ArduinoBLE
160+ // for multiple instances, turn this into a vector and lookup
161+ template <class _Settings > BLEMIDI_ArduinoBLE<_Settings>* BLEMIDI_ArduinoBLE<_Settings>::self;
162+
195163template <class _Settings >
196164bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_ArduinoBLE <_Settings>, _Settings> *bleMidiTransport)
197165{
@@ -207,9 +175,10 @@ bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Transp
207175 _midiService.addCharacteristic (_midiChar);
208176 BLE.addService (_midiService);
209177
210- BLE.setCallbacks (new MyServerCallbacks<_Settings>(this ));
178+ BLE.setEventHandler (BLEConnected, blePeripheralConnectedHandler);
179+ BLE.setEventHandler (BLEDisconnected, blePeripheralDisconnectedHandler);
211180
212- _midiChar.setCallbacks ( new MyCharacteristicCallbacks<_Settings>( this ) );
181+ _midiChar.setEventHandler (BLEWritten, switchCharacteristicWritten );
213182
214183 // set the initial value for the characeristic:
215184 // (when not set, the device will disconnect after 0.5 seconds)
@@ -223,6 +192,7 @@ bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Transp
223192 return true ;
224193}
225194
195+
226196/* ! \brief Create an instance for ArduinoBLE <DeviceName>
227197 */
228198#define BLEMIDI_CREATE_CUSTOM_INSTANCE (DeviceName, Name, _Settings ) \
0 commit comments