NFC.enableReaderMode非常慢。怎么修?

时间:2017-04-27 09:38:23

标签: java android nfc

使用以下测试程序......

MainActivity

package nfctest;

import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent nfcIntent = new Intent(this, getClass());
        nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        setContentView(R.layout.activity_main);

        nfc();
    }

    @Override
    protected void onResume(){
        nfcAdapter = NfcAdapter.getDefaultAdapter( this );

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);

        IntentFilter[] filters = new IntentFilter[]{};

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        nfcAdapter.enableForegroundDispatch(this, pendingIntent, filters, null);

        super.onResume();
    }

    NfcAdapter nfcAdapter;
    private void nfc(){
        nfcAdapter = NfcAdapter.getDefaultAdapter( this );

        System.out.println("NFC isEnabled: " + nfcAdapter.isEnabled());
        System.out.println("NFC isNdefPushEnabled: " + nfcAdapter.isNdefPushEnabled());

        Bundle options = new Bundle();
        options.putInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY, 1);

        nfcAdapter.enableReaderMode(this, new NfcAdapter.ReaderCallback() {
            @Override
            public void onTagDiscovered(Tag tag) {
                System.out.println("NFC onTagDiscovered" + tag.toString());
            }
        }, Integer.MAX_VALUE, options);
    }

    @Override
    protected void onNewIntent(Intent intent){
        super.onNewIntent(intent);
        System.out.println("NFC onNewIntent");
    }


    @Override
    protected void onPause(){
        nfcAdapter.disableForegroundDispatch(this);
        super.onPause();
    }
}

的AndroidManifest.xml

<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />

我做错了什么?

我将卡放在手机下面(屏幕朝上,如果重要),打印需要很多分钟:

NFC onTagDiscoveredTAG: Tech [android.nfc.tech.IsoDep, android.nfc.tech.NfcB]

为什么在将卡放在手机下后,手机会立即打印出来?

0 个答案:

没有答案
相关问题