在oncreate方法中获取NFC唯一ID

时间:2013-12-09 04:05:45

标签: android nfc uniqueidentifier oncreate

我正在尝试在on create方法中获取NFC标记的唯一ID。我已成功使用名为“onNewIntent”的覆盖方法完成此操作但是我想在onc​​reate方法中获取它。这是我试过的,启动时手机上的应用程序崩溃了。请任何人都可以帮忙。感谢。

public class MainActivity extends Activity {

    TextView uid;

    private final String[][] techList = new String[][] {
        new String[] {
            NfcA.class.getName(),
            NfcB.class.getName(),
            NfcF.class.getName(),
            NfcV.class.getName(),
            NdefFormatable.class.getName(),
            TagTechnology.class.getName(),
            IsoDep.class.getName(),
            MifareClassic.class.getName(),
            MifareUltralight.class.getName(), 
            Ndef.class.getName()
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        uid = (TextView) findViewById(R.id.UID);
        Intent i = new Intent();
        IntentFilter filter = new IntentFilter();
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
        NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[]{filter}, this.techList);
        if (i.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
            ((TextView)findViewById(R.id.UID)).setText(ByteArrayToHexString(i.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
        }
    }

    private String ByteArrayToHexString(byte [] inarray) { //converts byte arrays to string
        int i, j, in;
        String [] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
        String out= "";

        for(j = 0 ; j < inarray.length ; ++j) 
            {
            in = (int) inarray[j] & 0xff;
            i = (in >> 4) & 0x0f;
            out += hex[i];
            i = in & 0x0f;
            out += hex[i];
            }
        return out;
    }
}

2 个答案:

答案 0 :(得分:2)

  

好的,基本上我创造了一个新的意图。我从设备获得默认的NFC硬件。如果发现了标记,则应将textview更改为NFC标记的ID

要实现这一点,您只需设置新文字并在onNewIntent()中调用invalidate(),以便使用新文字重新绘制TextView:< / p>

package com.example.changetext;

import android.app.Activity;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView mNfcAdapterUid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNfcAdapterUid = (TextView)findViewById(R.id.nfc_adapter_uid);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
            mNfcAdapterUid.setText(ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
            mNfcAdapterUid.invalidate();
        }
    }

    private String ByteArrayToHexString(byte[] inarray) { // converts byte arrays to string
        int i, j, in;
        String[] hex = {
                "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"
        };
        String out = "";

        for (j = 0; j < inarray.length; ++j) {
            in = inarray[j] & 0xff;
            i = (in >> 4) & 0x0f;
            out += hex[i];
            i = in & 0x0f;
            out += hex[i];
        }
        return out;
    }
}

希望这有帮助。

答案 1 :(得分:0)

您似乎在这里混合了两个概念:在应用清单和前台调度中注册的NFC意图。

根据NFC事件自动启动应用

此机制要求您在应用清单中注册意图过滤器,例如

<activity ...>
    <intent-filter>
        <action android:name="android.nfc.action.TECH_DISCOVERED"/>
    </intent-filter>
    <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
               android:resource="@xml/nfc_tech_filter" />
</activity>

然后,您的活动将在标签检测时自动启动。 (注意:避免对此类调度使用TAG_DISCOVERED意图,因为该意图(在清单中使用时)仅作为后备!)

现在,如果您的应用已启动,您可以通过TECH_DISCOVERED方法检测它是否是onCreate()意图启动的:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    uid = (TextView) findViewById(R.id.UID);

    Intent i = getIntent();  // get the intent that started us
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(i.getAction())) {
        uid.setText(ByteArrayToHexString(i.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
    }
}

前台调度系统:

前台调度系统用于注册当前前台活动,以便通知(并获得接收的最高优先级)NFC事件。您可以通过使用TAG_DISCOVERED方法注册相关意图(enableForegroundDispatch()在这种情况下很好)来使用它。收到NFC活动后,系统会通过onNewIntent()通知您的活动。

但请注意,只有当您的活动是前台活动时才允许注册前台调度(即您必须在onResume()中注册并且必须在onPause()中取消注册),否则您的应用将阻止NFC事件或崩溃(取决于Android版本)。所以从不enableForegroundDispatch()中使用onCreate()