Android NFC标签未检测到onClick按钮

时间:2015-04-06 11:53:55

标签: android nfc

我正在开发一个Android应用程序,我需要在单击按钮时扫描NFC标签。但是,如果我点击一个按钮,它没有检测到卡片,也没有发生任何事情。请帮助我解决这个问题。

 public class Check_in extends Activity {
    EditText card_details;
    PendingIntent pendingIntent;
    IntentFilter[] intentFiltersArray;
    String[][] techList;
    NfcAdapter nfcAdapter;
    LinearLayout scanContainer;
    private boolean isScanEnabled = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.check_in);

        card_details = (EditText) findViewById(R.id.editText);


    }

    public void get_nfc(View v)
    {
        nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if(nfcAdapter == null)
            Toast.makeText(this,"This Device Not supporting       NFC",Toast.LENGTH_SHORT).show();
        else
            handleIntent();

    }

    private void handleIntent() {
        pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        intentFiltersArray = new IntentFilter[] { ndef };
        techList = new String[][] { new String[] { NfcA.class.getName(),
                MifareUltralight.class.getName()} };


    }


    @Override
    protected void onResume() {
        super.onResume();
        if(nfcAdapter !=null) {
            if(nfcAdapter.isEnabled())
                nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techList);
            else
                Toast.makeText(this,"NFC Disabled",Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    protected void onPause() {

        if(nfcAdapter !=null)
            nfcAdapter.disableForegroundDispatch(this);
        super.onPause();
    }


    @Override
    public void onNewIntent(Intent intent) {
        if(!isScanEnabled)
            return;
        Parcelable tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        card_details.setText(dumpTagData(tag).toString());

        Toast.makeText(this, "This is "+dumpTagData(tag).toString(), 5000).show();
    }


    private String dumpTagData(Parcelable p) {
        StringBuilder sb = new StringBuilder();
        Tag tag = (Tag) p;
        byte[] id = tag.getId();
        sb.append(getHex(id));
        return sb.toString();
    }


    private String getHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            int b = bytes[i] & 0xff;
            if (b < 0x10)
                sb.append('0');
            sb.append(Integer.toHexString(b));
        }
        return sb.toString();
    }


}

1 个答案:

答案 0 :(得分:1)

在onResume()和onPause()方法中,nfcAdapter始终为null(因为您只需单击按钮上的nfcAdapter)。为什么永远不会调用此enableForegroundDispatch()方法。

我对您的代码进行了一些更改(但我还没有测试过)。

public class Check_in extends Activity {

    EditText card_details;
    PendingIntent pendingIntent;
    IntentFilter[] intentFiltersArray;
    String[][] techList;
    NfcAdapter nfcAdapter;
    LinearLayout scanContainer;
    private boolean isToScan = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.check_in);

        card_details = (EditText) findViewById(R.id.editText);

        nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    }

    public void get_nfc(View v){
        isToScan = true;
    }

    @Override
    protected void onResume() {
        super.onResume();

        if(nfcAdapter != null) {

            if (nfcAdapter.isEnabled()) {

                pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                    getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

                IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);

                intentFiltersArray = new IntentFilter[] { ndef };

                techList = new String[][] { new String[] { NfcA.class.getName(),
                    MifareUltralight.class.getName()} };

                nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techList);

            } else {
                Toast.makeText(this,"NFC Disabled",Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onPause() {

        if (nfcAdapter != null) {
            nfcAdapter.disableForegroundDispatch(this);
        }

        super.onPause();
    }

    @Override
    public void onNewIntent(Intent intent) {

        if (isToScan && intent.hasExtra(NfcAdapter.EXTRA_TAG)) {
            isToScan = false;

            Parcelable tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

            card_details.setText(dumpTagData(tag).toString());

            Toast.makeText(this, "This is " + card_details.getText().toString(), 5000).show();
        }
    }

    private String dumpTagData(Parcelable p) {
        StringBuilder sb = new StringBuilder();
        Tag tag = (Tag) p;
        byte[] id = tag.getId();
        sb.append(getHex(id));
        return sb.toString();
    }

    private String getHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            int b = bytes[i] & 0xff;
            if (b < 0x10)
                sb.append('0');
            sb.append(Integer.toHexString(b));
        }
        return sb.toString();
    }
}

希望这有帮助。