Android Tutorial NFC - Reading and Writing

Status
Not open for further replies.

BluSky76

Member
Licensed User
Longtime User
Hello Erel,

you can format with a NFC v2.00 card without using external programs or libraries as RSNFCTagWriter.

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can the android.nfc.tech.NdefFormatable tech to format the tag: https://developer.android.com/reference/android/nfc/tech/NdefFormatable.html

Try to connect to the tag with:
B4X:
TagTech.Initialize("TagTech", "android.nfc.tech.NdefFormatable" , si)
TagTech.Connect
Dim RecordsJO As JavaObject
RecordsJO.InitializeArray("android.nfc.NdefRecord",Array(nfc.CreateUriRecord("https://www.b4x.com"))
Dim message As JavaObject
message.InitializeNewInstance("android.nfc.NdefMessage", Array(RecordsJO))
TagTech.RunAsync("Format", "format", Array(message), 0)
End Sub

Private Sub Format_RunAsync (Flag As Int, Success As Boolean, Result As Object)
   Log($"Writing completed. Success=${Success}, Flag=${Flag}"$)
End Sub
 

boon2

Member
Licensed User
Longtime User
Private TagTech As TagTechnology

where is lib > tagtechnology
b4a version.3.8
thx
 

MCU01

Member
Licensed User
Longtime User
Hi, I'm trying to combine this NFC code with the TabStripViewPager explained here.

My NFC program works great. The problem is that when I add the tabs with TabStripViewPager, my NFC communication stops working. My NFC program works great with TabHosts, but it doesn't like the TabStripViewPager. Am I missing something here? I'm just a beginner, so go easy on me....

Update: Never mind, I found what I was doing wrong. I was calling the first tab, tab# 1 instead of calling it tab# 0 in the following line of code, If tbsMain.CurrentPage = 1 Then
 
Last edited:

Cnrez

Member
Licensed User
Longtime User
hi all,
i want to read and write to mifare desfire 4k card
it support tech :
Techs: [android.nfc.tech.IsoDep, android.nfc.tech.NfcA, android.nfc.tech.NdefFormatable]

i want to use android.nfc.tech.NdefFormatable
i modified the sample project, but it didn't work,
here is the log :

B4X:
Logger connected to:  CipherLab CipherLab RS30
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Techs: [android.nfc.tech.IsoDep, android.nfc.tech.NfcA, android.nfc.tech.NdefFormatable]
Connected: true
Reading completed. Success=false, Flag=0
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Techs: [android.nfc.tech.IsoDep, android.nfc.tech.NfcA, android.nfc.tech.NdefFormatable]
Connected: true
Reading completed. Success=false, Flag=0

please help.
regards

here is my project
 

Attachments

  • advanced_NFC_ndefformatable.zip
    9 KB · Views: 695

Wolli013

Well-Known Member
Licensed User
Longtime User
Hi Erel

Thanks for it!
How can I make one day NFC only readably and according to demand again recordably?
 

MCU01

Member
Licensed User
Longtime User
Hi,

I've been using this code for a while to read NFC tags and it works great. I have an electronic design where I'm using an NXP chip with an antenna tuned properly. Sometimes the phone recognizes the tag and sometimes I get the message "Tag does not support Ndef." from the following code.

B4X:
    '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}"$)
        'in this case we are only accessing Ndef tags.
        If techs.IndexOf("android.nfc.tech.Ndef") > -1 Then
            TagTech.Initialize("TagTech", "android.nfc.tech.Ndef" , si)
            'Connect to the tag
            TagTech.Connect
        Else
            ToastMessageShow("Tag does not support Ndef.", True)  'CHECK WHY THIS MESSAGE SOMETIMES COMES UP....
        End If
    End If

The tags work find with other readers. Just in case that you want to take a look at the datasheet of this tag here is the datasheet,

https://www.mouser.com/datasheet/2/302/NT3H2111_2211-1127378.pdf

Below is a screenshot of the tag read using a different program. It shows that the tag is NdefFormatable instead of being Ndef enabled.



Any help is greatly appreciated. Please, let me know if I need to start a new thread instead of posting here.

Thank you,

Robert
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…