Przeniosłem w ten sposób, ale dalej nie ma reakcji. Swoją drogą bardzo dziękuję za pomoc i cierpliwość ?
package com.example.komputer007.skanernfc;
import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareUltralight;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private NfcAdapter nfcAdapter;
PendingIntent mPendingIntent;
byte[] tablica_odczytane01 = new byte[16];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
Log.d("intencje", "onCreate");
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
Toast.makeText(this, "Urządzenie nie wspiera NFC", Toast.LENGTH_LONG).show();
finish();
} else if (!nfcAdapter.isEnabled()) {
Toast.makeText(this, "NFC wyłączone, włącz NFC", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onResume() {
super.onResume();
nfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
}
@Override
protected void onPause() {
super.onPause();
nfcAdapter.disableForegroundDispatch(this);
}
@Override
protected void onNewIntent(Intent intent){
intent = getIntent();
String action = intent.getAction();
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
MifareUltralight ultralight = MifareUltralight.get(tag);
try {
ultralight.connect();
tablica_odczytane01=ultralight.readPages(4);
Log.d("odczytano","sukces!");
ultralight.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
dodana zawartość
Z tego co zaobserwowałem teraz jest tak, że:
1. Przykładam tag do smartfona i wteny wywoływana jest onPause() w której wywoływane jest
nfcAdapter.disableForegroundDispatch(this);
2. Nastepnie onNewIntent() w którym nie wchodzimy w
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)){...} dlaczego ten if nie jest spełniony?
3. na koncu OnResume() i
nfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);