jkbz64 / capacitor-datawedge

Capacitor plugin for DataWedge API
BSD 3-Clause "New" or "Revised" License
7 stars 8 forks source link

Customizable Event Name? #12

Closed cr1979 closed 3 months ago

cr1979 commented 3 months ago

Hello,

We are currently migrating our project from Cordova to Capacitor, and our currently used DataWedge plugin "com-darryncampbell-cordova-plugin-intent" is no longer compatible. Now, I have come across this plugin and it works, but unfortunately, a hardcoded event name is used in the Java code: "com.capacitor.datawedge.RESULT_ACTION". With the other plugin, we could determine the event name ourselves, for example like this:

(<any>window).plugins.intentShim.registerBroadcastReceiver(
  {
    filterActions: ['de.comp.prod.ACTION'],
    filterCategories: ['com.android.intent.category.DEFAULT'],
  },
  (intent) => {
    console.log(intent.extras['com.symbol.datawedge.data_string']);
    this.loggingService.log('scanService', 'Barcode Zebra: ' + intent.extras['com.symbol.datawedge.data_string']);
    this.barcode$.next(intent.extras['com.symbol.datawedge.data_string']);
  },
);

In this example, for data protection reasons, I used "comp.prod".

If we use this plugin now, our customers would have to change the event on each scanner. I foresee problems where customers might not understand how to do this.

Is it possible with this plugin to determine the event name in the code? For example, "de.comp.prod.ACTION" instead of "com.capacitor.datawedge.RESULT_ACTION".

jkbz64 commented 3 months ago

Hey, the action name is currently hardcoded, it should be trivial to modify the library to use custom one. Currently I don't have java/android setup environment and the device on hand to test the code. I will try to test it and package/deploy it soon when I get the chance, otherwise feel free to fork the library and do it yourself (code would look like that https://github.com/jkbz64/capacitor-datawedge/commit/15a75abb361e45f15d9806adca7256e8fb18a6cd, did not test it though).

cr1979 commented 3 months ago

Hey @jkbz64, Thank you so much for your support. I forked your project and made just a small change.

I changed

if (!action.equals(this.scanIntent)) return;

to

if (!action.equals(scanIntent)) return;

this.scanIntent refers to BroadcastReceiver and not the DataWedgePlugin Class. You will get a compiler error.

After that change, your code works fine with this code:

DataWedge.__registerReceiver({ intent: 'de.comp.prod.ACTION' });

DataWedge.addListener('scan', (event) => {
  console.log(event.data);
  this.barcode$.next(event.data);
});

I also tested without DataWedge.__registerReceiver({ intent: 'de.comp.prod.ACTION' }); it will use com.capacitor.datawedge.RESULT_ACTION again.

You find my fork here: https://github.com/cr1979/capacitor-datawedge I made the change in the custom-intent branch. I can also send a pull request if you would like.

jkbz64 commented 3 months ago

this.scanIntent refers to BroadcastReceiver and not the DataWedgePlugin Class. You will get a compiler error.

Ah yes, I did not even compile it, was blind writing the code :p

Thank you for fixing and testing the changes, please send the pull request if you want to, I will package and publish it :)

cr1979 commented 3 months ago

Pull request sent. I hope that's ok. It is my first pull request. Thanks again for your help and for that great plugin!

jkbz64 commented 3 months ago

Should be released in 0.3.1, let me know if it does not work.

cr1979 commented 3 months ago

That's okay. In the meantime, I will refer to my fork in the package.json. Do you know approximately when version 0.3.1 will be released?

cr1979 commented 3 months ago

As I see you already merged it into the master branch :)