美文网首页
iOS开发 swift 检测手机连接的外设

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

作者: 塞北孤雁 | 来源:发表于2021-06-08 16:00 被阅读0次
    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")
    }
    

    相关文章

      网友评论

          本文标题:iOS开发 swift 检测手机连接的外设

          本文链接:https://www.haomeiwen.com/subject/zjkjeltx.html