Java Question Adding new functionalities and tags decoding to NFC library

aeropic

Active Member
Licensed User
Longtime User
For this specific app, I believe you need to put a file "tech_filter.xml" into the directory "$yourworkingdir/Objects/res/xml" and make it read only. Here's a sample file, you can copy it to your directory.

Thanks JJCC, I copied the XML file and this solves the first error (not too bad !!!)

But there is still the other error:
B4X:
Parsing code.                           0.00
Compiling code.                         0.02
Compiling layouts code.                 0.00
Generating R file.                      0.02
Compiling generated Java code.          0.31
Convert byte code - optimized dex.      Error
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dx.cf.iface.ParseException: class name (BADoclet$Property) does not match path (BADoclet/BADoclet$Property.class)
    at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:520)
    at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
    at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
    at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
    at com.android.dx.command.dexer.Main.processClass(Main.java:665)
    at com.android.dx.command.dexer.Main.processFileBytes(Main.java:634)
    at com.android.dx.command.dexer.Main.access$600(Main.java:78)
    at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:572)
    at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
    at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
    at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
    at com.android.dx.command.dexer.Main.processOne(Main.java:596)
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:498)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:264)
    at com.android.dx.command.dexer.Main.run(Main.java:230)
    at com.android.dx.command.dexer.Main.main(Main.java:199)
    at com.android.dx.command.Main.main(Main.java:103)
...while parsing BADoclet/BADoclet$Property.class
1 error; aborting
    Optimized dexer failed. Switching to Standard dexer.

The problems occurs with NFC 1.23 and above. It is not present with NFC 1.22 ...
 

jjcc

Member
Licensed User
Longtime User
Don't quite understand. Maybe Erel can have some insight. I'm using B4A ver 3.00, it works. The B4ADoclet is quite old one though.
The difference between 1.22 and 1.23 is NfcV type functions. 1.24 fixed a bug, 1.25 added Ultra light funcitons.
 

DonManfred

Expert
Licensed User
Longtime User
Btw I'm running B4A 3.85

So you have a newer than me. Where do i get this version? :)

PS:
b4aversion003.png
 

chfajardo

Member
Licensed User
Longtime User
Thank you for the code!
Can you tell me how to set the key to "AAAAAAAAAAAA" instead of "FFFFFFFFFFFF".
Mifare Classic uses a 6 byte key, and in your example there is a 16 byte key.

Thank you in advance,

Carlos H. Fajardo.

The attachments are the new version of NFC library and a sample application. I added some extra java code to the library by Sherlok and Erel.

Here's how to use the lib:
1.initialize a "MiFare" object.
2.Call ReadSector() method with 3 parameters: SectorIndex, KeyData, KeyType
3.If succeeded, retrieve the data block by block with method GetBlockData()

B4X:
Sub Read()
   Dim key(16) As Byte
   Dim block_data() As Byte
   Dim bc As ByteConverter
  
   Dim i As Int
   For i  = 0 To 15
      key(i) = 255 '0xff
   Next

   'btnClear
   Dim mfc As MiFare
   Log("Check if the card is MF classic")
    If NFC.IsMifareClassic(Activity.GetStartingIntent) Then
      Dim StrData As String
      mfc.Initialize(Activity.GetStartingIntent)
      If True = mfc.ReadSector(0,key,0) Then' Read sector 0, key, type 0(as keyA)     
         Log("Read sector done")
         For i = 0 To 2 'read 3 blocks of data
            block_data = mfc.GetBlockData(i)
            StrData =  bc.HexFromBytes(block_data)
            Log( "data:" & StrData)
            If i = 0 Then
               Label1.Text = StrData
            Else If i = 1 Then
               Label2.Text = StrData
            Else If i = 2 Then
               Label3.Text = StrData
            End If
         Next
      End If
   End If
End Sub

In manifest editor, I added "android.nfc.action.NDEF_DISCOVERED" and "android.nfc.action.TAG_DISCOVERED" intent filter. Actually "android.nfc.action.TECH_DISCOVERED" is better than NDEF_DISCOVERED but it needs some code like:
B4X:
  <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
          android:resource="@xml/filter_nfc" />

I don't know how to put the "filter_nfc.xml" under "res/xml" directory. Maybe Erel knows.

Good luck
 

sasidhar

Active Member
Licensed User
Longtime User
Hello again. I solved this problem myself and updated the NFC lib so I can now read the UID from Mifare Classic and Mifare Ultralite tags on the Nexus 7 (2013) with Broacom NFC chip.

Cheers


Hi
Can you share the library which can read UID from mifare classic tag. I have downloaded NFC2.2 along with some sample code given, with that i could not able to..
I have nexus-5 mobile.

Thanks
Sasidhar
 

sasidhar

Active Member
Licensed User
Longtime User
Hi
Can you share the library which can read UID from mifare classic tag. I have downloaded NFC2.2 along with some sample code given, with that i could not able to..
I have nexus-5 mobile.

Thanks
Sasidhar

Hi,

I have downloaded NFC NFC 1.25..

If NFC.IsMifareClassic(Activity.GetStartingIntent) Then is not going inside. I am using Nexux5, but thru other google play apps i could able to read UID..
Please let me know how to solve this issue.

Thanks
Sasidhar.M
 

sasidhar

Active Member
Licensed User
Longtime User
Sorry for the error and delay. It was in rush because I was busy on other project.

I've updated the code. Meanwhile, fixed a couple of bugs in Lib.


Hi,

I have Issue with the code Mifare reader which is downloaded, I am using Nexus-5 and NFC library ver 1.25.

When i tried to read Mifare Classic tag I get no results.
I tried debugging,

NFC.IsNdefIntent(Activity.GetStartingIntent) returns false

NFC.IsMifareClassic(Activity.GetStartingIntent) returns false.


I have followed all the steps mentioned, Adding intent in manifest editor as well. Still there is no response.

Other applications from google play are able to read tag from same mobile. I hope there may be a small mistake I may be doing. Pls correct.

Thanks
Sasidhar.M
 

JCO

Active Member
Licensed User
Longtime User
Hi JJCC,

Many thanks for this library. It works just perfect for what I need.
However, in siome devices, I get a toast message saying "NFC tag type not supported", but I do get the reading and can use the ID info.
Is it possible to prevent the lib from showing the toast message ?

Thanks again,

Julio

UPDATE: After further research, it is not the library, but the device (Galaxy S4) which shows the message. Funny; since I do get the reading --
 
Last edited:

abcroverix

Member
Licensed User
Longtime User
Here's NFC1.25. It added functions for accessing Ultralight/NTag203 tags. Along with an sample B4A code showing how to use the ReadPage function.

Hi jjcc, i tried to write a mifare tag, but I don't understand the DataBlock byte array paramater. WriteSector(Sector, Key, Type, DataBlock(,))
The compiler seems to need a bidimensional array, but the ReadSector needs a monodimesional array.

How I should use the WriteSector command?

Another question: what the SetBlockData does?

Many thanks!

Fabio
 

masmukti

Member
Licensed User
Longtime User
Hello again. I solved this problem myself and updated the NFC lib so I can now read the UID from Mifare Classic and Mifare Ultralite tags on the Nexus 7 (2013) with Broacom NFC chip.

Cheers
Hi,
Could you share your code which can read read UID with Broadcom NFC chip ?
 

phukol

Active Member
Licensed User
Longtime User
Just want to ask it is possible to read the NFC tag without having to resume my activity using b4A? just like other NFC reading application which does not need to "refresh" everytime
 

davidneal

New Member
Licensed User
Hi Erel,
I'm rookie for B4A. I try NfcVReader code than get below error information.
Could you know how to fix issue? i don't have any idea. The NfcVreader form Jjcc as attaached.
Thank you for your help.

B4X:
main_read (java line: 469)
java.lang.ArrayIndexOutOfBoundsException: length=14; index=14
    at com.nfc.common.NfcvData.DecodeGetSystemInfoResponse(NfcvData.java:307)
    at com.nfc.common.NfcvData.getSystemInfo(NfcvData.java:338)
    at anywheresoftware.b4a.objects.NFC$NfcvWrapper.getSystemInfo(NFC.java:528)
    at co.micromacro.nfcv.main._read(main.java:469)
    at co.micromacro.nfcv.main._activity_resume(main.java:350)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at co.micromacro.nfcv.main$ResumeMessage.run(main.java:298)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:155)
    at android.app.ActivityThread.main(ActivityThread.java:5727)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
java.lang.ArrayIndexOutOfBoundsException: length=14; index=14
** Activity (main) Pause, UserClosed = false **
Pause called...
** Activity (main) Resume **
Check if the card is NfcV classic
Checking Nfcv inside IsNfcv
Intent is not null
tech:android.nfc.tech.NfcV
return from IsNfcv()
Yes it's Nfcv
Nfcv data initialized
SENDED Frame : 26  01  00
Inventory UID analysis
boolDeviceDetected true
return, response[0]== 0x00
main_read (java line: 469)
java.lang.ArrayIndexOutOfBoundsException: length=14; index=14
    at com.nfc.common.NfcvData.DecodeGetSystemInfoResponse(NfcvData.java:307)
    at com.nfc.common.NfcvData.getSystemInfo(NfcvData.java:338)
    at anywheresoftware.b4a.objects.NFC$NfcvWrapper.getSystemInfo(NFC.java:528)
    at co.micromacro.nfcv.main._read(main.java:469)
    at co.micromacro.nfcv.main._activity_resume(main.java:350)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at co.micromacro.nfcv.main$ResumeMessage.run(main.java:298)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:155)
    at android.app.ActivityThread.main(ActivityThread.java:5727)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
java.lang.ArrayIndexOutOfBoundsException: length=14; index=14
 

Attachments

  • NfcVReader.zip
    3.8 KB · Views: 492

fabpalma

Member
Licensed User
Longtime User
Chelu,
The function you want is card simulation as opposed to reader which is a quite new function. I believe Android 4.4 supports the function.
The current lib can not meet your requirement.

Starting from Android 4.4 there is this possibility.
Here there is the code (I don't know how to do a library from this Java code....)

http://developer.android.com/samples/CardEmulation/index.html

if anyone is able to import the Java code into this library, it would be fantastic !!
For example, we can use the Sony Smartwatch as NFC tag ....

Regards.
Fab
 

jjcc

Member
Licensed User
Longtime User
Starting from Android 4.4 there is this possibility.
Here there is the code (I don't know how to do a library from this Java code....)

http://developer.android.com/samples/CardEmulation/index.html

if anyone is able to import the Java code into this library, it would be fantastic !!
For example, we can use the Sony Smartwatch as NFC tag ....

Regards.
Fab
Fab,
I've looked at the sample code following your link. I think it's possible to build a wrapper lib for B4A.

However, HCE mode is quite different from the reader mode in that to develop a HCE application most works are related to implementing the APUD commands which need the knowledge of ISO7816, a standard for smart card communication protocol. In addition to the knowledge, the developer needs to understand how the the simulated smart card application respond to the reader. That's usually impossible unless the vendor of the card gives the tech details of the simulated card application.

The sample source code of your link only gives a demo of very simple 7816 command called "Select AID(AID means Application ID)", no further commands. Google just displayed that there's a possibility for Banks/Other Smart Card Vendors to simulate a smart card using an Android phone. For average Android developers it's not as useful as NFC function under reader mode.

More information from an expert of NFC on StackOverflow.com would be helpful.
http://stackoverflow.com/users/2425802/michael-roland

jjcc
 
Last edited:
Top