0

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!

Leri Gogsadze
  • 114
  • 1
  • 8
  • Verify first that `_session?.inputStream` is not nil. If it were, you'd get the behaviour you're describing – Rob Napier Jul 11 '19 at 14:08
  • @RobNapier No, it is not nil, as you can see, I printed it.. – Leri Gogsadze Jul 11 '19 at 14:31
  • Where did you print it? I don't mean `_session` is nil. I mean `inputStream` – Rob Napier Jul 11 '19 at 14:32
  • When I printed _session, it contained inputStream also and it was not nil. – Leri Gogsadze Jul 11 '19 at 14:34
  • First, those _ are hurting my brain :). You have implicitly unwrapped optionals, but then you are unwrapping them. Conditional unwrap is good, but for testing I would remove them as then you will get a crash if something is `nil`, which will point out any issues. The other question I would have is how are you retaining the instance of `RFID`? – Paulw11 Jul 12 '19 at 12:42
  • @Paulw11 Sorry for those underscores, I am not a Swift developer and I just followed samples... What do you mean when you say how I'm retaining the instance? – Leri Gogsadze Jul 12 '19 at 13:09
  • 1
    You must create an instance of your RFID class somewhere. You need to ensure you are holding a reference to that object or it will be released. For example if you only have it as a local variable in a function it will be released as soon as the function exits. – Paulw11 Jul 12 '19 at 21:40
  • @Paulw11Yeah, you're right. Solved. Thank you a lot :) – Leri Gogsadze Jul 15 '19 at 11:48

0 Answers0