NFC Android App acting differently on different ph

2019-08-08 10:52发布

问题:

I have currently been testing some examples being found on the internet on sending NDEF messages through the NFC on my android phone.

I have three phones that I tested the phone with: Samsung Galaxy Nexus (Android 4.4.4), S3 (Android 4.4.2) and S4 (Android 4.4.4).

The app works perfect the way I wanted it to on the GN (it sends the message), however on the S3 and the S4 it sends the package name of the app instead of the message.

Can anybody help me with this? Does anyone know why or how to fix this? I am pretty new to Android dev and don't fully understand why it is doing this.

Code:

package tapit.cbstech.com.tap_it_3;

import android.app.Activity;
import android.nfc.NfcAdapter;
import android.os.Bundle;


import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcEvent;
import android.os.Parcelable;
import android.widget.TextView;
import android.widget.Toast;


public class main extends Activity implements CreateNdefMessageCallback {
    NfcAdapter mNfcAdapter;
    TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = (TextView) findViewById(R.id.textView);
        // Check for available NFC Adapter
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter == null) {
            Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
        // Register callback
        mNfcAdapter.setNdefPushMessageCallback(this, this);
    }

    @Override
    public NdefMessage createNdefMessage(NfcEvent event) {
        String text = ("abcdefghi");
        NdefMessage msg = new NdefMessage(
                new NdefRecord[] { NdefRecord.createMime("text/plain", text.getBytes()),
                        /**
                         * The Android Application Record (AAR) is commented out. When a device
                         * receives a push with an AAR in it, the application specified in the AAR
                         * is guaranteed to run. The AAR overrides the tag dispatch system.
                         * You can add it back in to guarantee that this
                         * activity starts when receiving a beamed message. For now, this code
                         * uses the tag dispatch system.
                         */
                        //NdefRecord.createApplicationRecord("hello test")
                });
        return msg;
    }

    @Override
    public void onResume() {
        super.onResume();
        // Check to see that the Activity started due to an Android Beam
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
            processIntent(getIntent());
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        // onResume gets called after this to handle the intent
        setIntent(intent);
    }

    /**
     * Parses the NDEF Message from the intent and prints to the TextView
     */
    void processIntent(Intent intent) {
        textView = (TextView) findViewById(R.id.textView);
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
                NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage msg = (NdefMessage) rawMsgs[0];
        textView.setText(new String(msg.getRecords()[0].getPayload()));
    }
}

On the GNexus I get the "abcdefghi" but on the S3 and S4 I get "tapit.cbstech.com.tap_it_3"

Any help is appreciated! Thanks in advance!

Edit: Tested on a friends S3 and does the same (sending the package name) and tested on another friends nexus 5 (running Android L) and it worked sending the "abcdefghi" message.