【问题标题】:Issue in writing NDEF message to NFC tag将 NDEF 消息写入 NFC 标签的问题
【发布时间】:2015-07-17 23:27:28
【问题描述】:

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

感谢您为解决此问题提供的任何帮助。

【问题讨论】:

    标签: android nfc ndef


    【解决方案1】:

    检查您的清单文件以查看

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

    【讨论】:

      猜你喜欢
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-04
      • 1970-01-01
      相关资源
      最近更新 更多