我正在使用 Arduino Nano 33 BLE 设备实现适用于 iPad 的 BLE MIDI 控制器。以下代码能够:
- 使设备可作为 BLE MIDI 外围设备被发现
- 与 BLE MIDI 中央应用程序建立连接
仅与 Android 应用程序的连接是稳定的。每个 iOS 应用程序(例如 Garageband、AUM 等)都会立即关闭连接(arduino 板上的指示灯会在几秒钟内打开和关闭),但如果设备不断发送 MIDI 消息(请查看注释中的代码行)loop()函数)连接永远保持活动状态;不幸的是,重复发送消息不是我想要实现的控制器的目的。
BLE 服务的特定配置或要实施的轮询操作可能符合严格的 iOS 标准,但我找不到任何适用于Nano 33 BLE设备的有效解决方案或示例,其中不包括在循环中发送注释()功能。
#include <ArduinoBLE.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));
bool midi_connected = false;
void setup() {
// initialize serial communication
Serial.begin(9600);
// initialize built in LED:
pinMode(LED_BUILTIN, OUTPUT);
// Initialize BLE service:
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (true);
}
BLE.setLocalName("MBLE");
BLE.setAdvertisedService(midiService);
BLE.setEventHandler(BLEConnected, onConnected);
BLE.setEventHandler(BLEDisconnected, onDisconnected);
midiCharacteristic.setEventHandler(BLEWritten, onWritten);
midiService.addCharacteristic(midiCharacteristic);
BLE.addService(midiService);
BLE.setConnectable(true);
BLE.setAdvertisingInterval(32);
BLE.setConnectionInterval(32, 64);
BLE.advertise();
}
void loop() {
BLEDevice central = BLE.central();
if (central) {
// midiCommand(0x90, 60, 127);
// delay(250);
// midiCommand(0x80, 60, 0);
// delay(250);
}
}
void onConnected(BLEDevice central) {
digitalWrite(LED_BUILTIN, HIGH);
midi_connected = true;
}
void onDisconnected(BLEDevice central) {
digitalWrite(LED_BUILTIN, LOW);
midi_connected = false;
}
void onWritten(BLEDevice central, BLECharacteristic characteristic) {
auto buffer = characteristic.value();
auto length = characteristic.valueLength();
if (length > 0)
{
// echo on the next midi channel
midiCommand(buffer[2], buffer[3], buffer[4]);
}
}
void midiCommand(byte cmd, byte data1, byte data2) {
midiData[2] = cmd;
midiData[3] = data1;
midiData[4] = data2;
midiCharacteristic.setValue(midiData, sizeof(midiData));
}