与fluter一起使用Datawedge

时间:2019-08-06 13:52:05

标签: flutter datawedge

我正在尝试在Zebra android扫描仪上的flutter应用程序中使用datawedge intent API。我开始使用a git repository中的Zebra EMDK API,它非常有效。现在,我想迁移它(Zebra推荐使用它),因为我希望它也可以在手机上使用(如果可能的话)。

我正在尝试按照this page中的说明进行操作,并将其与git repo中的代码合并,但是在我的应用中未检测到扫描事件。

有人已经这样做并且可以帮助我吗?

这是我的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 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);
    // }
}

1 个答案:

答案 0 :(得分:0)

Zebra EMDK通过重写'onStatus'和'onData'函数来检索数据。

从“ onData”中检索条形码数据