当前位置: 首页 > 面试题库 >

Swift上的USB连接代表

裴畅
2023-03-14
问题内容

Swift中是否有代表可以通过计算机的USB插入新设备时让我的班级知道?我想知道何时有新设备可供我的程序使用。


问题答案:

这个答案对我有用但是它需要一些修改,例如创建桥接头以导入某些特定的IOKit部件。

首先,将IOKit.framework添加到您的项目中(在“链接的框架和库”中单击“ +”)。

然后创建一个新的空“ .m”文件,无论其名称如何。然后,Xcode将询问是否应创建“桥接标头”。说是。

忽略“ .m”文件。在Xcode刚刚创建的新的“ YOURAPPNAME-Bridging-Header.h”文件中,添加以下行:

#include <IOKit/IOKitLib.h>
#include <IOKit/usb/IOUSBLib.h>
#include <IOKit/hid/IOHIDKeys.h>

现在,您可以使用链接答案中的代码。这是一个简化的版本:

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)
    }
}

func deviceAdded(refCon: UnsafeMutablePointer<Void>, iterator: io_iterator_t) {
    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("Active device: \(deviceName!)")
        IOObjectRelease(usbDevice)
    }
}

func deviceRemoved(refCon: UnsafeMutablePointer<Void>, iterator: io_iterator_t) {
    // ...
}

注意:deviceAdded并且deviceRemoved必须是函数(不是方法)。

要使用此代码,只需启动观察器:

USBDetector.monitorUSBEvent()

这将列出当前已插入的设备,并且在每个新的USB设备插入/拔出事件中,它将打印设备名称。



 类似资料:
  •  USB连接 使用USB连接线,可连接下列的外接装置。 已安装Windows®或Mac OS等,可支持USB大容量储存装置之OS的个人计算机。 PS3™或HDD录放机等,能经由USB与PSP™联机的机器。 1. 用USB连接线连接PSP™主机与外接装置。 2. 进入(设定),并选择(USB连接)。 提示 启动PSP™主机电源时亦可连接。请同时参阅要连接之外接装置随附的使用说明书。 使用PSP-20

  • USB自动连接 使用USB连接线连接外接装置与已开启电源的PSP™后,PSP™会自动更换为USB模式。 关 不自动更换为USB模式。 开 自动更换为USB模式。 提示 正使用游戏等部份机能时,即使连接USB连接线,亦不会自动更换为USB模式。

  • 我不想获得连接到USB附件的许可,因为我想连接到特定的USB附件。有没有可能?正如我所读到的关于android.com的如下内容 注意:如果您的应用程序使用意向过滤器在连接附件时发现配件,则 那么有没有可能这样做。请帮帮忙。 谢谢高拉夫

  • 有没有办法打开tcpip端口5555进行调试,而不需要通过USB连接到电脑?

  • 问题内容: 我正在尝试从我的iOS应用程序到后端服务器(Node.js)建立简单的套接字连接(NO HTTP)。服务器证书已使用我自己创建的自定义CA创建并签名。我相信,为了让iOS信任我的服务器,我将不得不以某种方式将此自定义CA证书添加到用于确定Java / Android中TrustStore的工作方式的信任类型的受信任证书列表。 我尝试使用下面的代码进行连接,并且没有错误,但是write(

  • 我正在尝试检测已经连接到android的usb设备。我知道有动作可以检测USB何时连接或断开。但是我真的不知道如何在将usb设备连接到android后检查设备。另外,我发现每个USB设备都有它的设备类别代码,但是我怎么知道连接的是哪种设备呢?例如,我需要检测usb鼠标和键盘;我如何区分它们?