将NDEF消息写入NFC标签

时间:2015-07-17 23:27:28

标签: android nfc ndef

我正在尝试将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);
    }

我感谢任何帮助,以解决这个问题。

1 个答案:

答案 0 :(得分:0)

检查清单文件以查看

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

<强>机器人:alwaysRetainTaskState = “真”

(我使用以下方法,它的工作原理) 如果没问题,那么尝试在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);
    }
}