0

I am trying to use the datawedge intent API with my flutter application, on a Zebra android scanner. I started to use the Zebra EMDK API from a git repository, which works perfectly. Now I want to migrate it (which is recommended by Zebra) because I want it to be also available on mobiles (if it is possible).

I am trying to follow the instructions from this page and merge it with the code from the git repo, but no scan event is detected in my app.

Has someone already done this and could help me?

Here is my MainActivity.java:

package com.example.test_datawedge;

import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
// import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.EventChannel.EventSink;
import io.flutter.plugin.common.EventChannel.StreamHandler;
import io.flutter.plugins.GeneratedPluginRegistrant;
import java.util.ArrayList;

public class MainActivity extends FlutterActivity {
  private static final String BARCODE_RECEIVED_CHANNEL = "samples.flutter.io/barcodereceived";


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
    // setContentView(R.layout.activity_main);

    IntentFilter filter = new IntentFilter();
    filter.addCategory(Intent.CATEGORY_DEFAULT);
    filter.addAction(getResources().getString(R.string.activity_intent_filter_action));
    // registerReceiver(myBroadcastReceiver, filter);

        new EventChannel(getFlutterView(), BARCODE_RECEIVED_CHANNEL).setStreamHandler(
            new StreamHandler() {

              private BroadcastReceiver barcodeBroadcastReceiver;

              @Override
              public void onListen(Object arguments, EventSink events) {
                Log.d("FLUTTERDEMO", "EventChannelOnListen");

                barcodeBroadcastReceiver = createBarcodeBroadcastReceiver(events);
                registerReceiver(
                        barcodeBroadcastReceiver, new IntentFilter("readBarcode"));
              }

              @Override
              public void onCancel(Object arguments) {
                Log.d("FLUTTERDEMO", "EventChannelOnCancel");

                unregisterReceiver(barcodeBroadcastReceiver);
                barcodeBroadcastReceiver = null;
              }
            }
    );

  }

//   @Override
//     protected void onDestroy()
//     {
//         super.onDestroy();
//         unregisterReceiver(myBroadcastReceiver);
//     }

    //
    // After registering the broadcast receiver, the next step (below) is to define it.  
    // Here it's done in the MainActivity.java, but also can be handled by a separate class.
    // The logic of extracting the scanned data and displaying it on the screen 
    // is executed in its own method (later in the code). Note the use of the 
    // extra keys are defined in the strings.xml file.  
    //  
    private BroadcastReceiver createBarcodeBroadcastReceiver(final EventSink events) {
        return new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                String action = intent.getAction();

                Log.d("FLUTTERDEMO", "createBarcodeBroadcastReceiver " + action);

                if(action.equals("readBarcode")){
                    String barcode = intent.getStringExtra("barcode");
                    String barcodetype = intent.getStringExtra("barcodetype");
                    Log.d("FLUTTERDEMO", "createBarcodeBroadcastReceiver " + barcode);
                    events.success(barcode);
                }
            }
        };
    }
    //
    // The section below assumes that a UI exists in which to place the data. A production 
    // application would be driving much of the behavior following a scan.
    //
    // private void displayScanResult(Intent initiatingIntent, String howDataReceived)
    // {
    //     String decodedSource = initiatingIntent.getStringExtra(getResources().getString(R.string.datawedge_intent_key_source));
    //     String decodedData = initiatingIntent.getStringExtra(getResources().getString(R.string.datawedge_intent_key_data));
    //     String decodedLabelType = initiatingIntent.getStringExtra(getResources().getString(R.string.datawedge_intent_key_label_type));

    //     final TextView lblScanSource = (TextView) findViewById(R.id.lblScanSource);
    //     final TextView lblScanData = (TextView) findViewById(R.id.lblScanData);
    //     final TextView lblScanLabelType = (TextView) findViewById(R.id.lblScanDecoder);
    //     lblScanSource.setText(decodedSource + " " + howDataReceived);
    //     lblScanData.setText(decodedData);
    //     lblScanLabelType.setText(decodedLabelType);
    // }
}
Siddharth Mehra
  • 1,691
  • 1
  • 9
  • 32
Adrien
  • 1
  • 2
  • A few things to check: Is DataWedge configured to send Intents with the appropriate action (looks like you have specified the action to be 'readBarcode'; The profile you are modifying to send intents is the profile associated with your application (or the default profile). I recommend setting something up with a known working example first - I don't have an example Flutter app but this Java app should at least help ensure you have DataWedge configured correctly: https://github.com/darryncampbell/DataWedge-Intent-Example-1 – Darryn Campbell Aug 06 '19 at 14:44
  • Ok I have something better, an event is fired in my dart class, but it is null. The problem came effectively from the action name : "readBarcode". My Datawedge was well configured. I am gonna try to resolve the null event now. Thank you for your advices. – Adrien Aug 06 '19 at 15:08
  • Great, please let me know how it goes – Darryn Campbell Aug 06 '19 at 15:28
  • 1
    same problem, I was using a wrong name : `String barcode = intent.getStringExtra("barcode");` => the right one was : `String barcode = intent.getStringExtra("com.symbol.datawedge.data_string");` Now, I can use my app on a mobile device and I will use an other solution to scan with the camera. – Adrien Aug 06 '19 at 15:34
  • @Adrien Did you figure this out ? – Danny D Aug 31 '20 at 19:35
  • @DannyD yes, sorry for the late reply. The problem was the intent declaration if i remember well – Adrien Nov 02 '20 at 11:43

1 Answers1

0

Zebra EMDK retrieves data by overriding the 'onStatus' and 'onData' functions.

Retrieve your barcode data from 'onData'

Rowan Berry
  • 171
  • 7