private void formatTag(Tag tag, NdefMessage ndefMessage) { try { NdefFormatable ndefFormatable = NdefFormatable.get(tag); if (ndefFormatable == null) { Toast.makeText(this, "Tag is not formatable", Toast.LENGTH_LONG).show(); } ndefFormatable.connect(); ndefFormatable.format(ndefMessage); ndefFormatable.close(); } catch (Exception e) { Log.e("formatTag", e.getMessage()); } }
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); } } }
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(); } } }
/** * 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"); } } }
private void formatTag(Tag tag, NdefMessage ndefMessage) { try{ NdefFormatable ndefFormatable = NdefFormatable.get(tag); if(ndefFormatable == null) { Toast.makeText(this, "Tag is not NdefFormatable!!", Toast.LENGTH_SHORT).show(); return; } ndefFormatable.connect(); ndefFormatable.format(ndefMessage); ndefFormatable.close(); Toast.makeText(this, "Tag Written!! :) ", Toast.LENGTH_SHORT).show(); } catch (Exception e) { Log.e("formatTag", e.getMessage()); } }
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()}}; }
@Override protected Boolean doInBackground(Intent... params) { Intent intent = params[0]; Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); NdefMessage message = this.tag.encode(); if (tag != null) { try { Ndef ndefTag = Ndef.get(tag); if (ndefTag == null) { NdefFormatable ndefForm = NdefFormatable.get(tag); if (ndefForm != null) { ndefForm.connect(); ndefForm.format(message); ndefForm.close(); } } else { ndefTag.connect(); ndefTag.writeNdefMessage(message); ndefTag.close(); } return true; } catch(Exception e) { e.printStackTrace(); } } return false; }
public void enableForegroundDispatch() { Intent nfcIntent = new Intent(activity, getClass()); nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, nfcIntent, 0); IntentFilter[] intentFiltersArray = new IntentFilter[] {}; String[][] techList = new String[][] { { android.nfc.tech.Ndef.class.getName() }, { android.nfc.tech.NdefFormatable.class.getName() } }; adapter.enableForegroundDispatch(activity, pendingIntent, intentFiltersArray, techList); Toast.makeText(activity, "enabled dispatch", Toast.LENGTH_SHORT).show(); }
/** * Factory method that creates NfcTagHandler for a given NFC Tag. * * @param tag @see android.nfc.Tag * @return NfcTagHandler or null when unsupported Tag is provided. */ public static NfcTagHandler create(Tag tag) { if (tag == null) return null; Ndef ndef = Ndef.get(tag); if (ndef != null) return new NfcTagHandler(ndef, new NdefHandler(ndef)); NdefFormatable formattable = NdefFormatable.get(tag); if (formattable != null) { return new NfcTagHandler(formattable, new NdefFormattableHandler(formattable)); } return null; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.writeurl); final EditText urlEditText = (EditText)this.findViewById(R.id.urltext); Button writeUrlButton = (Button)this.findViewById(R.id.writeurlbutton); writeUrlButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { urlAddress = urlEditText.getText().toString(); TextView messageText = (TextView)findViewById(R.id.URL); messageText.setText("Touch NFC Tag to write app to open " +urlAddress); } }); mNfcAdapter = NfcAdapter.getDefaultAdapter(this); mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); //ndef.addDataScheme("http"); mFilters = new IntentFilter[] {ndef,}; mTechLists = new String[][] { new String[] { Ndef.class.getName() }, new String[] { NdefFormatable.class.getName() }}; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.writeurl); final EditText urlEditText = (EditText)this.findViewById(R.id.urltext); Button writeUrlButton = (Button)this.findViewById(R.id.writeurlbutton); writeUrlButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { urlAddress = urlEditText.getText().toString(); TextView messageText = (TextView)findViewById(R.id.URL); messageText.setText("Touch NFC Tag to write http://www."+urlAddress); } }); mNfcAdapter = NfcAdapter.getDefaultAdapter(this); mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); //ndef.addDataScheme("http"); mFilters = new IntentFilter[] {ndef,}; mTechLists = new String[][] { new String[] { Ndef.class.getName() }, new String[] { NdefFormatable.class.getName() }}; }
/** * Initializes which intents and NfcTechnologies to filter for */ private void initFields() { pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); mIntentFilters = new IntentFilter[]{new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED)}; mTechLists = new String[][]{new String[]{Ndef.class.getName()}, new String[]{NdefFormatable.class.getName()}}; }
/** * {@inheritDoc} */ @Override public boolean writeToNdefFormatableAndMakeReadonly(NdefMessage message, NdefFormatable ndefFormat) throws FormatException { setReadOnly(true); boolean result = writeToNdefFormatable(message, ndefFormat); setReadOnly(false); return result; }
@Override public boolean writeToTag(NdefMessage message, Tag tag) throws FormatException, ReadOnlyTagException, InsufficientCapacityException { Ndef ndef = Ndef.get(tag); NdefFormatable formatable = NdefFormatable.get(tag); boolean result; if (readOnly) { result = writeToNdefAndMakeReadonly(message, ndef) || writeToNdefFormatableAndMakeReadonly(message, formatable); } else { result = writeToNdef(message, ndef) || writeToNdefFormatable(message, formatable); } readOnly = false; return result; }
@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if(intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED) || intent.getAction().equals(NfcAdapter.ACTION_TECH_DISCOVERED)){ Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); String[] tagTechs = tag.getTechList(); if (Arrays.asList(tagTechs).contains(Ndef.class.getName())) { writeKeyOnTag(tag, false); } else if (Arrays.asList(tagTechs).contains(NdefFormatable.class.getName())) { writeKeyOnTag(tag, true); } else { Toast.makeText(this, "Tag not supported", Toast.LENGTH_LONG).show(); } } }
/** * 往标签写数据的方法 * * @param tag */ public void writeNFCTag(Tag tag) { if (tag == null) { return; } NdefMessage ndefMessage = new NdefMessage(new NdefRecord[]{NdefRecord .createApplicationRecord(mPackageName)}); //转换成字节获得大小 int size = ndefMessage.toByteArray().length; try { //2.判断NFC标签的数据类型(通过Ndef.get方法) Ndef ndef = Ndef.get(tag); //判断是否为NDEF标签 if (ndef != null) { ndef.connect(); //判断是否支持可写 if (!ndef.isWritable()) { return; } //判断标签的容量是否够用 if (ndef.getMaxSize() < size) { return; } //3.写入数据 ndef.writeNdefMessage(ndefMessage); Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show(); } else { //当我们买回来的NFC标签是没有格式化的,或者没有分区的执行此步 //Ndef格式类 NdefFormatable format = NdefFormatable.get(tag); //判断是否获得了NdefFormatable对象,有一些标签是只读的或者不允许格式化的 if (format != null) { //连接 format.connect(); //格式化并将信息写入标签 format.format(ndefMessage); Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show(); } } } catch (Exception e) { } }
/** * 往标签写数据的方法 * * @param tag */ public void writeNFCTag(Tag tag) { if (tag == null) { return; } NdefMessage ndefMessage = new NdefMessage(new NdefRecord[]{NdefRecord .createUri(Uri.parse("http://www.baidu.com"))}); //转换成字节获得大小 int size = ndefMessage.toByteArray().length; try { //2.判断NFC标签的数据类型(通过Ndef.get方法) Ndef ndef = Ndef.get(tag); //判断是否为NDEF标签 if (ndef != null) { ndef.connect(); //判断是否支持可写 if (!ndef.isWritable()) { return; } //判断标签的容量是否够用 if (ndef.getMaxSize() < size) { return; } //3.写入数据 ndef.writeNdefMessage(ndefMessage); Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show(); } else { //当我们买回来的NFC标签是没有格式化的,或者没有分区的执行此步 //Ndef格式类 NdefFormatable format = NdefFormatable.get(tag); //判断是否获得了NdefFormatable对象,有一些标签是只读的或者不允许格式化的 if (format != null) { //连接 format.connect(); //格式化并将信息写入标签 format.format(ndefMessage); Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show(); } } } catch (Exception e) { } }
private void registerNdefFormatable(CallbackContext callbackContext) { addTechList(new String[]{NdefFormatable.class.getName()}); callbackContext.success(); }
public void handleIntent(Intent intent) { String action = intent.getAction(); if (action == null) return; Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); if (tag == null) { sendError(TAG_READING_ERROR); }else { resetTechnologyFlags(); switch (action) { case NfcAdapter.ACTION_NDEF_DISCOVERED: setCurrentTag(tag); if (!getNdefMaxSize().hasError() && !getTagId().hasError()) { isTagSupported = true; isNdef_Flag = true; displayData(); sendNewTagFrame(); } else { sendError(TAG_READING_ERROR); } break; case NfcAdapter.ACTION_TECH_DISCOVERED: setCurrentTag(tag); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) { String[] techList = tag.getTechList(); for (String tech : techList) { if (!isTagSupported) { if (Ndef.class.getName().equals(tech)) { DataReply maxSize = getNdefMaxSize(); if (!maxSize.hasError() && !getTagId().hasError()) { isNdef_Flag = false; isTagSupported = true; DataReply recordCount = getNdefRecordCount(); if (recordCount.getIntegerData() > 0 && recordCount.getIntegerData() < 256) { isNdef_Flag = true; displayData(); sendNewTagFrame(); }else if (recordCount.getIntegerData() == 0){ sendNewEmptyTagFrame(); }else if(recordCount.getError() != 0){ sendError(recordCount.getError()); } } else if (maxSize.hasError()){ sendError(maxSize.getError()); } } else if (NdefFormatable.class.getName().equals(tech)) { /*isNdef_Flag = false; isTagSupported = true; if (!getNdefMaxSize().hasError()&& !getTagId().hasError()) { sendNewEmptyTagFrame(); displayData(); } else { sendError(TAG_READING_ERROR); }*/ } } else break; } if (!isTagSupported) sendError(TAG_NOT_SUPPORTED); } break; case NfcAdapter.ACTION_TAG_DISCOVERED: setCurrentTag(tag); break; } } }
private void formatTag(Tag tag, NdefMessage ndefMessage) { try { NdefFormatable ndefFormatable = NdefFormatable.get(tag); if (ndefFormatable == null) { Toast.makeText(this, "Tag is not ndef formatable!", Toast.LENGTH_SHORT).show(); return; } ndefFormatable.connect(); ndefFormatable.format(ndefMessage); ndefFormatable.close(); Toast.makeText(this, "Tag writen!", Toast.LENGTH_SHORT).show(); } catch (Exception e) { Log.e("formatTag", e.getMessage()); } }
NdefFormattableHandler(NdefFormatable ndefFormattable) { mNdefFormattable = ndefFormattable; }
private void writeNFCTag(Tag tag){ if(tag == null){ return; } NdefMessage ndefMessage = new NdefMessage( new NdefRecord[]{NdefRecord.createUri(Uri.parse("http://www.baidu.com"))} ); int size = ndefMessage.toByteArray().length; try { Ndef ndef = Ndef.get(tag); //���ж�һ�������ǩ�Dz���NDEF�� if(ndef != null){ //�����NDEF��ʽ�� ndef.connect(); //�����ж������ǩ�Ƿ��ǿ�д�� if( ! ndef.isWritable()){ //����Dz���д�ģ�ֱ�ӾͿ��Խ����� Toast.makeText(this , "��NFC��ǩ����д!" , Toast.LENGTH_SHORT).show(); return; } //�����жϵ�ǰ��ǩ����������Ƿ���װ������Ҫд�����Ϣ if(ndef.getMaxSize() < size){ Toast.makeText(this , "��NFC��ǩ������д����̫С!" , Toast.LENGTH_SHORT).show(); return; } //����Ϊֹ���Ϳ��Է��ĵİѶ���д��NFC��ǩ���� ndef.writeNdefMessage(ndefMessage); Toast.makeText(this , "NFC��ǩд�����ݳɹ�" , Toast.LENGTH_SHORT).show(); } else{ //�������NDEF��ʽ�� //���Խ������NDEF��ǩ��ʽ����NDEF��ʽ�� NdefFormatable format = NdefFormatable.get(tag); //��Ϊ��Щ��ǩ��ֻ���ģ�����������Ҫ�ж�һ�� //���format��Ϊnull����ʾ�����ǩ�ǿ��Խ��ܸ�ʽ���� if(format != null){ format.connect(); format.format(ndefMessage); //ͬʱ����˸�ʽ����д����Ϣ�IJ��� Toast.makeText(this , "NFC��ǩ��ʽ��д��ɹ�" , Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(this , "��NFC��ǩ������ʽ��" , Toast.LENGTH_SHORT).show(); } } } catch (Exception e) { Toast.makeText(this , "����ȡ��NFC��ǩ" , Toast.LENGTH_SHORT).show(); } }
@Override public boolean writeToNdefFormatable(NdefMessage message, NdefFormatable ndefFormatable) throws FormatException { return mNdefWrite.writeToNdefFormatable(message, ndefFormatable); }
@Override public boolean writeToNdefFormatableAndMakeReadonly(NdefMessage message, NdefFormatable ndefFormat) throws FormatException { return mNdefWrite.writeToNdefFormatableAndMakeReadonly(message, ndefFormat); }
/** * Write the message to an NdefFormatable * @param message to write * @param ndefFormatable to write to * @return true if success, false if ndefFormatable == null || message == null * @throws FormatException */ boolean writeToNdefFormatable(NdefMessage message, NdefFormatable ndefFormatable) throws FormatException;
/** * Write the message to an NdefFormatable and make readonly * @see NdefWrite#writeToNdefFormatable(android.nfc.NdefMessage, android.nfc.tech.NdefFormatable) */ boolean writeToNdefFormatableAndMakeReadonly(NdefMessage message, NdefFormatable ndefFormat) throws FormatException;