我有一个小应用程序,可通过BLE将Midi服务器连接到ios。 工作正常,但应用程序MIDIGetNumberOfSources不会返回所有源,仅返回默认源,而不返回带有midi数据的真实通道。 我必须再次调用MIDIGetNumberOfSources来获取它。
... 让计数:Int = MIDIGetNumberOfSources() ...
即使ios设备与Midi服务器配对并且进行了所有设置(midi uuid,找到的服务特征等)。
关于如何监视何时调用MIDIGetNumberOfSources的任何想法?
编辑 这是设置通道的Midi呼叫的摘要
// connection to BT services through BTCentralManager
let serviceMIDICharacteristic_UUID = CBUUID(string: "7772E5DB-3868-4112-A1A9-F2669D106BF3")
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if let characteristic = service.characteristics?.first(where: { $0.uuid == serviceMIDICharacteristic_UUID })
{
// list up MIDI channels
let count:Int = MIDIGetNumberOfSources()
for i in 0..<count {
let endpoint:MIDIEndpointRef = MIDIGetSource(i);
...
// usually we get 2 channels, the default "Network session 1" (i=0) and
// the "real" channel "iPhone... " (i=1) that have the server name etc..
// but I have to call MIDIGetNumberOfSources() few times to see it
//
}
// once MIDI service is connected from Bluetooth
let src = endpoint (i=1)
MIDIReceivePacket.init()
class MIDIReceivePacket {
...
// ======================== INITIALIZE MIDI - CREATE INPORT ===================================
init() {
if midiClient == 0 {
let result = MIDIClientCreateWithBlock(clientName, &midiClient, myNotifyCallback) }
...
err = MIDIInputPortCreate( midiClient, "MIDI_InPort" as CFString, MyMIDIReadProc, nil, &inPort)
...
err = MIDIPortConnectSource(inPort, src, &src)
...
}
func myNotifyCallback(message:UnsafePointer<MIDINotification>) -> Void {
let messageID = message.pointee.messageID
switch (messageID) {
case MIDINotificationMessageID.msgSetupChanged:
NSLog("MIDI setup changed")
break
...
}
func MyMIDIReadProc (pktList: UnsafePointer<MIDIPacketList>,
readProcRefCon: UnsafeMutableRawPointer?, srcConnRefCon: UnsafeMutableRawPointer?) -> Void
{
let packetList:MIDIPacketList = pktList.pointee
var packet:MIDIPacket = packetList.packet
...
// data read here
for _ in 1...packetList.numPackets
{
let bytes = Mirror(reflecting: packet.data).children
...
// long code here to read the buffer (found somewhere on SO)
}
以上所有调用均不会显示任何错误,并且当MIDIGetNumberOfSources返回正确数目的源时,我可以连接和接收Midi数据而不会出现任何问题。
注意;服务器是macOS,应用是iOS