I am developing an app with NFC. I was working with an example in which sending and receiving data was carried by the same Activity. Now I need to move sending to the other one and for some reason receing doesn't work anymore.
Here is my mainifest:
<uses-permission android:name="android.permission.NFC" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
<activity
android:name=".Screens.LogIn.LogInActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And the receiving activity:
public class LogInActivity extends AppCompatActivity {
public static final String TAG = LogInActivity.class.getSimpleName();
private String messagesToReceive= null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_in);
replaceFragment();
if (getIntent().getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
handleNfcIntent(getIntent());
}
}
private void replaceFragment() {
android.support.v4.app.FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
LogInFragment fragment = new LogInFragment();
ft.replace(R.id.fragmentFrame, fragment, LogInFragment.TAG);
ft.commit();
}
@Override
public void onResume() {
super.onResume();
handleNfcIntent(getIntent());
}
@Override
public void onNewIntent(Intent intent) {
handleNfcIntent(intent);
}
private void handleNfcIntent(Intent NfcIntent) {
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(NfcIntent.getAction())) {
Parcelable[] receivedArray =
NfcIntent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if(receivedArray != null) {
NdefMessage receivedMessage = (NdefMessage) receivedArray[0];
NdefRecord[] attachedRecords = receivedMessage.getRecords();
for (NdefRecord record:attachedRecords) {
String string = new String(record.getPayload());
if (string.equals(getPackageName())) { continue; }
messagesToReceive = string;
}
Toast.makeText(this, "Received " +
" Messages", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, "Received Blank Parcel", Toast.LENGTH_LONG).show();
}
}
}
The problem is that NfcIntent.getAction()
always equals android.intent.action.MAIN
even if the app opens when I send data with NFC. As you can see, there is action NDEF_DISCOVERED
in the manifest.
This is what I send:
public NdefRecord[] createRecords() {
NdefRecord[] records = new NdefRecord[2];
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
byte[] payload = messagesToSend.
getBytes(Charset.forName("UTF-8"));
NdefRecord record = new NdefRecord(
NdefRecord.TNF_WELL_KNOWN, //Our 3-bit Type name format
NdefRecord.RTD_TEXT, //Description of our payload
new byte[0], //The optional id for our Record
payload); //Our payload for the Record
records[0] = record;
}
else {
byte[] payload = messagesToSend.//messagesToSendArray.get(i).
getBytes(Charset.forName("UTF-8"));
NdefRecord record = NdefRecord.createMime("text/plain",payload);
records[0] = record;
}
records[1] = NdefRecord.createApplicationRecord(getActivity().getPackageName());
return records;
}
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
if (messagesToSend != null && !messagesToSend.equals("")) {
return null;
}
NdefRecord[] recordsToAttach = createRecords();
return new NdefMessage(recordsToAttach);
}