@@ -67,6 +67,8 @@ class BLEMIDI_ArduinoBLE
6767
6868 Fifo<byte, _Settings::MaxBufferSize> mRxBuffer ;
6969
70+ template <class > friend class MyServerCallbacks ;
71+
7072public:
7173 BLEMIDI_ArduinoBLE () : _midiService(SERVICE_UUID),
7274 _midiChar (CHARACTERISTIC_UUID, BLERead | BLEWrite | BLENotify | BLEWriteWithoutResponse, _Settings::MaxBufferSize)
@@ -93,8 +95,6 @@ class BLEMIDI_ArduinoBLE
9395 return true ;
9496 }
9597
96- poll ();
97-
9898 if (_midiChar.written ())
9999 {
100100 auto length = _midiChar.valueLength ();
@@ -120,54 +120,69 @@ class BLEMIDI_ArduinoBLE
120120 _bleMidiTransport->receive ((uint8_t *)buffer, length);
121121 }
122122
123- bool poll ()
123+ void connected ()
124124 {
125- BLEDevice central = BLE.central ();
126- if (!central)
127- {
128- if (_central)
129- {
130- onDisconnected (*_central);
131- _central = nullptr ;
132- }
133- return false ;
134- }
125+ if (_bleMidiTransport->_connectedCallback )
126+ _bleMidiTransport->_connectedCallback ();
127+ }
135128
136- if (!central.connected ())
137- return false ;
129+ void disconnected ()
130+ {
131+ if (_bleMidiTransport->_disconnectedCallback )
132+ _bleMidiTransport->_disconnectedCallback ();
138133
139- if (nullptr == _central)
140- {
141- onConnected (central);
142- _central = ¢ral;
143- }
144- else
145- {
146- if (*_central != central)
147- {
148- onDisconnected (*_central);
149- onConnected (central);
150- _central = ¢ral;
151- }
152- }
134+ end ();
135+ }
136+ };
153137
154- return true ;
138+ template <class _Settings >
139+ class MyServerCallbacks : public BLEDeviceCallbacks
140+ {
141+ public:
142+ MyServerCallbacks (BLEMIDI_ArduinoBLE<_Settings> *bluetooth)
143+ : _bluetooth(bluetooth)
144+ {
155145 }
156146
157- void onConnected (BLEDevice central)
147+ protected:
148+ BLEMIDI_ArduinoBLE<_Settings> *_bluetooth = nullptr ;
149+
150+ void onConnect (BLEDevice device)
151+ {
152+ if (_bluetooth)
153+ _bluetooth->connected ();
154+ };
155+
156+ void onDisconnect (BLEDevice device)
158157 {
159- _central = ¢ral;
158+ if (_bluetooth)
159+ _bluetooth->disconnected ();
160+ }
161+ };
160162
161- if (_bleMidiTransport->_connectedCallback )
162- _bleMidiTransport->_connectedCallback ();
163+ template <class _Settings >
164+ class MyCharacteristicCallbacks : public BLECharacteristicCallbacks
165+ {
166+ public:
167+ MyCharacteristicCallbacks (BLEMIDI_ArduinoBLE<_Settings> *bluetooth)
168+ : _bluetooth(bluetooth)
169+ {
163170 }
164171
165- void onDisconnected (BLEDevice central)
172+ protected:
173+ BLEMIDI_ArduinoBLE<_Settings> *_bluetooth = nullptr ;
174+
175+ void onWrite (BLECharacteristic characteristic)
166176 {
167- if (_bleMidiTransport->_disconnectedCallback )
168- _bleMidiTransport->_disconnectedCallback ();
177+ // std::string rxValue = characteristic->getValue();
178+ // if (rxValue.length() > 0)
179+ // {
180+ // _bluetooth->receive((uint8_t *)(rxValue.c_str()), rxValue.length());
181+ // }
182+ }
169183
170- _central = nullptr ;
184+ void onRead (BLECharacteristic characteristic)
185+ {
171186 }
172187};
173188
@@ -186,9 +201,13 @@ bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Transp
186201 _midiService.addCharacteristic (_midiChar);
187202 BLE.addService (_midiService);
188203
204+ BLE.setCallbacks (new MyServerCallbacks<_Settings>(this ));
205+
206+ _midiChar.setCallbacks (new MyCharacteristicCallbacks<_Settings>(this ));
207+
189208 // set the initial value for the characeristic:
190209 // (when not set, the device will disconnect after 0.5 seconds)
191- _midiChar.writeValue ((uint8_t )0 );
210+ _midiChar.writeValue ((uint8_t )0 ); // TODO: might not be needed, check!!
192211
193212 /* Start advertising BLE. It will start continuously transmitting BLE
194213 advertising packets and will be visible to remote BLE central devices
0 commit comments