Can't detect type B (iso-14443 B) card using PN7150 on Android

Viewed 39

Currently i'm using PN7150 with android board. The NFC reader (PN7150) can be used to read type A cards, but cannot detect type B cards at all.

Below is the PCB antenna that I used :

enter image description here

enter image description here

The following is a schematic for the antenna matching circuit:

enter image description here

Smith chart simulation results from RFSim99: enter image description here

Below is the signal shape (Smith chart) of the measurement results on the PCB board using Nano VNA.

enter image description here

This is my android code :

MainActivity.java

      package com.iwjo.nfcfieldstrength;


      public class MainActivity extends AppCompatActivity implements NfcAdapter.ReaderCallback, NfcThread.UiCallback{

          public static final boolean mbUsbExternalUSBManager = false;
          private static final String ACTION_USB_PERMISSION = "com.iwjo.ktpfieldstrength.USB_PERMISSION";
          private UsbManager mUsbManager = null;
          private PendingIntent mPermissionIntent= null;
          public static final int REQUEST_WRITE_PERMISSION = 786;
          private static final int REQUEST_WRITE_STORAGE_REQUEST_CODE = 112;

          private NfcAdapter nfcadapter;

          private String[][] nfctechfilter = new String[][] { new String[] { NfcA.class.getName() } };

          private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver(){
              public void onReceive(Context context, Intent intent){
                  String action = intent.getAction();
                  if(ACTION_USB_PERMISSION.equals(action)){
                      synchronized(this){

                          if (action.equals(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)) {
                              int state = intent.getIntExtra(NfcAdapter.EXTRA_ADAPTER_STATE,
                                      NfcAdapter.STATE_ON);
                              if (state == NfcAdapter.STATE_ON
                                      || state == NfcAdapter.STATE_TURNING_ON) {
                                  ///Log.d(TAGCL, "state: " + state);
                                  if (state == NfcAdapter.STATE_ON) {
                                      nfcadapter.enableReaderMode(
                                              MainActivity.this,
                                              MainActivity.this,
                                              NfcAdapter.FLAG_READER_NFC_A
                                                      | NfcAdapter.FLAG_READER_NFC_B
                                                      //| NfcAdapter.FLAG_READER_NFC_F
                                                      //| NfcAdapter.FLAG_READER_NFC_V
                                                      //| NfcAdapter.FLAG_READER_NFC_BARCODE
                                                      //| NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS
                                                      | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK,
                                              null);
                                  }
                              } else {
                                  showMessage(getString(R.string.nfc_not_activated));
                              }
                          }

                      }
                  }
              }
          };

          private void requestPermission() {
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                  requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},  REQUEST_WRITE_PERMISSION);
              }
          }
          @Override
          public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
              if (requestCode == REQUEST_WRITE_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  /////--Log.d(TAGSAM, "permission granted");
              }
          }

          @Override
          protected void onPostResume() {
              //bindComponents();
              super.onPostResume();
              if (!nfcadapter.isEnabled()) {
                  showMessage(getString(R.string.nfc_not_activated));
                  return;
              }

              IntentFilter filter = new IntentFilter(
                      NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
              //registerReceiver(mBroadcastReceiver, filter);

              nfcadapter.enableReaderMode(this, this, NfcAdapter.FLAG_READER_NFC_A
                      | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK | NfcAdapter.FLAG_READER_NFC_B | NfcAdapter.FLAG_READER_NFC_F, null);
          }

          @Override
          protected void onPause() {
              super.onPause();
              //unregisterReceiver(mBroadcastReceiver);
              nfcadapter.disableReaderMode(this);
          }
          @Override
          protected void onDestroy() {
              if( mbUsbExternalUSBManager ){
                  unregisterReceiver(mUsbReceiver);
              }
              if( mbUsbExternalUSBManager ){
                  unregisterReceiver(mUsbReceiver);
              }
              super.onDestroy();
          }


          @Override
          public void showMessage(final String msg) {
              runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
                  }
              });
          }

          @Override
          public void setEditText(final int id, final String txt, final int status) {
              runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      //EditText edit = (EditText) findViewById(id);
                      //eMessage.setText(txt);
                      setMessageText(txt, status);
                  }
              });
          }

          public void setMessageText(String text, int status)
          {
              // status 0 : normal proses, 1 : berhasil, 2 : error

          }

          @Override
          public void setBiodata(final Biodata bio) {
              runOnUiThread(new Runnable() {
                  @Override
                  public void run() {

                  }
              });
          }

          @Override
          public void setProgressBar(final int id, final int value) {
              runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      ProgressBar pbar = (ProgressBar) findViewById(id);
                      pbar.setProgress(value);
                  }
              });
          }

          private void setErrorMessage(String text)
          {
              //eMessage.setBackground(Color.red());
          }

          @Override
          public void onTagDiscovered(Tag tag) {
              if(isSam_authenticate) {
                  Runnable nfcr = new NfcThread(this, tag, this);
                  new Thread(nfcr).start();
              }
          }


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


                  ActionBar actionBar = getSupportActionBar();
                  //actionBar.setIcon(R.mipmap.ic_launcher);
                  actionBar.setDisplayShowHomeEnabled(true);

                  if (android.os.Build.VERSION.SDK_INT > 9) {
                      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                      StrictMode.setThreadPolicy(policy);
                  }



                  progressBar = (ProgressBar) findViewById(R.id.progressBar);
                  eMessage = (TextView) findViewById(R.id.eMessage);

                  getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

                  try {

                      nfcadapter = NfcAdapter.getDefaultAdapter(this);
                      if (nfcadapter == null) {
                          Toast.makeText(this, getString(R.string.nfc_unavailable), Toast.LENGTH_LONG).show();
                          finish();
                      }

                  } catch (Exception f) {
                  }

                  mainContext = this;

                  requestAppPermissions();

          }




          private void requestAppPermissions() {
              if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                  return;
              }

              if (hasReadPermissions() && hasWritePermissions()) {
                  return;
              }

              ActivityCompat.requestPermissions(this,
                      new String[] {
                              Manifest.permission.READ_EXTERNAL_STORAGE,
                              Manifest.permission.WRITE_EXTERNAL_STORAGE
                      }, REQUEST_WRITE_STORAGE_REQUEST_CODE); // your request code
          }

          private boolean hasReadPermissions() {
              return (ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
          }

          private boolean hasWritePermissions() {
              return (ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
          }



      }

NfcThread.java

        package com.iwjo.nfcfieldstrength;

        import android.content.Context;
        import android.nfc.Tag;
        import android.nfc.tech.IsoDep;
        import android.util.Log;
        import com.iwjo.ktpfieldstrength.R;

        import java.io.IOException;


        public class NfcThread implements Runnable {

            public static final String TAG_CL_IN = ">>";
            public static final String TAG_CL_OUT = "<<";




            public interface UiCallback {
                void showMessage(String msg);
                void setEditText(int id, String txt, int status);
                void setBiodata(Biodata bio);
                void setProgressBar(int id, int value);
            }

            private Context context;
            private Tag tag;
            private UiCallback cb;

            public NfcThread(Context context, Tag tag, UiCallback cb) {
                this.context = context;
                this.tag = tag;
                this.cb = cb;
            }

            private IsoDep iso;

            @Override
            public void run() {

                iso = IsoDep.get(tag);
                if (iso == null) {
                    cb.showMessage(context.getString(R.string.non_iso));
                    return;
                }
                try {
                    iso.connect();
                } catch (IOException e) {
                    cb.showMessage(context.getString(R.string.iso_connect_error));
                    Log.e(TAG, context.getString(R.string.iso_connect_error) + " : " + e.getMessage());
                    return;
                }

                try {


                    byte[] uid = iso.getTag().getId();
                    String ret = StringUtils.convertByteArrayToHexString(uid);
                    cb.showMessage("Card detected, UID : "+ret);
                    cb.setEditText(R.id.eMessage, "", 0);

                } catch (Exception e) {
                } finally {
                    try {
                        iso.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            private String send_apdu(String sapdu) throws IOException {
                //Log.i(TAG, "SEND -> " + sapdu);
                final byte [] apdu = StringUtils.convertHexStringToByteArray(StringUtils.removeSpaces(sapdu));
                byte [] recv = iso.transceive(apdu);
                String ret = StringUtils.convertByteArrayToHexString(recv);
                Log.i(TAG_CL_OUT, ret);
                return ret;
            }
        }

AndroidManifest.xml

      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.iwjo.nfcfieldstrength">

          <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
          <uses-permission android:name="android.permission.NFC" />
          <uses-feature android:name="android.hardware.nfc" android:required="true" />
          <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
          <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
          <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
          <uses-permission android:name="android.permission.INTERNET" />
          <permission android:name="android.permission.REBOOT" />
          <permission android:name="android.permission.MODIFY_PHONE_STATE" />
          <permission android:name="android.permission.DEVICE_POWER" />
          <uses-feature android:name="android.hardware.usb.host" />

          <application
              android:allowBackup="true"
              android:icon="@mipmap/ic_launcher"
              android:label="@string/app_name"
              android:roundIcon="@mipmap/ic_launcher_round"
              android:supportsRtl="true"
              android:theme="@style/AppTheme">
              <activity
                  android:name=".MainActivity"
                  android:exported="true">
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN" />

                      <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
              </activity>
          </application>

      </manifest>

Please give me advice..

0 Answers
Related