iOS开发 swift 检测手机连接的外设

import ExternalAccessory

override func viewDidLoad() {
    super.viewDidLoad()
    
    NotificationCenter.default.addObserver(self, selector: #selector(didConnectAccessory(_:)), name: Notification.Name.EAAccessoryDidConnect, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(didDisconnectAccessory(_:)), name: Notification.Name.EAAccessoryDidDisconnect, object: nil)

} 
    
    
    @objc
private func didConnectAccessory(_ notification: NSNotification) {
    let accessoryManager = EAAccessoryManager.shared()
    for accessory in accessoryManager.connectedAccessories {
        if accessory.protocolStrings.contains(communicationProtocol) {
            //We have found the accessory corresponding to our gadget
            let description = """
            Accessory name: \(accessory.name)
            Manufacturer: \(accessory.manufacturer)
            Model number: \(accessory.modelNumber)
            Serial number: \(accessory.serialNumber)
            HW Revision: \(accessory.hardwareRevision)
            FW Revision: \(accessory.firmwareRevision)
            Connected: \(accessory.isConnected)
            Connection ID: \(accessory.connectionID)
            Protocol strings: \(accessory.protocolStrings.joined(separator: "; "))
            """
            print(description)
          
        }
        
        self.accessory = accessory
        
    }
}
    
@objc
private func didDisconnectAccessory(_ notification: NSNotification) {
    print("disconnect")
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容