Java 类android.nfc.tech.Ndef 实例源码

项目:react-native-nfc-manager    文件:NfcManager.java   
@ReactMethod
  private void registerTagEvent(String alertMessage, Boolean invalidateAfterFirstRead, Callback callback) {
      Log.d(LOG_TAG, "registerTag");
isForegroundEnabled = true;

// capture all mime-based dispatch NDEF
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
    ndef.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
    throw new RuntimeException("fail", e);
   }
intentFilters.add(ndef);

// capture all rest NDEF, such as uri-based
      intentFilters.add(new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED));
techLists.add(new String[]{Ndef.class.getName()});

// for those without NDEF, get them as tags
      intentFilters.add(new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED));

if (isResumed) {
    enableDisableForegroundDispatch(true);
}
      callback.invoke();
    }
项目:android-nfc    文件:WriteUriActivity.java   
/**
 * 写入标签
 *
 * @param message
 * @param tag
 * @return
 */
public static boolean writeTag(NdefMessage message, Tag tag) {
    int size = message.toByteArray().length;
    try {
        Ndef ndef = Ndef.get(tag);
        if (ndef != null) {
            ndef.connect();
            if (!ndef.isWritable()) {
                return false;
            }
            if (ndef.getMaxSize() < size) {
                return false;
            }
            ndef.writeNdefMessage(message);
            return true;
        }
    } catch (Exception e) {
    }
    return false;
}
项目:CustomAndroidOneSheeld    文件:NfcShield.java   
private DataReply getNdefUsedSize() {
    DataReply dataReply = new DataReply();
    int size = 0;
    if (currentTag != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) {
            Ndef ndef = Ndef.get(currentTag);
            if (ndef != null && ndef.getCachedNdefMessage() != null)
                size = ndef.getCachedNdefMessage().toByteArray().length;
            else {
                dataReply.setError(TAG_READING_ERROR);
                return dataReply;
            }
            try {
                if (ndef != null) ndef.close();
            } catch (IOException e) {
            }
        }
    }
    dataReply.setIntegerData(size);
    return dataReply;
}
项目:CustomAndroidOneSheeld    文件:NfcShield.java   
private DataReply getNdefMaxSize() {
    DataReply dataReply = new DataReply();
    int maxSize = 0;
    if (currentTag != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) {
            Ndef ndef = Ndef.get(currentTag);
            if (ndef != null) {
                maxSize = ndef.getMaxSize();
                if (maxSize <= 0){
                    dataReply.setError(TAG_READING_ERROR);
                    return dataReply;
                }
            }else {
                dataReply.setError(TAG_READING_ERROR);
                return dataReply;
            }
            try {
                if (ndef != null) ndef.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    dataReply.setIntegerData(maxSize);
    return dataReply;
}
项目:Eulen    文件:nfctagbase.java   
protected void writeTAG(Tag tag) throws IOException, FormatException {
    Ndef ndefTag = Ndef.get(tag);
    byte[] stringBytes = passphrase.getBytes();

    NdefRecord data = NdefRecord.createMime(CONST.NFC_MIME_LOGIN, stringBytes);
    NdefMessage message = new NdefMessage(data);

    if (ndefTag != null) { //write to formatted tag
        ndefTag.connect();
        ndefTag.writeNdefMessage(message);
    } else { //format the tag
        NdefFormatable format = NdefFormatable.get(tag);
        if(format != null) {
            format.connect();
            format.format(message);
        }
    }
}
项目:RxBluetoothAuto    文件:Home.java   
public String nfcRead(Tag t)
{
    Tag tag = t;
    Ndef ndef = Ndef.get(tag);
    if (ndef == null) {
        return null;
    }
    NdefMessage ndefMessage = ndef.getCachedNdefMessage();
    NdefRecord[] records = ndefMessage.getRecords();
    for (NdefRecord ndefRecord : records)
    {
        if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT))
        {
            try {return readText(ndefRecord);} catch (UnsupportedEncodingException e) {}
        }
    }

    return null;

}
项目:android-nfc-wrapper    文件:NFCReadTask.java   
@Override
protected NFCTag doInBackground(Intent... params) {
    Intent intent = params[0];

    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    Ndef ndef = Ndef.get(tag);

    if (ndef == null) {
        return null;
    }

    NdefMessage ndefMessage = ndef.getCachedNdefMessage();

    if(ndefMessage != null) {
        NdefRecord[] records = ndefMessage.getRecords();
        for (NdefRecord ndefRecord : records) {
            try {
                return new NFCTag(ndefRecord).decode();
            } catch (UnsupportedEncodingException e) {
                Log.e("NFC", "Unsupported Encoding", e);
            }
        }
    }

    return null;
}
项目:android-nfc-tag-read-write    文件:NFCWriteFragment.java   
private void writeToNfc(Ndef ndef, String message){

        mTvMessage.setText(getString(R.string.message_write_progress));
        if (ndef != null) {

            try {
                ndef.connect();
                NdefRecord mimeRecord = NdefRecord.createMime("text/plain", message.getBytes(Charset.forName("US-ASCII")));
                ndef.writeNdefMessage(new NdefMessage(mimeRecord));
                ndef.close();
                //Write Successful
                mTvMessage.setText(getString(R.string.message_write_success));

            } catch (IOException | FormatException e) {
                e.printStackTrace();
                mTvMessage.setText(getString(R.string.message_write_error));

            } finally {
                mProgress.setVisibility(View.GONE);
            }

        }
    }
项目:android-nfc-tag-read-write    文件:MainActivity.java   
@Override
protected void onNewIntent(Intent intent) {
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

    Log.d(TAG, "onNewIntent: "+intent.getAction());

    if(tag != null) {
        Toast.makeText(this, getString(R.string.message_tag_detected), Toast.LENGTH_SHORT).show();
        Ndef ndef = Ndef.get(tag);

        if (isDialogDisplayed) {

            if (isWrite) {

                String messageToWrite = mEtMessage.getText().toString();
                mNfcWriteFragment = (NFCWriteFragment) getFragmentManager().findFragmentByTag(NFCWriteFragment.TAG);
                mNfcWriteFragment.onNfcDetected(ndef,messageToWrite);

            } else {

                mNfcReadFragment = (NFCReadFragment)getFragmentManager().findFragmentByTag(NFCReadFragment.TAG);
                mNfcReadFragment.onNfcDetected(ndef);
            }
        }
    }
}
项目:archive-carro-inteligente    文件:NFCHandler.java   
public void writeTag(Tag tag, NdefMessage message)
{
    if (tag != null) {
        try {
            Ndef ndefTag = Ndef.get(tag);

            if (ndefTag == null) {
                // Let's try to format the Tag in NDEF
                NdefFormatable nForm = NdefFormatable.get(tag);
                if (nForm != null) {
                    nForm.connect();
                    nForm.format(message);
                    nForm.close();
                }
            }
            else {
                ndefTag.connect();
                ndefTag.writeNdefMessage(message);
                ndefTag.close();
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}
项目:archive-carro-inteligente    文件:NFCHandler.java   
public String getTagText() throws Exception
{
    if (tag == null)
    {
        throw new Exception("Please call handle new read as tag is null");
    }
    Ndef ndef = Ndef.get(tag);
    NdefMessage ndefMessage = ndef.getCachedNdefMessage();

    NdefRecord[] records = ndefMessage.getRecords();
    for (NdefRecord ndefRecord : records)
    {
        if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT))
        {
            try
            {
                return readText(ndefRecord);
            }
            catch (UnsupportedEncodingException e)
            {
                Toast.makeText(activity, "Unsupported encoding", Toast.LENGTH_SHORT).show();
            }
        }
    }
    return null;
}
项目:1Sheeld-Android-App    文件:NfcShield.java   
private DataReply getNdefUsedSize() {
    DataReply dataReply = new DataReply();
    int size = 0;
    if (currentTag != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) {
            Ndef ndef = Ndef.get(currentTag);
            if (ndef != null && ndef.getCachedNdefMessage() != null)
                size = ndef.getCachedNdefMessage().toByteArray().length;
            else {
                dataReply.setError(TAG_READING_ERROR);
                return dataReply;
            }
            try {
                if (ndef != null) ndef.close();
            } catch (IOException e) {
            }
        }
    }
    dataReply.setIntegerData(size);
    return dataReply;
}
项目:1Sheeld-Android-App    文件:NfcShield.java   
private DataReply getNdefMaxSize() {
    DataReply dataReply = new DataReply();
    int maxSize = 0;
    if (currentTag != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) {
            Ndef ndef = Ndef.get(currentTag);
            if (ndef != null) {
                maxSize = ndef.getMaxSize();
                if (maxSize <= 0){
                    dataReply.setError(TAG_READING_ERROR);
                    return dataReply;
                }
            }else {
                dataReply.setError(TAG_READING_ERROR);
                return dataReply;
            }
            try {
                if (ndef != null) ndef.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    dataReply.setIntegerData(maxSize);
    return dataReply;
}
项目:PowerSwitch_Android    文件:NfcHandler.java   
/**
 * Writes an NdefMessage to a NFC tag
 */
public static void writeTag(NdefMessage message, Tag tag) throws Exception {
    int size = message.toByteArray().length;
    Ndef ndef = Ndef.get(tag);
    if (ndef != null) {
        ndef.connect();
        if (!ndef.isWritable()) {
            throw new NfcTagNotWritableException();
        }
        if (ndef.getMaxSize() < size) {
            throw new NfcTagInsufficientMemoryException(ndef.getMaxSize(), size);
        }
        ndef.writeNdefMessage(message);
    } else {
        NdefFormatable format = NdefFormatable.get(tag);
        if (format != null) {
            format.connect();
            format.format(message);
        } else {
            throw new IllegalArgumentException("Ndef format is NULL");
        }
    }
}
项目:LEHomeMobile_android    文件:NfcReadNdefAsyncTask.java   
@Override
protected ArrayList<String> doInBackground(Tag... params) {
    Tag tag = params[0];

    Ndef ndef = Ndef.get(tag);
    if (ndef == null) {
        // NDEF is not supported by this Tag.
        return null;
    }

    ArrayList<String> results = new ArrayList<>();
    NdefMessage ndefMessage = ndef.getCachedNdefMessage();
    NdefRecord[] records = ndefMessage.getRecords();
    for (NdefRecord ndefRecord : records) {
        if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
            try {
                results.add(readText(ndefRecord));
            } catch (UnsupportedEncodingException e) {
                Log.e(TAG, "Unsupported Encoding", e);
            }
        }
    }

    return results;
}
项目:LEHomeMobile_android    文件:NFCHelper.java   
public static boolean writableTag(Tag tag) {
    try {
        Ndef ndef = Ndef.get(tag);
        if (ndef != null) {
            ndef.connect();
            if (!ndef.isWritable()) {
                ndef.close();
                return false;
            }
            ndef.close();
            return true;
        }
    } catch (Exception e) {
    }
    return false;
}
项目:saarang-iosched    文件:NfcBadgeActivity.java   
private void readTag(Tag t) {
    byte[] id = t.getId();

    // get NDEF tag details
    Ndef ndefTag = Ndef.get(t);

    // get NDEF message details
    NdefMessage ndefMesg = ndefTag.getCachedNdefMessage();
    if (ndefMesg == null) {
        return;
    }
    NdefRecord[] ndefRecords = ndefMesg.getRecords();
    if (ndefRecords == null) {
        return;
    }
    for (NdefRecord record : ndefRecords) {
        short tnf = record.getTnf();
        String type = new String(record.getType());
        if (tnf == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(type.getBytes(), NdefRecord.RTD_URI)) {
            String url = new String(record.getPayload());
            recordBadge(url);
        }
    }
}
项目:AppDevFestSudeste2015    文件:NfcBadgeActivity.java   
private void readTag(Tag t) {
    byte[] id = t.getId();

    // get NDEF tag details
    Ndef ndefTag = Ndef.get(t);

    // get NDEF message details
    NdefMessage ndefMesg = ndefTag.getCachedNdefMessage();
    if (ndefMesg == null) {
        return;
    }
    NdefRecord[] ndefRecords = ndefMesg.getRecords();
    if (ndefRecords == null) {
        return;
    }
    for (NdefRecord record : ndefRecords) {
        short tnf = record.getTnf();
        String type = new String(record.getType());
        if (tnf == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(type.getBytes(), NdefRecord.RTD_URI)) {
            String url = new String(record.getPayload());
            recordBadge(url);
        }
    }
}
项目:PhoneProfilesPlus    文件:NFCTagReadWriteManager.java   
private void readTagFromIntent(Intent intent) {
    if (intent != null){
        String action = intent.getAction();
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
            tagReaded = true;

            // get NDEF tag details
            Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            if (tag != null) {
                Ndef ndefTag = Ndef.get(tag);
                //int tagSize = ndefTag.getMaxSize();         // tag size
                tagIsWritable = ndefTag.isWritable();   // is tag writable?
                //String tagType = ndefTag.getType();            // tag type

                Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
                if (rawMsgs != null) {
                    NdefRecord[] records = ((NdefMessage) rawMsgs[0]).getRecords();
                    String text = ndefRecordToString(records[0]);
                    onTagReadListener.onTagRead(text);
                }
            }
        }
    }
}
项目:NFCMessageBoard    文件:MainScreen.java   
@Override
protected String doInBackground(Tag... params) {
    Tag tag = params[0];

    Ndef ndef = Ndef.get(tag);
    if (ndef == null) {
        // NDEF is not supported by this Tag.
        return null;
    }

    NdefMessage ndefMessage = ndef.getCachedNdefMessage();

    NdefRecord[] records = ndefMessage.getRecords();
    for (NdefRecord ndefRecord : records) {
        if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
            try {
                return readText(ndefRecord);
            } catch (UnsupportedEncodingException e) {
                //Log.e(TAG, "Unsupported Encoding", e);
            }
        }
    }

    return null;
}
项目:NFCPoC    文件:MainActivity.java   
@Override
protected String doInBackground(Tag... params) {
    Tag tag = params[0];

    Ndef ndef = Ndef.get(tag);
    if (ndef == null) {
        // NDEF is not supported by this Tag.
        return null;
    }

    NdefMessage ndefMessage = ndef.getCachedNdefMessage();

    NdefRecord[] records = ndefMessage.getRecords();
    for (NdefRecord ndefRecord : records) {
        if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
            try {
                return readText(ndefRecord);
            } catch (UnsupportedEncodingException e) {
                Log.e(TAG, "Unsupported Encoding", e);
            }
        }
    }

    return null;
}
项目:easypgp    文件:NfcActivity.java   
@Override
protected String doInBackground(Tag... params) {
  Tag tag = params[0];

  Ndef ndef = Ndef.get(tag);
  if (ndef == null) {
    // NDEF is not supported by this Tag.
    return null;
  }
  try {
    NdefMessage msg = new NdefMessage(
        new NdefRecord[] { NdefRecord.createMime(
            "application/pubk.com.sawyer.easypgp", publicKeyBytes) });
    ndef.connect();
    ndef.writeNdefMessage(msg);
    ndef.close();
    return "Success";
  } catch (Exception e) {
    e.printStackTrace();
  }

  return null;
}
项目:easypgp    文件:NfcActivity.java   
@Override
protected String doInBackground(Tag... params) {
  Tag tag = params[0];

  Ndef ndef = Ndef.get(tag);
  if (ndef == null) {
    // NDEF is not supported by this Tag.
    return null;
  }

  NdefMessage ndefMessage = ndef.getCachedNdefMessage();

  NdefRecord[] records = ndefMessage.getRecords();
  for (NdefRecord ndefRecord : records) {
    try {
      tmpPubKey = readKey(ndefRecord);
      return tmpPubKey.toString();
    } catch (UnsupportedEncodingException e) {
      Log.e(TAG, "Unsupported Encoding", e);
    }
  }

  return null;
}
项目:devfestnorte-app    文件:NfcBadgeActivity.java   
private void readTag(Tag t) {
    byte[] id = t.getId();

    // get NDEF tag details
    Ndef ndefTag = Ndef.get(t);

    // get NDEF message details
    NdefMessage ndefMesg = ndefTag.getCachedNdefMessage();
    if (ndefMesg == null) {
        return;
    }
    NdefRecord[] ndefRecords = ndefMesg.getRecords();
    if (ndefRecords == null) {
        return;
    }
    for (NdefRecord record : ndefRecords) {
        short tnf = record.getTnf();
        String type = new String(record.getType());
        if (tnf == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(type.getBytes(), NdefRecord.RTD_URI)) {
            String url = new String(record.getPayload());
            recordBadge(url);
        }
    }
}
项目:ch.bfh.mobicomp    文件:MainActivity.java   
private void read(Intent intent) {
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    Ndef ndef = Ndef.get(tag);

    if (ndef == null) {
        Toast.makeText(this, "Read failed", Toast.LENGTH_LONG).show();
    } else {
        // Read tag
        NdefMessage msg;
        msg = ndef.getCachedNdefMessage();
        if (msg == null || msg.getRecords() == null
                || msg.getRecords()[0] == null
                || msg.getRecords()[0].getPayload() == null) {
            Toast.makeText(this, "Read failed", Toast.LENGTH_LONG).show();
            return;
        }

        String text = new String(msg.getRecords()[0].getPayload());

        Toast.makeText(this, text, Toast.LENGTH_LONG).show();

    }
}
项目:ch.bfh.mobicomp    文件:MainActivity.java   
private void read(Intent intent) {
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    Ndef ndef = Ndef.get(tag);

    if (ndef == null) {
        Toast.makeText(this, "Read failed", Toast.LENGTH_LONG).show();
    } else {
        // Read tag
        NdefMessage msg;
        msg = ndef.getCachedNdefMessage();
        if (msg == null || msg.getRecords() == null
                || msg.getRecords()[0] == null
                || msg.getRecords()[0].getPayload() == null) {
            Toast.makeText(this, "Read failed", Toast.LENGTH_LONG).show();
            return;
        }

        String text = new String(msg.getRecords()[0].getPayload());

        Toast.makeText(this, text, Toast.LENGTH_LONG).show();

    }
}
项目:saarang-iosched    文件:NfcBadgeActivity.java   
private void readTag(Tag t) {
    byte[] id = t.getId();

    // get NDEF tag details
    Ndef ndefTag = Ndef.get(t);

    // get NDEF message details
    NdefMessage ndefMesg = ndefTag.getCachedNdefMessage();
    if (ndefMesg == null) {
        return;
    }
    NdefRecord[] ndefRecords = ndefMesg.getRecords();
    if (ndefRecords == null) {
        return;
    }
    for (NdefRecord record : ndefRecords) {
        short tnf = record.getTnf();
        String type = new String(record.getType());
        if (tnf == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(type.getBytes(), NdefRecord.RTD_URI)) {
            String url = new String(record.getPayload());
            recordBadge(url);
        }
    }
}
项目:Android--Event-Triggered-Skype-Caller    文件:WriteNFCActivity.java   
private boolean writableTag(Tag tag) {

        try {
            Ndef ndef = Ndef.get(tag);
            if (ndef != null) {
                ndef.connect();
                if (!ndef.isWritable()) {
                    ToastUtils.makeWarningToast(this, "Tag is Read-Only.");

                    ndef.close();
                    return false;
                }
                ndef.close();
                return true;
            }
        } catch (Exception e) {
            Logger.e(TAG, e.getLocalizedMessage());
            ToastUtils.makeWarningToast(this, "Failed to Read Tag");
        }

        return false;
    }
项目:itheima    文件:ShowNFCTagContentActivity.java   
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_nfctag_content);
    mTagContent = (TextView) findViewById(R.id.textview_tag_content);

    mDetectedTag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);

    Ndef ndef = Ndef.get(mDetectedTag);

    mTagText = ndef.getType() + "\nmax size:" + ndef.getMaxSize()
            + "bytes\n\n";

    readNFCTag();

    mTagContent.setText(mTagText);
}
项目:itheima    文件:MainActivity.java   
/**
 * 将数据写入标签中
 * @param msg
 * @param tag
 * @return
 */
private boolean writeTag(NdefMessage msg,Tag tag){

    try {

        Ndef ndef = Ndef.get(tag);
        ndef.connect();
        ndef.writeNdefMessage(msg);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }


    return false;
}
项目:CryptoNFC    文件:MainActivity.java   
private void setForegroundListener() {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    boolean handleFormatable = preferences.getBoolean("format_ndef_formatable_tags", false);

    pi = PendingIntent.getActivity(this, 0, new Intent(this,getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    intentFiltersArray = null;
    if(handleFormatable)
        techList = new String[][]{ new String[]{ NfcA.class.getName(),Ndef.class.getName()},
                new String[]{ NfcB.class.getName(),Ndef.class.getName()},
                new String[]{ NfcF.class.getName(),Ndef.class.getName()},
                new String[]{ NfcV.class.getName(),Ndef.class.getName()},
                new String[]{ NfcA.class.getName(),NdefFormatable.class.getName()},
                new String[]{ NfcB.class.getName(),NdefFormatable.class.getName()},
                new String[]{ NfcF.class.getName(),NdefFormatable.class.getName()},
                new String[]{ NfcV.class.getName(),NdefFormatable.class.getName()}};
    else
        techList = new String[][]{ new String[]{ NfcA.class.getName(),Ndef.class.getName()},
                new String[]{ NfcB.class.getName(),Ndef.class.getName()},
                new String[]{ NfcF.class.getName(),Ndef.class.getName()},
                new String[]{ NfcV.class.getName(),Ndef.class.getName()}};
}
项目:FridgeCheckup    文件:WriteTagActivity.java   
private void parseIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
    Log.i(TAG, "Ndef NFC TAG discovered " + intent);
    nfcTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    ndef = Ndef.get(nfcTag);

    Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    if (rawMsgs != null) {
    readMsgs = new NdefMessage[rawMsgs.length];
    for (int i = 0; i < rawMsgs.length; i++) {
        readMsgs[i] = (NdefMessage) rawMsgs[i];
    }
    }
} else if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
    Log.i(TAG, "NFC TAG discovered " + intent);
    nfcTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    } else {
    Log.d(TAG, "unknown or no NFC TAG " + intent);
}
   }
项目:AndroidMultiChannelMiddleware    文件:NfcCommunicator.java   
@SuppressLint("NewApi")
public NfcCommunicator(Activity activity, NetworkDaemon daemon) {
    super(activity, daemon);

    IntentFilter filter = new IntentFilter();
    filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
    filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
    filter.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
    // Filter for the custom external type as specified in
    // http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#ext-type
    // (This does not seem work as it does not exclude NDEF messages with a different type)
    filter.addDataScheme("vnd.android.nfc");
    filter.addDataAuthority("ext", null);
    filter.addDataPath(extRecordDomain + ":" + extRecordType, PatternMatcher.PATTERN_PREFIX);
    mFilters = new IntentFilter[] { filter };

    // Only consider NDEF-Tags
    mTechLists = new String[][] { new String[] { Ndef.class.getName() } };

    setupActivityBasedVariables(activity);
}
项目:C85-Android-4.4-Sample    文件:MainActivity.java   
/**
 * NFC
 */
@Override
public void onTagDiscovered(Tag tag) {
    Ndef ndef = Ndef.get(tag);
    NdefMessage message;

    if (ndef != null) {
        message = ndef.getCachedNdefMessage();
    } else {
        return;
    }

    mUrls.clear();

    if (message != null) {
        digUrisInMessage(message);
    }

    // URL一覧の変更をListViewに通知する
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mAdapter.notifyDataSetChanged();
        }
    });
}
项目:kanban-salad    文件:NfcProgramer.java   
@Override
public ThereWas programTag(ProgramableTag tag) {
    Intent intent = parentActivity.getIntent();

    if (aTagWeCanHandleWasScanned(intent)) {
        Ndef ndefTag = getNdefTagFrom(intent);

        if (!ndefTag.isWritable()) {
            throw new TagIsNotWritable();
        }

        program(ndefTag, tag);
        return ThereWas.A_TAG_TO_PROGRAM;
    }

    return ThereWas.NO_TAG_TO_PROGRAM;
}
项目:SuperGenPass    文件:NfcWriteFragment.java   
private void writeNfcTag(@NonNull final Tag tag) {
    final Ndef ndef = Ndef.get(tag);

    if (ndef == null) {
        showWriteError(getText(R.string.nfc_error_no_ndef));
        return;
    }

    if (!ndef.isWritable()) {
        showWriteError(getText(R.string.nfc_error_not_writable));
        return;
    }

    final NdefMessage message = NdefUtils.toNdefMessage(mPassword);

    final int messageLength = message.getByteArrayLength();
    final int maxSize = ndef.getMaxSize();

    if (messageLength > maxSize) {
        showWriteError(
                getString(R.string.nfc_error_not_enough_space_format, messageLength, maxSize));
        return;
    }

    new TagWriteTask(this).execute(new TagWriteParam(ndef, message));
}
项目:SuperGenPass    文件:NfcWriteFragment.java   
@Override
protected Boolean doInBackground(final TagWriteParam... params) {
    final Ndef ndef = params[0].mTag;
    final NdefMessage message = params[0].mMessage;

    try {
        ndef.connect();
        ndef.writeNdefMessage(message);

        Log.d(TAG, "Wrote tag");
        return true;
    } catch (IOException | FormatException e) {
        mException = e;
    }

    return false;
}
项目:react-native-nfc-manager    文件:NfcManager.java   
private WritableMap ndef2React(Ndef ndef, Parcelable[] messages) {
try {
    JSONObject json = buildNdefJSON(ndef, messages);
    return JsonConvert.jsonToReact(json);
} catch (JSONException ex) {
    return null;
}
  }
项目:react-native-nfc-manager    文件:NfcManager.java   
JSONObject buildNdefJSON(Ndef ndef, Parcelable[] messages) {
    JSONObject json = Util.ndefToJSON(ndef);

    // ndef is null for peer-to-peer
    // ndef and messages are null for ndef format-able
    if (ndef == null && messages != null) {

        try {

            if (messages.length > 0) {
                NdefMessage message = (NdefMessage) messages[0];
                json.put("ndefMessage", Util.messageToJSON(message));
                // guessing type, would prefer a more definitive way to determine type
                json.put("type", "NDEF Push Protocol");
            }

            if (messages.length > 1) {
                Log.d(LOG_TAG, "Expected one ndefMessage but found " + messages.length);
            }

        } catch (JSONException e) {
            // shouldn't happen
            Log.e(Util.TAG, "Failed to convert ndefMessage into json", e);
        }
    }
    return json;
}
项目:react-native-nfc-manager    文件:Util.java   
static String translateType(String type) {
    String translation;
    if (type.equals(Ndef.NFC_FORUM_TYPE_1)) {
        translation = "NFC Forum Type 1";
    } else if (type.equals(Ndef.NFC_FORUM_TYPE_2)) {
        translation = "NFC Forum Type 2";
    } else if (type.equals(Ndef.NFC_FORUM_TYPE_3)) {
        translation = "NFC Forum Type 3";
    } else if (type.equals(Ndef.NFC_FORUM_TYPE_4)) {
        translation = "NFC Forum Type 4";
    } else {
        translation = type;
    }
    return translation;
}