datalogic / android-samples-astudio

Sample apps for Datalogic Android SDK
9 stars 39 forks source link

Doesn't work on release mode in flutter app #16

Open KalaliEhsan opened 1 year ago

KalaliEhsan commented 1 year ago

I want to support my Flutter app from the DATALOGIC PDA device. So I use maven dependency to add DATALOGIC Android SDK like this link. But I didn't use the following tag in the Android manifest:

The inclusion of the following tag (within the element) in all Android applications built with the Datalogic SDK is necessary:

<uses-library
android:name="com.datalogic.device"
android:required="true" />

Because I want to install my Flutter app on all Android devices (like mobile, tablet, etc.). So my native code with Java in my Flutter app is like the following:

public class MainActivity extends FlutterActivity {
    private final String TAG_NAME = getClass().getName();
    // datalogic pda scanner values
    private static final String DATALOGIC_EVENT_CHANNEL = "native/datalogic";
    private EventChannel.EventSink datalogicEvent;
    BarcodeManager decoder = null;
    ReadListener listener = null;

    @Override
    protected void onResume() {
        super.onResume();
        datalogicInit();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // [EventChannel] for datalogic pda scanner
        new EventChannel(Objects.requireNonNull(getFlutterEngine()).getDartExecutor(), DATALOGIC_EVENT_CHANNEL).setStreamHandler(
                new EventChannel.StreamHandler() {
                    @Override
                    public void onListen(Object args, final EventChannel.EventSink events) {
                        Log.w(TAG_NAME, "Adding listener");
                        datalogicEvent = events;
                    }

                    @Override
                    public void onCancel(Object args) {
                        Log.w(TAG_NAME, "Cancelling listener");
                        datalogicEvent = null;
                    }
                }
        );
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
       datalogicDispose();
    }

    private void datalogicInit() {
        // If the decoder instance is null, create it.
        if (decoder == null) { // Remember an onPause call will set it to null.
            decoder = new BarcodeManager();
        }

        // From here on, we want to be notified with exceptions in case of errors.
        ErrorManager.enableExceptions(true);

        try {
            // Create an anonymous class.
            listener = new ReadListener() {
                // Implement the callback method.
                @Override
                public void onRead(DecodeResult decodeResult) {
                    // Change the displayed text to the current received result.
                    datalogicEvent.success(decodeResult.getText());
                }
            };

            // Remember to add it, as a listener.
            decoder.addReadListener(listener);

        } catch (DecodeException e) {
            Log.e(TAG_NAME, "Error while trying to bind a listener to BarcodeManager", e);
        }
    }

    private void datalogicDispose(){
        // If we have an instance of BarcodeManager.
        if (decoder != null) {
            try {
                // Unregister our listener from it and free resources.
                decoder.removeReadListener(listener);

                // Let the garbage collector take care of our reference.
                decoder = null;
            } catch (Exception e) {
                Log.e(TAG_NAME, "Error while trying to remove a listener from BarcodeManager", e);
            }
        }
        datalogicEvent = null;
    }
}

And my dart code in Flutter app is like the following:

class PdaScannerListener extends StatefulWidget {
  final Widget child;
  final ValueChanged onScanned;

  const PdaScannerListener({
    super.key,
    required this.child,
    required this.onScanned,
  });

  @override
  State<StatefulWidget> createState() => PdaScannerListenerState();
}

class PdaScannerListenerState extends State<PdaScannerListener> {
  late EventChannel eventChannel;

  @override
  void initState() {
    eventChannel = EventChannel("native/datalogic");

    eventChannel.receiveBroadcastStream().listen((event) {
       widget.onScanned(event);
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}

My app works on debug mode but doesn't work on release mode. What is the reason?