Odk-x use intent to scan barcode in html page

Hello everyone,

I have HTML page like Geotagger where I can Search in the list using input text,
But I want to make a button where I can use the zxing barcode.
so I can easily find what I want from the list.
how can I make Intent in the javascript?
can anybody help me?

Thanks

1 Like

You can use the odkcommon.js injected interface to launch an intent using the odkCommon.doAction function.

Brief example:

    $('#barcode').on('click', function() {
        var dispatchStruct = JSON.stringify({actionTypeKey: actionBarcode,
            htmlPath:htmlFileNameValue, userAction:userActionValue});

        odkCommon.doAction(dispatchStruct, 'com.google.zxing.client.android.SCAN', null);
    });
1 Like

hello @W_Brunette
I did use the odkcommon.js to launch an intent but I can not receive data back .

window.addEventListener("load", function () {
odkCommon.registerListener(
function () {
alert("inside the listener");
var action = odkCommon.viewFirstQueuedAction();
$("#searchForText").val("inside the listener");
if (action !== null) {
console.debug(action);
$("#searchForText").val(JSON.stringify(action));
var dispatchStruct = action.dispatchStruct;
if (dispatchStruct && dispatchStruct.userAction == "scan-kast-code") {
var jsonValue = action.jsonValue;
$("#searchForText").val("status = " + jsonValue.status);
if (jsonValue.status === 0) {
$("#searchForText").val(jsonValue.result);
console.log(jsonValue);
}
}
// process action -- be idempotent!
// if processing fails, the action will still
// be on the queue.
odkCommon.removeFirstQueuedAction();
}
});
$("#barcode").click(function () {
var dispatchStruct = { userAction: "scan-kast-code" };
var intentArgs = {
// extras: extrasBundle,
// uri: // set the data field of intent to this
// data: // unless data is supplied -- that takes precedence
// type: // set the intent type to this value
// package: // set the intent package to this value
};

    return odkCommon.doAction(dispatchStruct,
        'com.google.zxing.client.android.SCAN',
        intentArgs);
});

})

the registerListener doesn't work or i don't know how to use it.

Can you check with me, please.

1 Like

There are a few issues with your listener:

  1. action.dispatchStruct is a JSON string, you need to parse it to read its content.
  2. action.jsonValue.status is expected to be -1
  3. The result of the scan is stored in action.jsonValue.result.SCAN_RESULT

If the issue is that the listener isn't being called at all, try calling your listener right after registering it.

1 Like