There are 2 main problems:
1. On Linux (using BlueZ 5.50), it works, and the MIDI events show up in my MIDI monitor. However, after some time (ranging from a couple of seconds to many minutes), it randomly disconnects, and doesn't automatically connect again. I have no idea why this is.
2. On Windows 10, I can connect to the ESP32, but even though it says "connected" in the Windows settings, but in the Serial Monitor, it says "Disconnected" 2 or three seconds after "Connected".
Between the "Connected" and "Disconnected", the "notify" fails, because "notifications disabled; ignoring".
On Android, I can see it using the Bluetooth settings, and I can pair, but I can't do anything with it. It doesn't show up when scanning using the MIDI BLE Connect app.
[hr]
I'm using this example and Neil Kolban's BLE library that comes with the ESP32 Arduino Core:
Code: Select all
/*
BLE_MIDI Example by neilbags
https://github.com/neilbags/arduino-esp32-BLE-MIDI
Based on BLE_notify example by Evandro Copercini.
Creates a BLE MIDI service and characteristic.
Once a client subscibes, send a MIDI message every 2 seconds
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLE2902.h>
#define SERVICE_UUID "03b80e5a-ede8-4b33-a751-6ce34ec4c700"
#define CHARACTERISTIC_UUID "7772e5db-3868-4112-a1a9-f2669d106bf3"
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
uint8_t midiPacket[] = {
0x80, // header
0x80, // timestamp, not implemented
0x00, // status
0x3c, // 0x3c == 60 == middle c
0x00 // velocity
};
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
Serial.println("Connected ++++++++++++++++++++++++++");
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
Serial.println("Disconnected -----------------------");
}
};
void setup() {
Serial.begin(115200);
BLEDevice::init("ESP32 MIDI Example");
// Create the BLE Server
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(BLEUUID(SERVICE_UUID));
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
BLEUUID(CHARACTERISTIC_UUID),
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_WRITE_NR
);
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
// Create a BLE Descriptor
pCharacteristic->addDescriptor(new BLE2902());
// Start the service
pService->start();
// Start advertising
pServer->getAdvertising()->start();
}
void loop() {
if (deviceConnected) {
// note down
midiPacket[2] = 0x90; // note down, channel 0
midiPacket[4] = 127; // velocity
pCharacteristic->setValue(midiPacket, 5); // packet, length in bytes
pCharacteristic->notify();
// play note for 500ms
delay(500);
// note up
midiPacket[2] = 0x80; // note up, channel 0
midiPacket[4] = 0; // velocity
pCharacteristic->setValue(midiPacket, 5); // packet, length in bytes)
pCharacteristic->notify();
delay(500);
}
}
I have no idea how to debug this, so any help or pointers would be greatly appreciated.
Thanks,
Pieter