NFC Android App在不同手机上的表现不同

时间:2014-11-21 13:13:10

标签: android nfc nfc-p2p android-beam

我目前正在测试互联网上通过我的Android手机上通过NFC发送NDEF消息的一些示例。

我有三部手机用于测试手机:三星Galaxy Nexus(Android 4.4.4),S3(Android 4.4.2)和S4(Android 4.4.4)。

该应用程序在GN(它发送消息)上的工作方式非常完美,但是在S3和S4上它会发送应用程序的包名而不是消息。

有人可以帮我这个吗?有谁知道为什么或如何解决这个问题?我是Android开发人员的新手,并不完全理解为什么会这样做。

代码:

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

在GNexus上我得到了“abcdefghi”但在S3和S4上我得到了“tapit.cbstech.com.tap_it_3”

任何帮助表示赞赏!提前谢谢!

编辑:在朋友S3上测试并执行相同的操作(发送包名称)并在另一个朋友nexus 5(运行Android L)上进行测试,它发送了“abcdefghi”消息。

0 个答案:

没有答案
相关问题