为什么标签消息无法在NFC上正确显示?

时间:2019-05-12 23:18:33

标签: java android nfc

我正在尝试开发NFC android应用程序,但是出于任何原因我的标签文本都不正确。

首先,我将消息写在图像上(您好):

https://www.dropbox.com/s/wtymsw8ga2kjfeu/Screenshot_20190512-180454.png?dl=0

我写完消息后,另一部手机上显示的文字是(play.google.com/store/apps/details?id=nfctutorials.tutorial04&feature=beam):

https://www.dropbox.com/s/ncoy75cptg7khx1/Screenshot_20190512-180431.png?dl=0

我什至下载了Google Play商店应用,结果也是一样。

这是我的清单配置:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data  android:mimeType="text/plain" />
            </intent-filter>
        </activity>
    </application>

我对MainActivity类的意图:

 @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        if (intent.hasExtra(NfcAdapter.EXTRA_TAG)) {
            Toast.makeText(this, "NfcIntent!", Toast.LENGTH_SHORT).show();

            if(tglReadWrite.isChecked())
            {
                Parcelable[] parcelables = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

                if(parcelables != null && parcelables.length > 0)
                {
                    readTextFromMessage((NdefMessage) parcelables[0]);
                }else{
                    Toast.makeText(this, "No NDEF messages found!", Toast.LENGTH_SHORT).show();
                }

            }else{
                Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                NdefMessage ndefMessage = createNdefMessage(txtTagContent.getText()+"luis");

                writeNdefMessage(tag, ndefMessage);
            }

        }
    }

这种方法是我从记录中获取消息的方式:

private void readTextFromMessage(NdefMessage ndefMessage) {

        NdefRecord[] ndefRecords = ndefMessage.getRecords();

        if(ndefRecords != null && ndefRecords.length>0){

            NdefRecord ndefRecord = ndefRecords[0];

            String tagContent = getTextFromNdefRecord(ndefRecord);

            txtTagContent.setText(tagContent);
            Toast.makeText(this, tagContent, Toast.LENGTH_SHORT).show();

        }else
        {
            Toast.makeText(this, "No NDEF records found!", Toast.LENGTH_SHORT).show();
        }

    }

我在做什么错?感谢您的任何建议!

0 个答案:

没有答案