EASession stream function doesn't fire

2019-08-17 03:33发布

问题:

I have MFi device(RFID reader) and I'm trying to catch scanned tags in my app. So I use EAAccessorymanager to get the shared accessory and read data from it. When I open session and scan tags, stream function does not fire and I can't understand what I'm doing wrong.

import Foundation
import ExternalAccessory


class RFID: NSObject, EAAccessoryDelegate, StreamDelegate {

        var _accessory: EAAccessory!
        var _session: EASession!
        var _protocolString: String?


    func turnOn() {
        let manager = EAAccessoryManager.shared()
        let accessories = manager.connectedAccessories
        if (accessories.count > 0) {
            _accessory = accessories[0];

            _session = EASession(accessory: _accessory, forProtocol: _accessory.protocolStrings[0]);

            _accessory.delegate = self;
            _session?.inputStream?.delegate = self;
            _session?.inputStream?.schedule(in: .current, forMode: .default);
            _session?.inputStream?.open();

            print(_accessory);
            print(_session);
        }
    }

    func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
        switch eventCode {
        case Stream.Event.openCompleted:
            break
        case Stream.Event.hasBytesAvailable:
            // Read Data
            print("Data Available");
            break
        case Stream.Event.hasSpaceAvailable:
            // Write Data
            break
        case Stream.Event.errorOccurred:
            break
        case Stream.Event.endEncountered:
            break

        default:
            break
        }
    }
}

NOTE: printed accessory is always right one!