简体   繁体   English

将NDEF消息写入NFC标签时出现问题

[英]Issue in writing NDEF message to NFC tag

I am trying to write NDEF message to NFC tag. 我正在尝试将NDEF消息写入NFC标签。 I have the code to write NDEF message in onNewIntent(). 我有在onNewIntent()中写入NDEF消息的代码。 But the control is not going to onNewIntent(). 但是控件不会转到onNewIntent()。 After onResume(), it is hanging. 在onResume()之后,它挂起了。 pls find below the code. 请在代码下方找到。

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);
        Toast.makeText(this, "inside oncreate", Toast.LENGTH_LONG).show();

        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter == null) {
            // Stop here, we definitely need NFC
            Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
            finish();
            return;

        }
    }
    public void onResume() {
        super.onResume();
        Toast.makeText(this, "inside OnResume", Toast.LENGTH_LONG).show();
        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 onNewIntent(Intent intent) {
        Toast.makeText(this, "inside onnewintent", Toast.LENGTH_LONG).show();

        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) {
                        }
                    }   
                }
            }

        }

    }
    public void onPause() {
        super.onPause();
        NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        nfcAdapter.disableForegroundDispatch(this);
    }

I appreciate any help to get this sorted out. 感谢您为解决此问题提供的帮助。

Check your manifest file to see 检查清单文件以查看

<activity
    android:name=".MainActivity"
    android:alwaysRetainTaskState="true"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:label="@string/app_name"
    android:launchMode="singleInstance"
    android:screenOrientation="nosensor" >

android:alwaysRetainTaskState="true" android:alwaysRetainTaskState =“ true”

(I use the following method and it works) If it is fine then try catching the pending intent at onCreate(). (我使用下面的方法,它可以工作)如果可以,请尝试在onCreate()上捕获挂起的意图。

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;

    private PendingIntent pendingIntent;
    String mLocalBluetoothAddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(this, "inside oncreate", Toast.LENGTH_LONG).show();

        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter == null) {
            // Stop here, we definitely need NFC
            Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
            finish();
            return;

        } else {
            pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
            onTagFound(getIntent());
        }
    }

    public void onResume() {
        super.onResume();
        Toast.makeText(this, "inside OnResume", Toast.LENGTH_LONG).show();
        NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (nfcAdapter != null && pendingIntent != null) {
            nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
        }
    }

    public void onNewIntent(Intent intent) {
        Toast.makeText(this, "inside onnewintent", Toast.LENGTH_LONG).show();
        onTagFound(intent);
    }

    public void onTagFound(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) {
                        }
                    }
                }
            }

        }
    }

    public void onPause() {
        super.onPause();
        NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        nfcAdapter.disableForegroundDispatch(this);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM