#Region  Project Attributes
    #ApplicationLabel: Advanced NFC
    #VersionCode: 2
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
'        5=5h
'        -21=ebh
'        35=23h
'        -71=b9h
'
'        05
'        eb
'        23
'        b9
Sub Process_Globals
    Private prevIntent As Intent
    Dim jTagTech As JavaObject   
    Dim tag As JavaObject
    Dim Id0() As  Byte
    Dim Id(8) As  String
    'Dim key(6) As Byte = Array As Byte(0xA0,0xA1,0xA2,0xA3,0xA4,0xA5)  ' Fake key for the forum
    Dim key() As Byte = Array As Byte(0xA0,0xA1,0xA2,0xA3,0xA4,0xA5)
    Dim key() As Byte = Array As Byte(0xFF,0xFF,0xFF,0xFF,0xFF,0xFF)
    Dim sector As Int=0
            
End Sub
Sub Globals
    Private nfc As NFC
    Private TagTech As TagTechnology
    
    Private rdbRead As RadioButton
    Private ListView1 As ListView
    Private rdbWrite As RadioButton
    Private flTxt1 As FloatLabeledEditText
    Private flTxt2 As FloatLabeledEditText
End Sub
Sub Activity_Create(FirstTime As Boolean)
    
    Activity.LoadLayout("1")
    ListView1.SingleLineLayout.Label.TextSize = 15
    ListView1.SetColorAnimated(Colors.Red,Colors.Blue,Colors.Black)
    flTxt2.EditText.ForceDoneButton = True
End Sub
Sub Activity_Resume 'ok
    Try
        
    'forces all nfc intents to be sent to this activity
    nfc.EnableForegroundDispatch
    Dim si As Intent = Activity.GetStartingIntent
    'check that the intent is a new intent
    If si.IsInitialized = False Or si = prevIntent Then Return
    prevIntent = si
    If si.Action.EndsWith("TECH_DISCOVERED") Or si.Action.EndsWith("NDEF_DISCOVERED") Or si.Action.EndsWith("TAG_DISCOVERED") Then
        Dim techs As List = nfc.GetTechList(si)
        Log($"Techs: ${techs}"$)
        
 
            If techs.IndexOf("android.nfc.tech.MifareClassic")> -1 Then
                TagTech.Initialize("TagTech", "android.nfc.tech.MifareClassic", si)
                TagTech.Connect
                jTagTech  = TagTech
                tag   = jTagTech.RunMethod("getTag", Null)
                  Id0   = tag.RunMethod("getId", Null)
                    If Id0(0)>0 Or Id0(1)>0 Or Id0(2)>0 Or Id0(3)>0  Then
                Id(0)=    Right("0" & Bit.ToHexString(ToUnsigned(Id0(0))),2)
                Id(1)=    Right("0" & Bit.ToHexString(ToUnsigned(Id0(1))),2)
                Id(2)=    Right("0" & Bit.ToHexString(ToUnsigned(Id0(2))),2)
                Id(3)=    Right("0" & Bit.ToHexString(ToUnsigned(Id0(3))),2)
                
                Log(Id(0)&" "&Id(1)&" "&Id(2)&" "&Id(3))
                
                ListView1.AddSingleLine(Id(0)&" "&Id(1)&" "&Id(2)&" "&Id(3))
            End If         
            
            Else
                ToastMessageShow("Tag does not support Ndef.", True)
            End If
    End If
    Catch
        Log(LastException)
'        Dim in As Intent
'        in.Initialize("android.settings.NFC_SETTINGS", "")
'        StartActivity(in)
    
    End Try
End Sub
Private Sub TagTech_Connected (Success As Boolean)
    Log($"Connected: ${Success}"$)
    If Success = False Then
        
        ToastMessageShow("Error connecting to tag", True)
        Log(LastException)
    Else
        TagTech.RunAsync("Authenticate", "authenticateSectorWithKeyB", Array( sector, key ), 0)
  
    End If
End Sub
 
Private Sub Authenticate_RunAsync (Flag As Int, Success As Boolean, Result As Object)
    Log($"Finished trying to auth. Success=${Success}, Flag=${Flag}"$)
    If Success Then
        Dim blockIndex As Int=1
        TagTech.RunAsync("readBlock","readBlock", Array(blockIndex), 0)
    End If
End Sub
Private Sub readBlock_RunAsync(Flag As Int, Success As Boolean, Result As Object)
    Log($"Finished trying to read. Success=${Success}, Flag=${Flag}"$)
    Log(LastException)
    If Success Then
        If Result<>Null Then
            Dim myBlock() As Byte = Result
            ListView1.AddSingleLine(">:"&Result)
        End If
    Else
        ListView1.AddSingleLine("cannot read")
    End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
    nfc.DisableForegroundDispatch
End Sub
Sub ToUnsigned(b As Byte) As Int
    Dim x As String
    x= Bit.And(0xFF, b)
    Return x
End Sub
Sub Right(Text As String, Length As Int) As String
    If Length>Text.Length Then Length=Text.Length
    Return Text.SubString(Text.Length-Length)
End Sub
'check: