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

2021-06-08  本文已影响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")
}
上一篇下一篇

猜你喜欢

热点阅读