Get Barcode in Cordova

40 views
Skip to first unread message

Alexandru Pufan

unread,
Jul 11, 2016, 12:52:02 PM7/11/16
to phonegap

Hello! I have a PE900S PDA device, on which I'm installing an Ionic application that needs to scan barcodes using the device integrated laser scanner.

For this, I've created a Cordova plugin, whose class in Main.java looks like this for now:


public class Scanner extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {

    if (action.equals("scan")) {

        Intent intentService = new Intent("com.hyipc.core.service.barcode.BarcodeService2D");
        intentService.putExtra("KEY_ACTION", "UP");

        cordova.getActivity().startService(intentService);
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        intentService.putExtra("KEY_ACTION", "DOWN");
        cordova.getActivity().startService(intentService);

        intentService.putExtra("KEY_ACTION", "TONE=100");

        return true;

    } else {

        return false;

    }
}


This code starts the scanner, and creates a beep noise when a scan succeeds. The problem is that I don't know how to capture the result (the barcode string), so that I can send it back to my Ionic app.

I have this example Java code from the manufacturer, which is supposed to do what I want, but I don't know how to integrate it in my app. Any thoughts?


package com.example.getbarcode;

import java.security.PublicKey;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
    public static final String ACTION_BARCODE_SERVICE_BROADCAST = "action_barcode_broadcast";
    private BroadcastReceiver mReceiver = new BarcodeReceiver();
    public static final String KEY_BARCODE_STR = "key_barcode_string";
    private TextView tvBarcode;
    private String strBarcode = "";
    private Button btnScan;
    public static final String KEY_ACTION = "KEY_ACTION";
    public static final String TONE = "TONE=100";
    private Intent intentService = new Intent("com.hyipc.core.service.barcode.BarcodeService2D");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tvBarcode = (TextView) this.findViewById(R.id.tvBarcode);
    btnScan = (Button) this.findViewById(R.id.btnScan);
    intentService.putExtra(KEY_ACTION, TONE);
    this.startService(intentService);
}

public class BarcodeReceiver extends BroadcastReceiver {
    public void onReceive(Context ctx, Intent intent) {
        if (intent.getAction().equals(ACTION_BARCODE_SERVICE_BROADCAST)) {
                strBarcode = intent.getExtras().getString(KEY_BARCODE_STR);
                tvBarcode.setText(strBarcode);
                strBarcode = "";
        }
    }
}

public void doClick(View v){
    if (v.equals(btnScan)) {
        intentService.putExtra(KEY_ACTION, "UP");
         .startService(intentService);
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        intentService.putExtra(KEY_ACTION, "DOWN");
        this.startService(intentService);
    }
}

@Override
protected void onPause() {
    super.onPause();
    //close barcodePower

    this.unregisterReceiver(mReceiver);
}

@Override
protected void onResume() {
    super.onResume();
    intentService.putExtra(KEY_ACTION, "INIT");
    this.startService(intentService);

    // register receiver
    IntentFilter filter = new IntentFilter();
    filter.addAction(ACTION_BARCODE_SERVICE_BROADCAST);
    this.registerReceiver(mReceiver, filter);

}}



Reply all
Reply to author
Forward
0 new messages