darryncampbell / darryncampbell-cordova-plugin-intent

General purpose intent shim layer for cordova appliations on Android. Handles various techniques for sending and receiving intents.
MIT License
86 stars 134 forks source link

Ionic registerBroadcastReceiver cannot be used with subscribe #50

Closed jasonfish568 closed 6 years ago

jasonfish568 commented 6 years ago

Hi,

I've just been wondering the conflict of the code you mentioned in a pull request and the current code. The registerBroadcastReceiver returns void in the source code, but the code you mentioned before used subscribe.

If we can't use subscribe with registerBroadcastReceiver, then what is the correct way to get the message sent from Datawedge API?

this.webIntent.registerBroadcastReceiver({ filterActions: [ 'com.example.ACTION' ], filterCategories: [ 'android.intent.category.DEFAULT' ] }).subscribe((intent) => {console.log('Received Intent: ' + JSON.stringify(intent.extras));});

Thanks and regards

darryncampbell commented 6 years ago

Hi, Please use the plugin directly

(<any>window).plugins.intentShim.registerBroadcastReceiver

For the full code in context, please see https://github.com/Zebra/ZebraIonicDemo/blob/master/src/providers/barcode/barcode.ts#L23

The changes to ionic native to allow use of subscribe() with registerBroadcastReceiver are in version 5 (of ionic native) which is currently at beta.. I have made changes to the ZebraIonicDemo sample in preparation for this fix moving out of beta in a separate branch: https://github.com/Zebra/ZebraIonicDemo/blob/Transition_To_WebIntent/src/providers/barcode/barcode.ts

jasonfish568 commented 6 years ago

Hi Darryn, Thanks for your reply. Now I see why the import of webIntent got commented out.

Just wondering, as I am using a MC36 device. It doesn't come with the default Datawedge but a Scan Config app. Everything seems very similar. However, the two demo apps: DataWedgeIonic2 and Zebra Ionic Demo both don't get a response from the content. Zebra Ionic Demo says the datawedge version is pre 6.3, even after I manually created the profile, it still doesn't work. By any chance you might have seen this before?

darryncampbell commented 6 years ago

I'm aware of the MC36 but because it is China exclusive I have never had the opportunity to use one unfortunately. Looking at the integrator guide (https://www.zebra.com/content/dam/zebra_new_ia/en-us/manuals/mobile-computers/mc36-integrator-guide-en-us.pdf) it says the 'Scan Config' does support intent output but it doesn't specify how the intents are sent - it could be that this device is not sending broadcast intents but rather sending the intents as startActivity(). Googling around I found this post from last year - https://developer.zebra.com/thread/34563 where I was not very helpful but the original poster stated they could not receive intents regardless of how they tried to receive them

I still suspect the intents are invoked via startActivity() so you might want to try using the getIntent() method from this plugin.

jasonfish568 commented 6 years ago

Um, right. We have come to the same point. I didn't know MC36 was China exclusive and now it explains why it doesn't have google services framework included.

Speaking of the getIntent() function, is it a listener or something? Should I use startActivity() before using it? The ScanDemo that comes with the machine does receive intent, as the scan button fired the scanning and send the intent to the demo app. If this is the case, then I don't think the app should start any activity? By the way, ionic does not detect keydown event on the yellow scan buttons which is another annoying thing to me.

darryncampbell commented 6 years ago

getIntent() just maps to Android's Activity's getIntent() function, https://developer.android.com/reference/android/app/Activity.html#getIntent() so it returns the intent that started the activity, you would call it in onCreate() or onResume() in the Android lifecycle.

I think we are in the same boat not understanding how the Intents are sent on MC36.

One thing to check is that you are registering for the correct intent category when receiving broadcasts - in that integrator guide it says the default category is com.symbol.category.DEFAULT - this is obviously not the default android category so you'll need to register for it in your broadcast intent filter.

jasonfish568 commented 6 years ago

I am now working on DatawedgeIonic2,

Sorry this seems very nooby but I don't quite understand how this would work. I've put the code in app.component.ts in the constructor. Is this how I should call this function?

(<any>window).plugins.intentShim.getIntent( function(intent) { console.log('Action' + JSON.stringify(intent.action)); var intentExtras = intent.extras; if (intentExtras == null) intentExtras = "No extras in intent"; console.log('Received Intent: ' + JSON.stringify(intent.extras)); let toast = toastCtrl.create({ message: 'Scan: ' + intent.extras["com.symbol.datawedge.data_string"], duration: 3000 }); toast.present(); }, function() { console.log('Error getting launch intent'); });

I have set the Intent Action to com.ionic.datawedge.ACTION, the intent category to com.android.intent.category.DEFAULT. I've changed these two parameters back and forth but never got it working.

darryncampbell commented 6 years ago

I suggest :

jasonfish568 commented 6 years ago

Thanks Darryn, I will come back after testing with the API Exerciser.

jasonfish568 commented 6 years ago

I have tested the Exerciser on MC36. The app says the machines does not support Datawedge API since version 6.2. Hence I believe the firmware does not have a complete Datawedge installed. The Scan Config is probably just a simple demo developed by the Chinese team that is selling this model.

As of using ionic as our framework, it would be a bit difficult for us to use the getContent() function I reckon? Or maybe we have to manually add on the intent filter into the manifest file?

Another question is, is there any way that I can download a firmware of a model that is sold generally in other countries and install it onto this MC36? I couldn't find any other models that look like MC36(the keyboard is very useful) on Zebra's website. And indeed, even if I did find one I wouldn't be able to download the firmware anyway. But like TC25, these are all Android computers, I'm guessing they are very much similar.

darryncampbell commented 6 years ago

Even if the device is not running Datawedge 6.2, the exerciser app should still be able to show data received from intents and could therefore be used to determine how Scan Config is sending those intents.

All the MC36 system images are here: https://www.zebra.com/us/en/support-downloads/software/operating-system/mc36-operating-system-2d.html - I don't think there was a variant sold outside of China and yes, you would need a support contract to download the image. Zebra sell a number of devices with a hardware keyboard, including the newly released MC3300 - to discuss sales I suggest you contact Zebra directly.

jasonfish568 commented 6 years ago

I am now trying to use adb to simulate the activity.

However, when using the following command, it generates error:

./adb shell am start -a com.symbol.scanconfig.SCANDEMO -c com.symbol.category.DEFAULT

Error: Error: Activity not started, unable to resolve Intent { act=com.symbol.scanconfig.SCANDEMO cat=[com.symbol.category.DEFAULT] flg=0x10000000 }

But if I specify the component name: ./adb shell am start -a "com.symbol.scanconfig.SCANDEMO" -c "com.symbol.category.DEFAULT" -n com.zebra.datawedgeionic/.MainActivity -e com.symbol.datawedge.data_string 123 It works fine.

The datawedge api exerciser does not receive anything when scanning. No matter how I set the profile, it simply doesn't trigger anything.

jasonfish568 commented 6 years ago

After talking to Zebra tech support I can confirm that MC36 does not have Datawedge installed. It has a Scanwedge installed. The tech support said to work with Scanwedge I would need to use the SDK available on Zebra website(https://www.zebra.com/us/en/support-downloads/software/developer-tools/mc36-sdk.html).

I will have a look at the SDK and then come back with what I found.

darryncampbell commented 6 years ago

Thanks for the update - I would be very interested to see what you find with the native SDK. It feels non-intuitive that interfacing with the Intent output requires a native SDK but since I have no experience with the platform I can not say.

jasonfish568 commented 6 years ago

Hi Darryn,

Yes just like what you said. I've only realised what I was doing after decompiled the SDK library file. The library file is a simple interface so I couldn't find the actual code hiding underneath.

But I shouldn't need to check the SDK in fact. I finally found the problem by using adb logcat, monitoring all intent activities. I found out that Scan Config only sends intent to that Scan Demo app, no matter what Action or Category I define. And that explains why DW API Exerciser couldn't receive any intent messages. It is very disappointing that MC36's system does not have Datawedge. Such a good looking machine deserve a better system. I would also suggest other developers if you are developing a hybrid app, do not work with MC36. It has very limited tech support too.

So, I modified the code in my project and used this web intent plugin and tested on another machine(not Zebra), it works perfect.

To anyone who comes across to this page and wondering why customs hardware buttons do not trigger keydown event, please read this page . For MC36, the reason was MC36 has a sort of full keyboard, the 3 yellow scan buttons did not use any keyCode that exists in normal keyboard. Therefore, in order to detect the keydown events, it is required to manually register these three keys in CordovaLib. This problem does not occur if you are developing a native app.

darryncampbell commented 6 years ago

Thanks for the analysis & feedback. Glad you finally got to the bottom of why you could not receive intents on the MC36, although it is not a solution! Although MC36 still receives security updates, the newer TC20/TC25 devices have taken over as a value-tier device which I am happy to say do support the full Datawedge - though I note you need a hardware keyboard so these devices would not be suitable for your needs.

darryncampbell commented 6 years ago

Closing this issue - the original issue was addressed.

jasonfish568 commented 5 years ago

In the end I have purchased a TC56(reseller saying TC20/TC25 not released yet, sounds like only happening in Australia). Using Datawedge on this one, took me only 30 seconds set up the profile and 10mins reading through the sample code and adjusted the app to receive proper intent data. Then it works right away. Thanks for providing all the example code!