22
33#include < ArduinoBLE.h>
44
5- #define BLE_POLLING
6- // #define BLE_EVENTS // TODO: requires static function (don't like)
7-
85BEGIN_BLEMIDI_NAMESPACE
96
107template <typename T, short rawSize>
@@ -90,7 +87,6 @@ class BLEMIDI_ArduinoBLE
9087
9188 bool available (byte *pvBuffer)
9289 {
93- #ifdef BLE_POLLING
9490 if (mRxBuffer .count () > 0 )
9591 {
9692 *pvBuffer = mRxBuffer .dequeue ();
@@ -109,11 +105,6 @@ class BLEMIDI_ArduinoBLE
109105 }
110106 }
111107 return false ;
112- #endif
113- #ifdef BLE_EVENTS
114- BLE.poll ();
115- return false ;
116- #endif
117108 }
118109
119110 void add (byte value)
@@ -136,7 +127,7 @@ class BLEMIDI_ArduinoBLE
136127 {
137128 if (_central)
138129 {
139- BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler (*_central);
130+ onDisconnected (*_central);
140131 _central = nullptr ;
141132 }
142133 return false ;
@@ -147,46 +138,37 @@ class BLEMIDI_ArduinoBLE
147138
148139 if (nullptr == _central)
149140 {
150- BLEMIDI_ArduinoBLE::blePeripheralConnectHandler (central);
141+ onConnected (central);
151142 _central = ¢ral;
152143 }
153144 else
154145 {
155146 if (*_central != central)
156147 {
157- BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler (*_central);
158- BLEMIDI_ArduinoBLE::blePeripheralConnectHandler (central);
148+ onDisconnected (*_central);
149+ onConnected (central);
159150 _central = ¢ral;
160151 }
161152 }
162153
163154 return true ;
164155 }
165156
166- void blePeripheralConnectHandler (BLEDevice central)
157+ void onConnected (BLEDevice central)
167158 {
168159 _central = ¢ral;
169160
170161 if (_bleMidiTransport->_connectedCallback )
171162 _bleMidiTransport->_connectedCallback ();
172163 }
173164
174- void blePeripheralDisconnectHandler (BLEDevice central)
165+ void onDisconnected (BLEDevice central)
175166 {
176167 if (_bleMidiTransport->_disconnectedCallback )
177168 _bleMidiTransport->_disconnectedCallback ();
178169
179170 _central = nullptr ;
180171 }
181-
182- void characteristicWritten (BLEDevice central, BLECharacteristic characteristic)
183- {
184- auto buffer = characteristic.value ();
185- auto length = characteristic.valueLength ();
186-
187- if (length > 0 )
188- receive (buffer, length);
189- }
190172};
191173
192174template <class _Settings >
@@ -208,14 +190,6 @@ bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Transp
208190 // (when not set, the device will disconnect after 0.5 seconds)
209191 _midiChar.writeValue ((uint8_t )0 );
210192
211- #ifdef BLE_EVENTS
212- // assign event handlers for connected, disconnected to peripheral
213- BLE.setEventHandler (BLEConnected, BLEMIDI_ArduinoBLE::blePeripheralConnectHandler);
214- BLE.setEventHandler (BLEDisconnected, BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler);
215-
216- _midiChar.setEventHandler (BLEWritten, characteristicWritten);
217- #endif
218-
219193 /* Start advertising BLE. It will start continuously transmitting BLE
220194 advertising packets and will be visible to remote BLE central devices
221195 until it receives a new connection */
0 commit comments