0

我的活动正在实施

CreateNdefMessageCallback, OnNdefPushCompleteCallback

我打电话

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

我已经覆盖

@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;
}

但是当我收到时,我得到了不同的记录,其中两条,一条带有用于搜索应用程序的市场 uri,一条带有 java 包名称(我猜)。在此之前,我只是简单地使用了 enableForegroundNdefPush... 等,所以没有任何回调,但是因为我需要它来处理我在两个设备上的推送的完成。我已经将它更改为这种方式,现在它没有正确传输我的数据了。

编辑:

为了更好地理解:

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;
}

}
4

1 回答 1

1

我为您发布此答案,您可以接受。

解决方案是 Android-Manifest 中的错误 Mime-Type。它必须与 NdefRecord 定义的相同

于 2012-06-05T13:32:58.427 回答