Update BLEMIDI_ArduinoBLE.h
added : _midiChar.writeValue((uint8_t)0); otherwise the device will disconnect immediately
This commit is contained in:
parent
8c605fea37
commit
edd2f21a39
|
|
@ -7,13 +7,9 @@
|
|||
|
||||
BEGIN_BLEMIDI_NAMESPACE
|
||||
|
||||
BLEService midiService(SERVICE_UUID);
|
||||
|
||||
BLEStringCharacteristic midiChar(CHARACTERISTIC_UUID, // standard 16-bit characteristic UUID
|
||||
BLERead | BLEWrite | BLENotify | BLEWriteWithoutResponse, 16); // remote clients will be able to get notifications if this characteristic changes
|
||||
|
||||
template <typename T, short rawSize>
|
||||
class Fifo {
|
||||
class Fifo
|
||||
{
|
||||
public:
|
||||
const size_t size; // speculative feature, in case it's needed
|
||||
|
||||
|
|
@ -69,10 +65,14 @@ private:
|
|||
BLEMIDI_Transport<class BLEMIDI_ArduinoBLE<_Settings>, _Settings> *_bleMidiTransport;
|
||||
BLEDevice *_central;
|
||||
|
||||
BLEService _midiService;
|
||||
BLECharacteristic _midiChar;
|
||||
|
||||
Fifo<byte, _Settings::MaxBufferSize> mRxBuffer;
|
||||
|
||||
public:
|
||||
BLEMIDI_ArduinoBLE()
|
||||
BLEMIDI_ArduinoBLE() : _midiService(SERVICE_UUID),
|
||||
_midiChar(CHARACTERISTIC_UUID, BLERead | BLEWrite | BLENotify | BLEWriteWithoutResponse, _Settings::MaxBufferSize)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -84,30 +84,28 @@ public:
|
|||
|
||||
void write(uint8_t *buffer, size_t length)
|
||||
{
|
||||
// TODO: test length
|
||||
((BLECharacteristic)midiChar).writeValue(buffer, length);
|
||||
if (length > 0)
|
||||
((BLECharacteristic)_midiChar).writeValue(buffer, length);
|
||||
}
|
||||
|
||||
bool available(byte *pvBuffer)
|
||||
{
|
||||
#ifdef BLE_POLLING
|
||||
|
||||
if (mRxBuffer.count() > 0) {
|
||||
if (mRxBuffer.count() > 0)
|
||||
{
|
||||
*pvBuffer = mRxBuffer.dequeue();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
poll();
|
||||
|
||||
if (midiChar.written()) {
|
||||
// auto buffer = midiChar.value();
|
||||
auto length = midiChar.valueLength();
|
||||
|
||||
if (length > 0) {
|
||||
auto buffer = midiChar.value().c_str();
|
||||
if (_midiChar.written())
|
||||
{
|
||||
auto length = _midiChar.valueLength();
|
||||
if (length > 0)
|
||||
{
|
||||
auto buffer = _midiChar.value();
|
||||
_bleMidiTransport->receive((byte *)buffer, length);
|
||||
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
|
@ -127,31 +125,35 @@ public:
|
|||
protected:
|
||||
void receive(const unsigned char *buffer, size_t length)
|
||||
{
|
||||
// forward the buffer so it can be parsed
|
||||
if (length > 0)
|
||||
_bleMidiTransport->receive((uint8_t *)buffer, length);
|
||||
}
|
||||
|
||||
bool poll()
|
||||
{
|
||||
BLEDevice central = BLE.central();
|
||||
if (!central) {
|
||||
if (_central) {
|
||||
if (!central)
|
||||
{
|
||||
if (_central)
|
||||
{
|
||||
BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler(*_central);
|
||||
_central = nullptr;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!central.connected()) {
|
||||
if (!central.connected())
|
||||
return false;
|
||||
}
|
||||
|
||||
if (nullptr == _central) {
|
||||
if (nullptr == _central)
|
||||
{
|
||||
BLEMIDI_ArduinoBLE::blePeripheralConnectHandler(central);
|
||||
_central = ¢ral;
|
||||
}
|
||||
else {
|
||||
if (*_central != central) {
|
||||
else
|
||||
{
|
||||
if (*_central != central)
|
||||
{
|
||||
BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler(*_central);
|
||||
BLEMIDI_ArduinoBLE::blePeripheralConnectHandler(central);
|
||||
_central = ¢ral;
|
||||
|
|
@ -177,7 +179,8 @@ protected:
|
|||
_central = nullptr;
|
||||
}
|
||||
|
||||
void characteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
|
||||
void characteristicWritten(BLEDevice central, BLECharacteristic characteristic)
|
||||
{
|
||||
auto buffer = characteristic.value();
|
||||
auto length = characteristic.valueLength();
|
||||
|
||||
|
|
@ -197,23 +200,25 @@ bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char* deviceName, BLEMIDI_Transp
|
|||
|
||||
BLE.setLocalName(deviceName);
|
||||
|
||||
BLE.setAdvertisedService(midiService);
|
||||
midiService.addCharacteristic(midiChar);
|
||||
BLE.addService(midiService);
|
||||
BLE.setAdvertisedService(_midiService);
|
||||
_midiService.addCharacteristic(_midiChar);
|
||||
BLE.addService(_midiService);
|
||||
|
||||
// set the initial value for the characeristic:
|
||||
// (when not set, the device will disconnect after 0.5 seconds)
|
||||
_midiChar.writeValue((uint8_t)0);
|
||||
|
||||
#ifdef BLE_EVENTS
|
||||
// assign event handlers for connected, disconnected to peripheral
|
||||
BLE.setEventHandler(BLEConnected, BLEMIDI_ArduinoBLE::blePeripheralConnectHandler);
|
||||
BLE.setEventHandler(BLEDisconnected, BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler);
|
||||
|
||||
midiChar.setEventHandler(BLEWritten, characteristicWritten);
|
||||
_midiChar.setEventHandler(BLEWritten, characteristicWritten);
|
||||
#endif
|
||||
|
||||
/* Start advertising BLE. It will start continuously transmitting BLE
|
||||
advertising packets and will be visible to remote BLE central devices
|
||||
until it receives a new connection */
|
||||
|
||||
// start advertising
|
||||
BLE.advertise();
|
||||
|
||||
return true;
|
||||
|
|
|
|||
Loading…
Reference in New Issue