本文介绍了简单的NFC code不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一个开始编写使用NFC的应用程序。我只需要一部手机发送NDEF消息到另一个只包含一个简单的字符串。

I am trying to find a start writing an app using NFC. I simply need to send a NDef Message from one phone to another containing only a simple String.

我的发送活动:

public class MainActivity extends Activity {

    NfcAdapter mNfcAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter == null) {
            Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
        NdefMessage msg = new NdefMessage(
                new NdefRecord[] { createTextRecord("Bla", Locale.GERMANY , true )});
        mNfcAdapter.setNdefPushMessage(msg, this);
    }



    public NdefRecord createTextRecord(String payload, Locale locale, boolean encodeInUtf8) {
        byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));
        Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
        byte[] textBytes = payload.getBytes(utfEncoding);
        int utfBit = encodeInUtf8 ? 0 : (1 << 7);
        char status = (char) (utfBit + langBytes.length);
        byte[] data = new byte[1 + langBytes.length + textBytes.length];
        data[0] = (byte) status;
        System.arraycopy(langBytes, 0, data, 1, langBytes.length);
        System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);
        NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
        NdefRecord.RTD_TEXT, new byte[0], data);
        return record;
    }
   }

然后,我只是试着得到一些意图在手机的其他的东西happend:

Then I simply try to receive some Intent on the other phone that something happend:

public class MainActivity extends Activity {

    NfcAdapter mNfcAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter == null) {
            Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

            if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
                Toast.makeText(getApplicationContext(), "Received an Event onResume", Toast.LENGTH_LONG).show();
            }

        }

}

可悲的是没有任何反应,如果我一敲电话。感谢您的帮助!

Sadly nothing happens if I tap the phones. Thanks for any help!

推荐答案

在其网站上。你应该总是查找文档中的第一个。

On Their website. You should always look up in the documentation first.

本文档介绍了Android中执行基本的NFC任务。它  解释如何发送和接收的NFC数据在NDEF消息的形式  并介绍了支持这些功能的Andr​​oid框架的API。  对于更高级的主题,包括有工作的讨论  非NDEF数据,请参见高级NFC。

这篇关于简单的NFC code不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:32