Worklight 6.0 Barcode or QR code Scanner issue

299 views Asked by At

I am doing POC for QR-code or barcode scanner using worklight 6.0 and using cordova plugin. I tried to scan the QR code, it's working fine. I was facing issues when I simply start the scanner but I did not anything scan yet. Just I press back button on android our apps closed, I need to come back to app from hybrid part.

Can anyone suggest me. When I cancel the scanner the app is closed, I need come back to hybrids part.

My Android plugin:

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;


public class BarcodeScanner extends CordovaPlugin {
    public static final int REQUEST_CODE = 0x0ba7c0de;

    private static final String SCAN = "scan";
    private static final String ENCODE = "encode";
    private static final String CANCELLED = "cancelled";
    private static final String FORMAT = "format";
    private static final String TEXT = "text";
    private static final String DATA = "data";
    private static final String TYPE = "type";
    private static final String SCAN_INTENT = "com.phonegap.plugins.barcodescanner.SCAN";
    private static final String ENCODE_DATA = "ENCODE_DATA";
    private static final String ENCODE_TYPE = "ENCODE_TYPE";
    private static final String ENCODE_INTENT = "com.phonegap.plugins.barcodescanner.ENCODE";
    private static final String TEXT_TYPE = "TEXT_TYPE";
    private static final String EMAIL_TYPE = "EMAIL_TYPE";
    private static final String PHONE_TYPE = "PHONE_TYPE";
    private static final String SMS_TYPE = "SMS_TYPE";

    private static final String LOG_TAG = "BarcodeScanner";

    private CallbackContext callbackContext;

    /**
     * Constructor.
     */
    public BarcodeScanner() {
    }
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
        this.callbackContext = callbackContext;
        Log.d(LOG_TAG, "*************************** EXECUTE ************************************");
        if (action.equals(ENCODE)) {
            JSONObject obj = args.optJSONObject(0);
            if (obj != null) {
                String type = obj.optString(TYPE);
                String data = obj.optString(DATA);

                // If the type is null then force the type to text
                if (type == null) {
                    type = TEXT_TYPE;
                }

                if (data == null) {
                    callbackContext.error("User did not specify data to encode");
                    return true;
                }

                encode(type, data);
            } else {
                callbackContext.error("User did not specify data to encode");
                return true;
            }
        } else if (action.equals(SCAN)) {
            Log.d(LOG_TAG, "*************************** SCAN START ************************************");
            scan();
        } else {
            Log.d(LOG_TAG, "*************************** SCAN FALSE ************************************");
            return false;
        }
        return true;
    }

    /**
     * Starts an intent to scan and decode a barcode.
     */
    public void scan() {
        Intent intentScan = new Intent(SCAN_INTENT);
        intentScan.addCategory(Intent.CATEGORY_DEFAULT);

        this.cordova.startActivityForResult((CordovaPlugin) this, intentScan, REQUEST_CODE);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                Log.d(LOG_TAG, "*************************** RESULT OK ************************************");
                JSONObject obj = new JSONObject();
                try {
                    obj.put(TEXT, intent.getStringExtra("SCAN_RESULT"));
                    obj.put(FORMAT, intent.getStringExtra("SCAN_RESULT_FORMAT"));
                    obj.put(CANCELLED, false);
                } catch (JSONException e) {
                    Log.d(LOG_TAG, "This should never happen");
                }
                //this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
                this.callbackContext.success(obj);
            } else if (resultCode == Activity.RESULT_CANCELED) {
                Log.d(LOG_TAG, "*************************** RESULT CANCELED ************************************");
                JSONObject obj = new JSONObject();
                try {
                    obj.put(TEXT, "cancel");
                    obj.put(FORMAT, "cancel");
                    obj.put(CANCELLED, true);
                } catch (JSONException e) {
                    Log.d(LOG_TAG, "This should never happen");
                }
                //this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
                this.callbackContext.success(obj);
                //this.cordova.getActivity().finish();
            } else {
                //this.error(new PluginResult(PluginResult.Status.ERROR), this.callback);
                this.callbackContext.error("Unexpected error");
            }
        }
    }

    /**
     * Initiates a barcode encode.
     *
     * @param type Endoiding type.
     * @param data The data to encode in the bar code.
     */
    public void encode(String type, String data) {
        Intent intentEncode = new Intent(ENCODE_INTENT);
        intentEncode.putExtra(ENCODE_TYPE, type);
        intentEncode.putExtra(ENCODE_DATA, data);
        this.cordova.getActivity().startActivity(intentEncode);
    }
}

Callback for JavaScript:

    function onScan(){
        cordova.exec(onScanSuccess, onScanFailure, 'BarcodeScanner', 'scan', []);

    }

    function onScanSuccess(result) {
        console.log("onScanSuccess @@@@@@@@@@@@@@@@@@@@ text  ==" + result.text );
        console.log("onScanSuccess @@@@@@@@@@@@@@@@@@@@ format == " + result.format );
        }

        function onScanFailure(error) {
             console.log("onScanFailure @@@@@@@@@@@@@@@@@@@@ " + result);
        }
1

There are 1 answers

2
Idan Adar On

You need to override the back button, to do the same as your RESULT_CANCLE. You can see an example of how to override the button, here: Override back button to act like home button as well as in many other seraches for "override android back button".

Something like this code snippet:

// 2.0 and above
@Override
public void onBackPressed() {
  // code here
}

// Before 2.0
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
      // code here
    }
    return super.onKeyDown(keyCode, event);
}