read tag UID of a classic mifare 1k or NFC

abcroverix

Member
Licensed User
Longtime User
Hello, I'm new in java and android programming.
I need to read the classic mifare 1k or NFC UID
I tried to use the NFC.GeTagtUID from NFC library 1.1 (posted by Sherlock)
I get an array of 4 bytes. First two bytes are the same to the first two byte of the UID (I can read the UID with another device), but the last two bytes are negative numbers and are wrong.

This is the piece of my code:

If NFC.IsNdefIntent(Activity.GetStartingIntent) Then
Dim XID(20) As Byte
XID=NFC.GeTagtUID(Activity.GetStartingIntent)
end if

For example the last two bytes I get are (in decimal): -54,-73,
but the last two bytes in the UID are (in hex): CA, B7.

Someone can explain to me because this happen?
Where I'm wrong?

Many thanks

Fabio
 

Lectos

Member
Licensed User
Of course!

Be aware that only a few smartphones are able to read Mifare Cards using NFC, but it is worth a try!

This is the code for reading the NFC UID:

Put this in Process_Globals:

Private nfcIntent As Intent

Sub ConvertToUnsigned(b As Byte) As Int
Return Bit.And(b, 0xFF)
End Sub

Sub read_NFC_UID(nfcIntent1 As Intent) As String
Dim XID(20) As Byte
Dim i As Int
Dim bc As ByteConverter
Dim NFC_UID As String
NFC_UID = ""
If NFC.IsNdefIntent(nfcIntent1) Then
XID=NFC.GeTagtUID(nfcIntent1)
For i = 0 To NFC.GeTagtUID(nfcIntent1).Length-1
XID(i)= ConvertToUnsigned(XID(i))
Next
NFC_UID = bc.HexFromBytes(XID)
End If
Return NFC_UID
End Sub

In the Activity_Resume, put this:

nfcIntent = Activity.GetStartingIntent
NFC_UID = read_NFC_UID(nfcIntent)

I am using these libraries:
byteconverter, core, mul, nfc, nfcforeground and rsnfctagwriter
But i think you can do it with only:
byteconverter, core, nfc, nfcforeground

Also, put this on manifest editor:

AddActivityText(main, <intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</intent-filter>)
AddActivityText(main, <intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>)

I hope it helps you!

Thank you very much @chfajardo

I'll give it a try and tell you.

Best regards.
 
Upvote 0

Lectos

Member
Licensed User
Of course!

Be aware that only a few smartphones are able to read Mifare Cards using NFC, but it is worth a try!

This is the code for reading the NFC UID:

Put this in Process_Globals:

Private nfcIntent As Intent

Sub ConvertToUnsigned(b As Byte) As Int
Return Bit.And(b, 0xFF)
End Sub

Sub read_NFC_UID(nfcIntent1 As Intent) As String
Dim XID(20) As Byte
Dim i As Int
Dim bc As ByteConverter
Dim NFC_UID As String
NFC_UID = ""
If NFC.IsNdefIntent(nfcIntent1) Then
XID=NFC.GeTagtUID(nfcIntent1)
For i = 0 To NFC.GeTagtUID(nfcIntent1).Length-1
XID(i)= ConvertToUnsigned(XID(i))
Next
NFC_UID = bc.HexFromBytes(XID)
End If
Return NFC_UID
End Sub

In the Activity_Resume, put this:

nfcIntent = Activity.GetStartingIntent
NFC_UID = read_NFC_UID(nfcIntent)

I am using these libraries:
byteconverter, core, mul, nfc, nfcforeground and rsnfctagwriter
But i think you can do it with only:
byteconverter, core, nfc, nfcforeground

Also, put this on manifest editor:

AddActivityText(main, <intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</intent-filter>)
AddActivityText(main, <intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>)

I hope it helps you!

Hello @chfajardo

I've tried your example and is working, but I had to add the lines below to the manifest:

AddActivityText(Main, <intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>)

Is the library nfcforeground needed to run the app? I can't see any reference in the code.

On the other hand, I have a problem with the length of UID readed. The function is reading 4 bytes only but I need to read 7. Is there any possibility to change this?

Thanks and best regards.
 
Upvote 0

chfajardo

Member
Licensed User
Longtime User
I am glad it is working for you!
I use NFC foreground to do all NFC the operations in the foreground, and not in the background. This way, your screen will not turn black during the NFC read, for instance.
For the UID, I am converting it to HEX (bc.HexFromBytes(XID)), but you can use it as bytes or string (bc.StringFromBytes(XID)) or, use the XID array of bytes.
Thank you for the feedback!
 
Upvote 0
Top