簡體   English   中英

將NDEF消息寫入NFC標簽時出現問題

[英]Issue in writing NDEF message to NFC tag

我正在嘗試將NDEF消息寫入NFC標簽。 我有在onNewIntent()中寫入NDEF消息的代碼。 但是控件不會轉到onNewIntent()。 在onResume()之后,它掛起了。 請在代碼下方找到。

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

感謝您為解決此問題提供的幫助。

檢查清單文件以查看

<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”

(我使用下面的方法,它可以工作)如果可以,請嘗試在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