有没有办法判断一个MIDI设备是否通过USB在iOS上连接?

时间:2017-03-28 13:31:36

标签: ios swift midi coremidi

我使用CoreMIDI通过iOS-Devices上的Camera Connection Kit从MIDI键盘接收消息。我的应用程序是关于音高识别。我希望以下功能是自动的:

默认使用麦克风(已经实现),如果连接了MIDI键盘,则使用麦克风。

它可以找出如何判断它是否是使用默认驱动程序的USB键盘。只需要询问名为" USB-MIDI":

的设备
private func getUSBDeviceReference() -> MIDIDeviceRef? {
    for index in 0..<MIDIGetNumberOfDevices() {
        let device = MIDIGetDevice(index)
        var name : Unmanaged<CFString>?
        MIDIObjectGetStringProperty(device, kMIDIPropertyName, &name)
        if name!.takeRetainedValue() as String == "USB-MIDI" {
            return device
        }
    }
    return nil
}

但不幸的是,USB-Keyboard使用自定义驱动程序。 如何查看其中一个?标准蓝牙和网络设备似乎始终在线。即使在设备上打开了Wifi和蓝牙(奇怪吗?)。

1 个答案:

答案 0 :(得分:0)

我最终使用了USBLocationID。它适用于我目前测试的任何设备,并没有用户抱怨。但我不希望很多用户使用我的应用程序的MIDI功能。

/// Filters all `MIDIDeviceRef`'s for USB-Devices
private func getUSBDeviceReferences() -> [MIDIDeviceRef] {
        var devices = [MIDIDeviceRef]()
        for index in 0..<MIDIGetNumberOfDevices() {
            let device = MIDIGetDevice(index)
            var list: Unmanaged<CFPropertyList>?
            MIDIObjectGetProperties(device, &list, true)
            if let list = list {
                let dict = list.takeRetainedValue() as! NSDictionary
                if dict["USBLocationID"] != nil {
                    devices.append(device)
                }
            }
        }
        return devices
    }