@@ -67,6 +67,8 @@ class BLEMIDI_ArduinoBLE
67
67
68
68
Fifo<byte, _Settings::MaxBufferSize> mRxBuffer ;
69
69
70
+ template <class > friend class MyServerCallbacks ;
71
+
70
72
public:
71
73
BLEMIDI_ArduinoBLE () : _midiService(SERVICE_UUID),
72
74
_midiChar (CHARACTERISTIC_UUID, BLERead | BLEWrite | BLENotify | BLEWriteWithoutResponse, _Settings::MaxBufferSize)
@@ -93,8 +95,6 @@ class BLEMIDI_ArduinoBLE
93
95
return true ;
94
96
}
95
97
96
- poll ();
97
-
98
98
if (_midiChar.written ())
99
99
{
100
100
auto length = _midiChar.valueLength ();
@@ -120,54 +120,69 @@ class BLEMIDI_ArduinoBLE
120
120
_bleMidiTransport->receive ((uint8_t *)buffer, length);
121
121
}
122
122
123
- bool poll ()
123
+ void connected ()
124
124
{
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
+ }
135
128
136
- if (!central.connected ())
137
- return false ;
129
+ void disconnected ()
130
+ {
131
+ if (_bleMidiTransport->_disconnectedCallback )
132
+ _bleMidiTransport->_disconnectedCallback ();
138
133
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
+ };
153
137
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
+ {
155
145
}
156
146
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)
158
157
{
159
- _central = ¢ral;
158
+ if (_bluetooth)
159
+ _bluetooth->disconnected ();
160
+ }
161
+ };
160
162
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
+ {
163
170
}
164
171
165
- void onDisconnected (BLEDevice central)
172
+ protected:
173
+ BLEMIDI_ArduinoBLE<_Settings> *_bluetooth = nullptr ;
174
+
175
+ void onWrite (BLECharacteristic characteristic)
166
176
{
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
+ }
169
183
170
- _central = nullptr ;
184
+ void onRead (BLECharacteristic characteristic)
185
+ {
171
186
}
172
187
};
173
188
@@ -186,9 +201,13 @@ bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Transp
186
201
_midiService.addCharacteristic (_midiChar);
187
202
BLE.addService (_midiService);
188
203
204
+ BLE.setCallbacks (new MyServerCallbacks<_Settings>(this ));
205
+
206
+ _midiChar.setCallbacks (new MyCharacteristicCallbacks<_Settings>(this ));
207
+
189
208
// set the initial value for the characeristic:
190
209
// (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!!
192
211
193
212
/* Start advertising BLE. It will start continuously transmitting BLE
194
213
advertising packets and will be visible to remote BLE central devices
0 commit comments