我正在使用Arduino Nano 33 BLE设备实现iPad的BLE控制器。以下代码能够:
peripheral
连接。
这种连接只与Android应用程序保持稳定。每个iOS应用程序(如Garageband、AUM等)立即关闭连接( arduino板上的led在几秒钟内打开或关闭),但是如果设备不断发送MIDI消息(查看循环()函数中注释的代码行),则连接将永远保持活动状态;不幸的是,重复发送消息并不是我想要实现的控制器的目的。
可能要实现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));
}发布于 2020-11-06 21:17:10
我(终于)自己找到了一个解决方案,看看苹果公司提供的MIDI BLE规范。
附件应要求连接间隔15 ms或以下。Apple建议从连接间隔11.25 ms的请求开始,如果连接请求被Apple产品拒绝,则为15 ms。超过15 ms的间隔不适合于现场播放情况。
以及以后的
支持蓝牙低能量MIDI的
苹果设备将尝试在与附件建立连接后读取MIDI /O特性。..。附件应响应最初的MIDI /O特性,使用没有有效载荷的数据包读取。
因此,我在setup()函数中更改了连接间隔
BLE.setConnectionInterval(9, 12);,并在连接事件处理函数中包含了几条线路。
void onConnected(BLEDevice central) {
digitalWrite(LED_BUILTIN, HIGH);
midi_connected = true;
midiCharacteristic.setValue(0);
}就这样!
https://stackoverflow.com/questions/64685875
复制相似问题