Android Question Problem of NFC for Mifare Ultralight.

dragonguy

Active Member
Licensed User
Longtime User
i have a problem to read Mifare Utralight card(ntag203), how can i read the data from the card?
i using NFC libraries 1.23.

here is my code:

B4X:
Sub Process_Globals
    Dim Obj_Phone2 As Phone
      Dim AndroidSdk10 As Boolean : AndroidSdk10 =False
      If Obj_Phone2.SdkVersion >= 10 Then
      AndroidSdk10 = True
      Dim NFC As NFC
      End If
End Sub

B4X:
Sub Activity_Resume
    Read
End Sub

B4X:
Sub Read()
    If AndroidSdk10 = True Then
        If NFC.IsNdefIntent(Activity.GetStartingIntent) Then
            Log("Read successful.")
        Else
            Log("Failed")
        End If
    End If
    Else
        Msgbox("Please enable the NFC function","NFC Error")
        Dim DoAction As Intent
          DoAction.Initialize("android.settings.NFC_SETTINGS", "")
        StartActivity(DoAction)
    End If
End Sub

manifest:
B4X:
AddActivityText(Main, <intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="*/*" />
</intent-filter>)

Every time device read the card show log failed. please help me.
Thanks!
 

dragonguy

Active Member
Licensed User
Longtime User
anything I can do? I check website info

NFC Forum Tag 2 Type compliance
NTAG203 IC provides full compliance to the NFC Forum Tag 2 Type technical specification and enables NDEF data structure configurations.

Is it this card is ndef type?
 
Upvote 0

jlerner

Member
Licensed User
Longtime User
"""""""""HI!!.. im having then error "Could not initialize MifareUltralight" ?? ..can you helpme?"""""""""




may you post ur code here, so we can look for error part.

Here is my code..... (thanks!)



Sub Read()
Dim page_data() As Byte
Dim bc As ByteConverter
Dim startPage As Int

'get start block
startPage = edtPageIndex.Text


'btnClear
Log("Check if the card is Mifare ultralight")
Log( NFC.IsNdefIntent(Activity.GetStartingIntent))

If NFC.IsMifareClassic (Activity.GetStartingIntent) Then
Dim ulTag As MifareUltralight
ulTag.Initialize(Activity.GetStartingIntent )
Dim strData As String
Dim strType As String
ulTag.Initialize(Activity.GetStartingIntent)

'If( ulTag.Type = 1 ) Then
' strType = "Ultralight"
' Else If (ulTag.Type = 2) Then'
' strType = "UltralightC/NT'AG203"
'Else
' strType = "compatible"
' End If

lblUidValue.Text = strType
ulTag.Connect
page_data = ulTag.ReadPage(startPage )
strData = ""
If (page_data <> Null) Then
strData = bc.HexFromBytes(page_data)
End If

Edit1.Text = strData

End If
 
Upvote 0

dragonguy

Active Member
Licensed User
Longtime User
my app only read the MifareUltralight and i use NFC libraries version is v1.22
i share my here
B4X:
Sub Read()
    If NFC1.IsNdefIntent(Activity.GetStartingIntent) Then
                Log("Read successful.")
                Dim records As List
                Dim str_data As String=""
                  records = NFC1.GetNdefRecords(Activity.GetStartingIntent)
                  For i = 0 To records.Size - 1
                     Dim r As NdefRecord
                     r = records.Get(i)
                     Log(r.GetAsTextType)
                     str_data=r.GetAsTextType
                  Next
            End If   
End Sub

hope can help you :)
 
Upvote 0

jlerner

Member
Licensed User
Longtime User
are you need to read the MifareUltralight or MifareClassic? and what NFC libraries version you use?

im using then lib : NFC2 (Version 1.22) , but i try with NFC123 and NFC (Version 1.0).... the error is in then line :"page_data = ulTag.ReadPage(startPage ) " ... I need Read a MifareClassic tag... thanks!
 
Upvote 0

dragonguy

Active Member
Licensed User
Longtime User
here is my read MifareClassic card coding. using nfc lib v1.22
B4X:
Sub Read()
    Dim key(16) As Byte
    Dim block_data() As Byte
    Dim bc As ByteConverter
    Dim sector As Int
   
    'prepare key
    Dim i As Int
    For i  = 0 To 15
        key(i) = 255 '0xff
    Next
   
    'get sector index
    sector = SectorIndex
   
    Dim mfc As MiFare
    NfcF.EnableForeground
    Log("Check if the card is MF classic")
    If AndroidSdk10 = True Then
    If NfcF.NfcEnabled Then
    If NFC1.IsMifareClassic(Activity.GetStartingIntent) Then
        Dim StrData As String
        mfc.Initialize(Activity.GetStartingIntent)
        If True = mfc.ReadSector(sector,key,0) Then' Read sector 0, key, type 0(as keyA)   
            For i = 0 To 2 'read 3 blocks of data
                block_data = mfc.GetBlockData(i)
                StrData = StrData &  bc.StringFromBytes(block_data,"ASCII")
            Next
        End If
    End If
    Else
        Msgbox("Please enable the NFC function","NFC Error")
        Dim DoAction As Intent
          DoAction.Initialize("android.settings.NFC_SETTINGS", "")
         StartActivity(DoAction)
    End If
    End If
End Sub
 
Upvote 0

jlerner

Member
Licensed User
Longtime User
here is my read MifareClassic card coding. using nfc lib v1.22
B4X:
Sub Read()
    Dim key(16) As Byte
    Dim block_data() As Byte
    Dim bc As ByteConverter
    Dim sector As Int
  
    'prepare key
    Dim i As Int
    For i  = 0 To 15
        key(i) = 255 '0xff
    Next
  
    'get sector index
    sector = SectorIndex
  
    Dim mfc As MiFare
    NfcF.EnableForeground
    Log("Check if the card is MF classic")
    If AndroidSdk10 = True Then
    If NfcF.NfcEnabled Then
    If NFC1.IsMifareClassic(Activity.GetStartingIntent) Then
        Dim StrData As String
        mfc.Initialize(Activity.GetStartingIntent)
        If True = mfc.ReadSector(sector,key,0) Then' Read sector 0, key, type 0(as keyA)  
            For i = 0 To 2 'read 3 blocks of data
                block_data = mfc.GetBlockData(i)
                StrData = StrData &  bc.StringFromBytes(block_data,"ASCII")
            Next
        End If
    End If
    Else
        Msgbox("Please enable the NFC function","NFC Error")
        Dim DoAction As Intent
          DoAction.Initialize("android.settings.NFC_SETTINGS", "")
         StartActivity(DoAction)
    End If
    End If
End Sub


THANKS!!! really thanks!.... one more favor ...(if you accept of course!) ... can you send me a full example please?....
 
Upvote 0
Top