title: Android Unable to write mifare ultralight tag - Not an Ndef Tag?
body: <p>I have an android app that writes nfc tags,  and I can write an "NXP MIFARE Ultralight (Utralight C) - NTAG216" With no problem.  However the same code does not appear to work with an "NXP MIFARE Ultralight (Ultralight)"</p>
  
  <p>The tech is the same,  the iso number is the same,  but ndef.java spits out its not an ndef tag.</p>
  
  <p>Here is the code bits that do the writing:</p>
  
  <pre><code>public void enableForegroundDispatch() {
      Intent intent = new Intent(activity, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
      PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);
      IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
      IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
      IntentFilter[] intentFilter = new IntentFilter[]{tagDetected,techDetected};
      nfcAdapter.enableForegroundDispatch(activity, pendingIntent, intentFilter, null);
  }
  ...
  public boolean writeNdefMessage(Intent intent, NdefMessage ndefMessage) {
      Tag tag = getTagFromIntent(intent);
      return writeNdefMessage(tag, ndefMessage);
  }
  ...
  private boolean writeNdefMessage(Tag tag, NdefMessage ndefMessage) {
      boolean result = false;
      try {
          if (tag != null) {
              Ndef ndef = Ndef.get(tag);
              if (ndef == null) {
                  result = formatTag(tag, ndefMessage);
              } else {
                  ndef.connect();
                  if (ndef.isWritable()) {
                      ndef.writeNdefMessage(ndefMessage);
                      result = true;
                  }
                  ndef.close();
              }
          }
      } catch (Exception e) {
          Log.e("writeNdefMessage", e.toString());
          e.printStackTrace();
      }
      return result;
  }
  ...
  private boolean formatTag(Tag tag, NdefMessage ndefMessage) {
      try {
          NdefFormatable ndefFormat = NdefFormatable.get(tag);
          if (ndefFormat != null) {
              ndefFormat.connect();
              ndefFormat.format(ndefMessage);
              ndefFormat.close();
              return true;
          }
      } catch (Exception e) {
          Log.e("formatTag", e.toString());
          e.printStackTrace();
      }
      return false;
  }
  </code></pre>
  
  <p>Here is the stack trace:</p>
  
  <pre><code> 08-07 17:44:26.961  13634-13634/redacted E/writeNdefMessage﹕ java.io.IOException
   08-07 17:44:26.961  13634-13634/redacted W/System.err﹕ java.io.IOException
   08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at android.nfc.tech.Ndef.writeNdefMessage(Ndef.java:320)
   08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at redacted.helpers.NfcHelper.writeNdefMessage(NfcHelper.java:113)
   08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at redacted.helpers.NfcHelper.writeNdefMessage(NfcHelper.java:100)
   08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at redacted.activities.MainActivity.onNewIntent(MainActivity.java:138)
   08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1211)
   08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1223)
   08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2459)
   08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at android.app.ActivityThread.performNewIntents(ActivityThread.java:2471)
   08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2480)
   08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at android.app.ActivityThread.access$1600(ActivityThread.java:151)
   08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1375)
   08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
   08-07 17:44:26.963  13634-13634/redacted W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
   08-07 17:44:26.963  13634-13634/redacted W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5257)
   08-07 17:44:26.963  13634-13634/redacted W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
   08-07 17:44:26.963  13634-13634/redacted W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
   08-07 17:44:26.963  13634-13634/redacted W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
   08-07 17:44:26.963  13634-13634/redacted W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  </code></pre>
  
  <p>And for good measure,  the ndef.java function:</p>
  
  <pre><code> public void writeNdefMessage(NdefMessage msg) throws IOException, FormatException {
      checkConnected();
  
      try {
          INfcTag tagService = mTag.getTagService();
          if (tagService == null) {
              throw new IOException("Mock tags don't support this operation.");
          }
          int serviceHandle = mTag.getServiceHandle();
          if (tagService.isNdef(serviceHandle)) {
              int errorCode = tagService.ndefWrite(serviceHandle, msg);
              switch (errorCode) {
                  case ErrorCodes.SUCCESS:
                      break;
                  case ErrorCodes.ERROR_IO:
                      throw new IOException();
                  case ErrorCodes.ERROR_INVALID_PARAM:
                      throw new FormatException();
                  default:
                      // Should not happen
                      throw new IOException();
              }
          }
          else {
              throw new IOException("Tag is not ndef");
          }
      } catch (RemoteException e) {
          Log.e(TAG, "NFC service dead", e);
      }
  }
  </code></pre>
  
  <p>Any idea what is wrong here?  NFC Tools and TagWriter have no problem writing an Ndef message to these cards,  is it possible its just not supported by the ndef library?  and I need to use my own code to write these tags?</p>