【发布时间】:2017-02-21 19:50:36
【问题描述】:
这是我拥有的代码。我能够检测到 USB 设备何时连接,然后轮询它以查看该设备的 Device Name 是什么。我希望使用一些类似于查找 BSD 路径(我认为这就是它的名称)或 TTY/CU 安装位置的东西。当我尝试更改密钥时,我最终会收到 EXCC_BAD_INSTRUCTION 错误。
我知道它一定在某个地方! :P 任何帮助将不胜感激!提前致谢!
import Foundation
import Cocoa
class USBDetector {
class func monitorUSBEvent() {
var portIterator: io_iterator_t = 0
let matchingDict = IOServiceMatching(kIOUSBDeviceClassName)
let gNotifyPort: IONotificationPortRef = IONotificationPortCreate(kIOMasterPortDefault)
let runLoopSource: Unmanaged<CFRunLoopSource>! = IONotificationPortGetRunLoopSource(gNotifyPort)
let gRunLoop: CFRunLoop! = CFRunLoopGetCurrent()
CFRunLoopAddSource(gRunLoop, runLoopSource.takeRetainedValue(), kCFRunLoopDefaultMode)
let observer = UnsafeMutablePointer<Void>(unsafeAddressOf(self))
_ = IOServiceAddMatchingNotification(gNotifyPort,
kIOMatchedNotification,
matchingDict,
deviceAdded,
observer,
&portIterator)
deviceAdded(nil, iterator: portIterator)
_ = IOServiceAddMatchingNotification(gNotifyPort,
kIOTerminatedNotification,
matchingDict,
deviceRemoved,
observer,
&portIterator)
deviceRemoved(nil, iterator: portIterator)
}
class func check() {
var portIterator: io_iterator_t = 0
let matchingDict = IOServiceMatching(kIOUSBDeviceClassName)
let gNotifyPort: IONotificationPortRef = IONotificationPortCreate(kIOMasterPortDefault)
let runLoopSource: Unmanaged<CFRunLoopSource>! = IONotificationPortGetRunLoopSource(gNotifyPort)
let gRunLoop: CFRunLoop! = CFRunLoopGetCurrent()
CFRunLoopAddSource(gRunLoop, runLoopSource.takeRetainedValue(), kCFRunLoopDefaultMode)
let observer = UnsafeMutablePointer<Void>(unsafeAddressOf(self))
deviceAdded(nil, iterator: portIterator)
deviceRemoved(nil, iterator: portIterator)
}
}
func deviceAdded(refCon: UnsafeMutablePointer<Void>, iterator: io_iterator_t) {
var foundit = false
var kr: kern_return_t = KERN_FAILURE
while case let usbDevice = IOIteratorNext(iterator) where usbDevice != 0 {
let deviceNameAsCFString = UnsafeMutablePointer<io_name_t>.alloc(1)
defer {deviceNameAsCFString.dealloc(1)}
kr = IORegistryEntryGetName(usbDevice, UnsafeMutablePointer(deviceNameAsCFString))
if kr != KERN_SUCCESS {
deviceNameAsCFString.memory.0 = 0
}
let deviceName = String.fromCString(UnsafePointer(deviceNameAsCFString))
let key: CFString! = "BSD Path"
let bsdPathAsCFtring: AnyObject? = IORegistryEntryCreateCFProperty(usbDevice, key, kCFAllocatorDefault, 0).takeUnretainedValue()
let bsdPath = bsdPathAsCFtring as! String?
if let path = bsdPath {
print(path)
}
print("Found: \(deviceName!)")
if(deviceName == "Arduino Leonardo") {
foundit = true
}
IOObjectRelease(usbDevice)
}
var appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate
if(foundit == true) {
appDelegate.arduboyhere();
}
}
func deviceRemoved(refCon: UnsafeMutablePointer<Void>, iterator: io_iterator_t) {
var foundit = false
var kr: kern_return_t = KERN_FAILURE
while case let usbDevice = IOIteratorNext(iterator) where usbDevice != 0 {
let deviceNameAsCFString = UnsafeMutablePointer<io_name_t>.alloc(1)
defer {deviceNameAsCFString.dealloc(1)}
kr = IORegistryEntryGetName(usbDevice, UnsafeMutablePointer(deviceNameAsCFString))
if kr != KERN_SUCCESS {
deviceNameAsCFString.memory.0 = 0
}
let deviceName = String.fromCString(UnsafePointer(deviceNameAsCFString))
print("Removed: \(deviceName!)")
if(deviceName == "Arduino Leonardo") {
foundit = true
}
IOObjectRelease(usbDevice)sa
}
var appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate
if(foundit == true) {
appDelegate.arduboygone();
}
}
【问题讨论】: