By using NFC technology in the sensor, we are using technology to retrieve water pollution levels via NFC communication via cell phone. With the existing logic, data is read through GetNdefMessage, and if it is incorrect, the type of type is written again through WriteNdefMessage and read again.
This time, Google told me to raise the target version to 33, so I tried using the NFC technology again on an Android 33 version phone, but it did not work on Android 13.
If you do GetNdefMessage after WriteNdefMessage, null comes out. I don't know why this is. From reading Google's official documentation, I understand that nothing related to NFC changed when going from 31 to 33. When you searched for the corresponding error, was it changed so that you cannot write to and read from NFC using ndef?
public class TestSensor extends AppCompatActivity {
private NfcAdapter nfcAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
}
catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onResume(){
super.onResume();
try {
if (nfcAdapter != null) {
Bundle options = new Bundle();
options.putInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY, 250);
nfcAdapter.enableReaderMode(this,new NfcReaderClass(), NfcAdapter.FLAG_READER_NFC_A |
NfcAdapter.FLAG_READER_NFC_B |
NfcAdapter.FLAG_READER_NFC_F |
NfcAdapter.FLAG_READER_NFC_V ,options);
}
}
catch (Exception e){
e.printStackTrace();
}
}
@Override
protected void onNewIntent(Intent intent){
super.onNewIntent(intent);
}
@Override
public void onPause(){
super.onPause();
try {
if (nfcAdapter != null) {
nfcAdapter.disableReaderMode(this);
}
}
catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onDestroy(){
super.onDestroy();
}
}
class NfcReaderClass implements NfcAdapter.ReaderCallback {
private Tag mTag = null;
private Ndef m_Ndef = null;
@Override
public void onTagDiscovered(Tag tag) {
try {
if (tag != null){
this.mTag = tag;
writeNfcNdef();
}
}
catch (Exception e){
e.printStackTrace();
}
}
public void writeNfcNdef(){
try {
this.m_Ndef = Ndef.get(this.mTag);
if (this.m_Ndef != null){
byte[] info2 = hexStringToByteArray("AA00");
NdefRecord record = RecordUtil.makeMimeTypeRecord("app/x-sensor", null, info2);
NdefMessage msg = new NdefMessage(new NdefRecord[]{record});
this.m_Ndef.connect();
this.m_Ndef.writeNdefMessage(msg);
this.m_Ndef.close();
this.m_Ndef = Ndef.get(this.mTag);
this.m_Ndef.connect();
NdefMessage msg1 = this.m_Ndef.getNdefMessage();
Log.e("msg","!!! "+ msg1);
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
I also referred to that article and this article to try to solve the problem.
After seeing an article saying that Write should not be done in onNewIntent, I tried Write in onResume. I also tried using OnReaderMode in OnResume and ActivateForegroundDispatch in onNewIntent to handle this.
I have the same problem with my app that reads data from a device using NFC, doing several consecutive read & write. On Android <=12 works perfectly, but on Android 13 after the first read & write, the next read will trigger the following error on logcat:
One article suggests doing
ndef.close()and connecting again for the next read, but this did not change anything for me.For now my workaround is to let the tag connecting close and wait for the phone to pick it again and continue the process. To make this happen without removing and positioning the phone constantly I am initializing the NFC interface with:
The result is a much slower interaction with my device but is better than nothing