Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 尝试将NDEF消息写入NFC标记时,标记返回null-未检测到标记_Android_Android Intent_Tags_Nfc_Ndef - Fatal编程技术网

Android 尝试将NDEF消息写入NFC标记时,标记返回null-未检测到标记

Android 尝试将NDEF消息写入NFC标记时,标记返回null-未检测到标记,android,android-intent,tags,nfc,ndef,Android,Android Intent,Tags,Nfc,Ndef,我正在尝试将NDEF消息写入标记。下面是我的代码。我在IDE中使用调试器运行代码,但在执行该行之后 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); tag为null(我假设这意味着没有找到标记),并且我无法写入NDEF消息。相反,我得到了一个NullPointerException public class MainActivity extends Activity { private BluetoothAdapte

我正在尝试将NDEF消息写入标记。下面是我的代码。我在IDE中使用调试器运行代码,但在执行该行之后

Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
tag
null
(我假设这意味着没有找到标记),并且我无法写入NDEF消息。相反,我得到了一个NullPointerException

public class MainActivity extends Activity {
    private BluetoothAdapter mBluetoothAdapter;
    private NfcAdapter mNfcAdapter;
    static final byte[] TYPE_BT_OOB = "application/vnd.bluetooth.ep.oob".
            getBytes(Charset.forName("US_ASCII"));
    private NdefMessage mNdefMessage;
    String mLocalBluetoothAddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        mNdefMessage = createHandoverRequestMessage();
        Intent intent = getIntent();
        //Intent intent = new Intent();

        String action = intent.getAction();
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);// I am getting only Null in tag here//
        try {
            Ndef ndef = Ndef.get(tag);
            ndef.connect();
            try {
                ndef.writeNdefMessage(mNdefMessage);
            } catch (FormatException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            ndef.close();
        } catch (IOException e) {
            Log.e("TagDispatch", e.toString());
        }
    }
这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.nfcdemov5"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk  android:minSdkVersion="16" />

    <uses-permission android:name="android.permission.NFC" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
        android:debuggable="true" >

        <activity
            android:name="com.example.nfcdemov5.MainActivity"
            android:label="@string/app_name" >

          <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="application/vnd.bluetooth.ep.oob" />
            </intent-filter>
        </activity>
    </application>
</manifest>

触摸NFC标签可触发NFC事件。因此,当您通过单击启动器图标(或从您的开发环境启动)打开应用程序时,ANdroid不会生成NFC事件(即使您的设备已放置在标签顶部)

因此,您需要做的是为NFC活动注册应用程序。您可以通过清单或应用程序在前台运行时,通过前台调度系统执行此操作。无论当前标签上有什么数据,您似乎都希望能够写入任何标签,因此我建议您坚持使用前台调度系统,不要在AndroidManifest.xml中注册任何NFC事件

您应该首先使用
onResume()
中的
enableForegroundDispatch()
方法注册以接收任何标记的事件:

此外,您必须在活动离开前台之前再次禁用前台调度(因此,在
onPause()
)中:

最后,您可以通过
onNewIntent()
方法处理标记。因此,您将将当前必须处理标记的代码从
onCreate()
移动到
onNewIntent()


您可以通过在单独的线程上执行实际写入来进一步完善这一点,因为阻止IO操作不应该在UI线程上执行,因为它们可能会导致您的应用程序冻结。

标签上有什么数据(以前)?您是通过点击标签开始活动,还是您的活动已经在前台可见?您是否使用
启用ForegroundDispatch()
?如果是,请显示相关代码(来自
onResume()
onPause()
onNewIntent()
)。感谢Michael的回复。标记没有任何数据。我唯一的目标是将NDEF写入标记中。抱歉-我对应用程序开发和NFC都是新手。好吧,那你现在到底在做什么?是否手动启动应用程序(通过单击启动器图标)?或者当你触摸标签时,应用会启动吗?当你触摸标签时,应用程序会做什么?同样,这是您活动的全部代码还是其他代码?除了上面给出的代码外,我还有准备NDEF消息的代码-createHandoverRequestMessage(),它将与蓝牙设备配对。我无法共享代码,因为它太长了。我试图通过连接支持NFC的手机并将其放在我想要写NDEF消息的标签上来执行。它什么也没做。所以我试着在上使用调试器。在Tag(Tag)语句之后,一旦进入try部分,就会抛出nullexception错误。当我检查标记的值时,它显示为null。在intent.getaction()之后的操作是“android.intent.action.MAIN”。我不确定这是否有助于你去告诉我。我尝试了上述代码。我刚刚将“ACTION\u TAG\u DISCOVERED”更改为ACTION\u NDEF\u DISCOVERED,它将进入“if”循环。但是,当写入标记时,它抛出IO exception=>java.IO.IOException。在使用上述代码时,应该发现标记,而不是NDEF。如果您的手机与标签失去接触(例如,如果标签未正确放置在手机上或在写入时被移除),或者如果NDEF消息太大而无法放在标签上,则可能会发生IOException。抱歉。最初,我尝试使用一个包含一些内容的标记,因此它不是tag_发现并引发IO异常。现在我尝试使用一个新的标记,目的是发现标记,并且能够编写NDEF消息。谢谢罗兰的帮助。。现在我需要检查写在标记中的NDEF消息是否有效:)。非常感谢。@abdullahicyc请不要劫持其他问题。如果你有新问题,可以在这里提问:(另请参阅)
public void onResume() {
    super.onResume();
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}
public void onPause() {
    super.onPause();
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcAdapter.disableForegroundDispatch(this);
}
public void onNewIntent(Intent intent) {
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        if (tag != null) {
            Ndef ndef = Ndef.get(tag);
            if (ndef != null) {
                try {
                    ndef.connect();
                    ndef.writeNdefMessage(createHandoverRequestMessage());
                } catch (Exception e) {
                    Log.e("TagWriting", e.toString());
                } finally {
                    try {
                        ndef.close();
                    } catch (Exception e) {
                    }
                }   
            }
        }
    }
}