Question

My activity is implementing

CreateNdefMessageCallback, OnNdefPushCompleteCallback

I'm calling

    mNfcAdapter.setNdefPushMessageCallback(this, this);
    // Register callback to listen for message-sent success
    mNfcAdapter.setOnNdefPushCompleteCallback(this, this);

I've overriden

@Override
public NdefMessage createNdefMessage(NfcEvent event) {
    Log.d(TAG, "Creating massage");
    String text = TAG + DATA_SPLITTER + ADDRESS + DATA_SPLITTER
            + USER_NAME;

    // Nachricht vorbereiten.
    // String text = "Hello NFC World!";
    NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
            MIME_TYPE.getBytes(), new byte[0], text.getBytes());
    NdefRecord[] records = { record };
    NdefMessage msg = new NdefMessage(records);
    return msg;
}

But when I'm receiving, I get different records, two of them, one with the market uri to search for the application, one with just the java package name (I guess). Before this, I've simply used enableForegroundNdefPush... etc, so without any Callback, but as I need it to handle the completion of my push on both devices.. I've changed it to this way, and now it does not properly transfer my data anymore.

EDIT:

To make it better understandable:

public class NfcActivity extends Activity implements CreateNdefMessageCallback,
    OnNdefPushCompleteCallback {
// ================================ Member =================================
// Speichert den NFC Adapter.
private NfcAdapter mNfcAdapter = null;
private boolean retryChannel = false;

public static final String TAG = "NfcActivity";
private static final String DATA_SPLITTER = "__:DATA:__";
private static final String MIME_TYPE = "application/my.applications.mimetype";

private String ADDRESS = null;
private String USER_NAME = null;

// Speichert das Intent, welches von Android mit erkannten Tags gefüllt
// und an die Activity weitergeleitet wird.
private PendingIntent mNfcPendingIntent = null;

private static final String LOG_TAG = "NfcActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ADDRESS = "Something";

    USER_NAME = "Someone";

    // Zugriff auf den NFC Adapter erhalten.
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

    mNfcAdapter.setNdefPushMessageCallback(this, this);
    mNfcAdapter.setOnNdefPushCompleteCallback(this, this);

    Intent intent = new Intent(this, getClass());
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    mNfcPendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
}

@Override
protected void onNewIntent(Intent intent) {
    if (!NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        return;
    }

    // Nutzdaten des Intent auslesen.
    Parcelable[] rawMsgs = intent
            .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    if (rawMsgs == null) {
        return;
    }

    for (int i = 0; i < rawMsgs.length; ++i) {
        NdefMessage msg = (NdefMessage) rawMsgs[i];
        NdefRecord[] records = msg.getRecords();
        for (NdefRecord record : records) {
            String text = new String(record.getPayload());
                            // this one has the wrong content. I receive two records, one for the market, one with java packet name (or something)
        }
    }
}

@Override
protected void onResume() {
    super.onResume();

            // activate receiving of messages
    mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, null,
            null);
}

@Override
protected void onPause() {
    super.onPause();
    // deactivate receiving
    mNfcAdapter.disableForegroundDispatch(this);
}


private void logError(String msg) {
    Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    Log.v(LOG_TAG, msg);
}

@Override
public void onNdefPushComplete(NfcEvent arg0) {
    Log.d(TAG, "Push complete");
}

@Override
public NdefMessage createNdefMessage(NfcEvent event) {
    Log.d(TAG, "Creating massage");

            String text = "asdfgh";

    // Nachricht vorbereiten.
    // String text = "Hello NFC World!";
    NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
            MIME_TYPE.getBytes(), new byte[0], text.getBytes());
    NdefRecord[] records = { record };
    NdefMessage msg = new NdefMessage(records);
    return msg;
}

}
Was it helpful?

Solution

I post this Answer for you, that you can accept it.

The solution was the wrong Mime-Type inside Android-Manifest. It must be the same as the NdefRecord defines

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top