My cordova application not launching after NFC tag detect

一世执手 提交于 2019-12-31 03:15:15

问题


I am using Apache cordova to build android applications. I made an application with NFC feature.

we already written data into NFC tag, with mimetype: myApp/firstNFCApp. Inside my application whenever detect tag with this mimetype my application will read data and showing that data into user friendly manner. This way I implemented, it's working fine.This was the code I written to fetch data from tag

nfc.addNdefListener(
     function(nfcEvent){
       console.log(nfc.bytesToString(nfcEvent.tag.ndefMessage[0].payload));
     },
     function(){
       console.log("sucessfully created");
     },
     function(){
       console.log("something went wrong");
     }
);

Now I want to launch my application,whenever device detect a tag with mimetype: myApp/firstNFCApp . For this, I written following code

<intent-filter>
  <data android:mimeType="myApp/firstNFCApp" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

and I added android:noHistory="true" to activity element in androidManifest.xml file.

What I want :

If the device detect any tag with my mimetype,want to launch application also need's to trigger that callback(means,console will print).I am using chariotsolutions/phonegap-nfc plugin.

This is way, I tried it's not working. can anyone help me thank you.


回答1:


In order to receive an NFC intent together with the whole NDEF message in your app, you would need to define a proper intent filter that matches the first record in the above NDEF message:

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <action android:name="android.nfc.action.TAG_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

Refer how to handle NFC




回答2:


As SweetWisher wrote, you need to define a proper action for your intent filter (android.nfc.action.NDEF_DISCOVERED( in your case. In addition, you should be aware that MIME types used in the NDEF_DISCOVERED intent filter must always be all-lower-case. The reason is that MIME types are case-insensitive as per the RFC but Android's intent filter matching is case-sensitive. Consequently, Android will convert MIME types to all-lower-case before matching in order to overcome case-sensitivity issues (see this answer for a more detailed explaination).

As a result, you intent filter would look something like this:

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="myapp/firstnfcapp" />
</intent-filter>


来源:https://stackoverflow.com/questions/26688456/my-cordova-application-not-launching-after-nfc-tag-detect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!