Bluetooth MIDI, but mostly BLUETOOTH.

Sorry if this should be in some other section.. but thsis is all I know.

SO.. I am experimenting with a BLE board, the NANO 33 IOT.
I want to connect it to iOS and send MIDI when I his a button.

I have a stable, reliable connection that sends a note over and over using this code, stolen from Tom Igoe mostly.

#include <ArduinoBLE.h>
#include <MIDIUSB.h>

byte midiData[] = {0x80, 0x80, 0x00, 0x00, 0x00};

// set up the MIDI service and MIDI message characteristic:
BLEService midiService("03B80E5A-EDE8-4B33-A751-6CE34EC4C700");
BLECharacteristic midiCharacteristic("7772E5DB-3868-4112-A1A9-F2669D106BF3",
                                     BLEWrite | BLEWriteWithoutResponse |
                                     BLENotify | BLERead, sizeof(midiData));

void setup() {
  // initialize serial communication
  Serial.begin(9600);
  // initialize built in LED:
  pinMode(LED_BUILTIN, OUTPUT);
  // Initialize BLE:
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (true);
  }
  // set local name and advertised service for BLE:
  BLE.setLocalName("TEST_BLE");
  BLE.setAdvertisedService(midiService);

  // add the characteristic and service:
  midiService.addCharacteristic(midiCharacteristic);
  BLE.addService(midiService);

  // start advertising
  BLE.advertise();
}

void loop() {
  // wait for a BLE central
  BLEDevice central = BLE.central();

  // if a central is connected to the peripheral:
  if (central) {
    // turn on LED to indicate connection:
    digitalWrite(LED_BUILTIN, HIGH);
    
    // insert function
   midiCommand(0x90, 66, 127);
   midiCommand(0x80, 66, 127);
  }
  
  // when the central disconnects, turn off the LED:
  digitalWrite(LED_BUILTIN, LOW);
}

// send a 3-byte midi message
void midiCommand(byte cmd, byte data1, byte  data2) {
  // MIDI data goes in the last three bytes of the midiData array:
  midiData[2] = cmd;
  midiData[3] = data1;
  midiData[4] = data2;

  midiCharacteristic.setValue(midiData, sizeof(midiData));
}

So that works just how I expect.

BUT.. If l replace the noteon/noteoff part, or even just comment it out.. like this:

#include <ArduinoBLE.h>
#include <MIDIUSB.h>

byte midiData[] = {0x80, 0x80, 0x00, 0x00, 0x00};

// set up the MIDI service and MIDI message characteristic:
BLEService midiService("03B80E5A-EDE8-4B33-A751-6CE34EC4C700");
BLECharacteristic midiCharacteristic("7772E5DB-3868-4112-A1A9-F2669D106BF3",
                                     BLEWrite | BLEWriteWithoutResponse |
                                     BLENotify | BLERead, sizeof(midiData));

void setup() {
  // initialize serial communication
  Serial.begin(9600);
  // initialize built in LED:
  pinMode(LED_BUILTIN, OUTPUT);
  // Initialize BLE:
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (true);
  }
  // set local name and advertised service for BLE:
  BLE.setLocalName("TEST_BLE");
  BLE.setAdvertisedService(midiService);

  // add the characteristic and service:
  midiService.addCharacteristic(midiCharacteristic);
  BLE.addService(midiService);

  // start advertising
  BLE.advertise();
}

void loop() {
  // wait for a BLE central
  BLEDevice central = BLE.central();

  // if a central is connected to the peripheral:
  if (central) {
    // turn on LED to indicate connection:
    digitalWrite(LED_BUILTIN, HIGH);
    
    // insert function
 //  midiCommand(0x90, 66, 127);
  // midiCommand(0x80, 66, 127);
  }
  
  // when the central disconnects, turn off the LED:
  digitalWrite(LED_BUILTIN, LOW);
}

// send a 3-byte midi message
void midiCommand(byte cmd, byte data1, byte  data2) {
  // MIDI data goes in the last three bytes of the midiData array:
  midiData[2] = cmd;
  midiData[3] = data1;
  midiData[4] = data2;

  midiCharacteristic.setValue(midiData, sizeof(midiData));
}

It compiles but will not connect via bluetooth anymore. The only difference is these lines commented out:
// midiCommand(0x90, 66, 127);
// midiCommand(0x80, 66, 127);
Though if I replace them with the code I want.. it still fails to connect

What am I missing here? Can anyone advise? I don't understand how taking away those lines affects how it connects :frowning: