android.nfc.tech.Ndef Java Examples
The following examples show how to use
android.nfc.tech.Ndef.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Tag.java From android_9.0.0_r45 with Apache License 2.0 | 6 votes |
private static HashMap<String, Integer> getTechStringToCodeMap() { HashMap<String, Integer> techStringToCodeMap = new HashMap<String, Integer>(); techStringToCodeMap.put(IsoDep.class.getName(), TagTechnology.ISO_DEP); techStringToCodeMap.put(MifareClassic.class.getName(), TagTechnology.MIFARE_CLASSIC); techStringToCodeMap.put(MifareUltralight.class.getName(), TagTechnology.MIFARE_ULTRALIGHT); techStringToCodeMap.put(Ndef.class.getName(), TagTechnology.NDEF); techStringToCodeMap.put(NdefFormatable.class.getName(), TagTechnology.NDEF_FORMATABLE); techStringToCodeMap.put(NfcA.class.getName(), TagTechnology.NFC_A); techStringToCodeMap.put(NfcB.class.getName(), TagTechnology.NFC_B); techStringToCodeMap.put(NfcF.class.getName(), TagTechnology.NFC_F); techStringToCodeMap.put(NfcV.class.getName(), TagTechnology.NFC_V); techStringToCodeMap.put(NfcBarcode.class.getName(), TagTechnology.NFC_BARCODE); return techStringToCodeMap; }
Example #2
Source File: NfcHandler.java From PowerSwitch_Android with GNU General Public License v3.0 | 6 votes |
/** * 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"); } } }
Example #3
Source File: MainActivity.java From android-nfc-tag-read-write with MIT License | 6 votes |
@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); } } } }
Example #4
Source File: NFCWriteFragment.java From android-nfc-tag-read-write with MIT License | 6 votes |
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); } } }
Example #5
Source File: WriteUriActivity.java From android-nfc with MIT License | 6 votes |
/** * 写入标签 * * @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; }
Example #6
Source File: NfcTagHandler.java From 365browser with Apache License 2.0 | 5 votes |
/** * 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; }
Example #7
Source File: NFCReader.java From DeviceConnect-Android with MIT License | 5 votes |
/** * Ndef の情報を読み込みます. * * @param tag タグ * @param tagInfo 情報を格納するクラス * @throws FormatException NFCタグのフォーマットが不正な場合に発生 */ private void readNdef(final Tag tag, final TagInfo tagInfo) throws FormatException { try { Ndef ndef = Ndef.get(tag); ndef.connect(); readNdefMessage(ndef.getNdefMessage(), tagInfo.getList()); } catch (IOException e) { // ignore. } }
Example #8
Source File: NFCTagReadWriteManager.java From PhoneProfilesPlus with Apache License 2.0 | 5 votes |
private void readTagFromIntent(Intent intent) { if (intent != null){ String action = intent.getAction(); /*if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) { uidRead = true; String uid = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)); onTagReadListener.onUidRead(uid); }*/ if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) { tagRead = 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[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); if (rawMessages != null) { NdefRecord[] records = ((NdefMessage) rawMessages[0]).getRecords(); String text = ndefRecordToString(records[0]); onTagReadListener.onTagRead(text); } } } } }
Example #9
Source File: NFCReadFragment.java From android-nfc-tag-read-write with MIT License | 5 votes |
private void readFromNFC(Ndef ndef) { try { ndef.connect(); NdefMessage ndefMessage = ndef.getNdefMessage(); String message = new String(ndefMessage.getRecords()[0].getPayload()); Log.d(TAG, "readFromNFC: "+message); mTvMessage.setText(message); ndef.close(); } catch (IOException | FormatException e) { e.printStackTrace(); } }
Example #10
Source File: NfcUtils.java From WiFiKeyShare with GNU General Public License v3.0 | 5 votes |
public static WifiConfiguration readTag(Tag tag) { Ndef ndef = Ndef.get(tag); if (ndef == null) { Log.d(TAG, "NDEF not supported"); return null; } NdefMessage ndefMessage = ndef.getCachedNdefMessage(); if (ndefMessage == null) { Log.d(TAG, "ndefMessage is null"); return null; } return NfcUtils.parse(ndefMessage); }
Example #11
Source File: NfcActivity.java From android-nfc-lib with MIT License | 5 votes |
/** * 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()}}; }
Example #12
Source File: BaseActivity.java From xmrwallet with Apache License 2.0 | 5 votes |
void writeNdef(Ndef ndef, Uri uri) throws IOException, FormatException { NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); if (nfcAdapter == null) return; // no NFC support here NdefRecord recordNFC = NdefRecord.createUri(uri); NdefMessage message = new NdefMessage(recordNFC); ndef.connect(); int tagSize = ndef.getMaxSize(); int msgSize = message.getByteArrayLength(); Timber.d("tagSize=%d, msgSIze=%d, uriSize=%d", tagSize, msgSize, uri.toString().length()); if (tagSize < msgSize) throw new IllegalArgumentException(getString(R.string.nfc_tag_size, tagSize, msgSize)); ndef.writeNdefMessage(message); }
Example #13
Source File: NdefWriteImpl.java From android-nfc-lib with MIT License | 5 votes |
/** * {@inheritDoc} */ @Override public boolean writeToNdefAndMakeReadonly(NdefMessage message, Ndef ndef) throws ReadOnlyTagException, InsufficientCapacityException, FormatException { setReadOnly(true); boolean result = writeToNdef(message, ndef); setReadOnly(false); return result; }
Example #14
Source File: ReadTextActivity.java From android-nfc with MIT License | 5 votes |
@Override public void onNewIntent(Intent intent) { //1.获取Tag对象 Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); //2.获取Ndef的实例 Ndef ndef = Ndef.get(detectedTag); mTagText = ndef.getType() + "\nmaxsize:" + ndef.getMaxSize() + "bytes\n\n"; readNfcTag(intent); mNfcText.setText(mTagText); }
Example #15
Source File: ReadUriActivity.java From android-nfc with MIT License | 5 votes |
@Override public void onNewIntent(Intent intent) { //获取Tag对象 Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); //获取Ndef的实例 Ndef ndef = Ndef.get(detectedTag); mTagText = ndef.getType() + "\n max size:" + ndef.getMaxSize() + " bytes\n\n"; readNfcTag(intent); mNfcText.setText(mTagText); }
Example #16
Source File: WriteTextActivity.java From android-nfc with MIT License | 5 votes |
/** * 写数据 * * @param ndefMessage 创建好的NDEF文本数据 * @param tag 标签 * @return */ public static boolean writeTag(NdefMessage ndefMessage, Tag tag) { try { Ndef ndef = Ndef.get(tag); ndef.connect(); ndef.writeNdefMessage(ndefMessage); return true; } catch (Exception e) { } return false; }
Example #17
Source File: WriteUtilityImpl.java From android-nfc-lib with MIT License | 5 votes |
@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; }
Example #18
Source File: OtpParser.java From yubikit-android with Apache License 2.0 | 5 votes |
/** * Parses nfc tag and extracts otp credential from it * @param tag an NDEF compatible tag * @return OTP data * @throws ParseTagException if tag has no NDEF Tag Technology or there is no YK OTP payload */ public static @NonNull String parseTag(Tag tag) throws ParseTagException { Ndef ndef = Ndef.get(tag); if (ndef == null) { throw new ParseTagException("Tag is not NDEF formatted"); } NdefMessage message; try { ndef.connect(); message = ndef.getNdefMessage(); } catch (FormatException | IOException e) { message = ndef.getCachedNdefMessage(); } finally { try { ndef.close(); } catch (IOException ignore) { } } if (message == null) { throw new ParseTagException("Couldn't read ndef message"); } String parsedData = parseNdefMessage(message); if (parsedData != null) { return parsedData; } throw new ParseTagException("Tag doesn't have YK OTP payload"); }
Example #19
Source File: NfcSensor.java From Cardboard with Apache License 2.0 | 4 votes |
public void onNfcIntent(Intent intent) { if ((!isNfcEnabled()) || (intent == null) || (!"android.nfc.action.NDEF_DISCOVERED".equals(intent .getAction()))) { return; } Uri uri = intent.getData(); Tag nfcTag = (Tag) intent.getParcelableExtra("android.nfc.extra.TAG"); if ((uri == null) || (nfcTag == null)) { return; } Ndef ndef = Ndef.get(nfcTag); if ((ndef == null) || (!uri.getScheme().equals("cardboard")) || ((!uri.getHost().equals("v1.0.0")) && (uri.getPathSegments() .size() == 2))) { return; } synchronized (this.mTagLock) { boolean isSameTag = false; if (this.mCurrentTag != null) { byte[] tagId1 = nfcTag.getId(); byte[] tagId2 = this.mCurrentTag.getTag().getId(); isSameTag = (tagId1 != null) && (tagId2 != null) && (Arrays.equals(tagId1, tagId2)); closeCurrentNfcTag(); if (!isSameTag) { sendDisconnectionEvent(); } } NdefMessage nfcTagContents; try { ndef.connect(); nfcTagContents = ndef.getCachedNdefMessage(); } catch (Exception e) { Log.e("NfcSensor", "Error reading NFC tag: " + e.toString()); if (isSameTag) { sendDisconnectionEvent(); } return; } this.mCurrentTag = ndef; if (!isSameTag) { synchronized (this.mListeners) { for (ListenerHelper listener : this.mListeners) { listener.onInsertedIntoCardboard(CardboardDeviceParams .createFromNfcContents(nfcTagContents)); } } } this.mTagConnectionFailures = 0; this.mNfcDisconnectTimer = new Timer("NFC disconnect timer"); this.mNfcDisconnectTimer.schedule(new TimerTask() { public void run() { synchronized (NfcSensor.this.mTagLock) { if (!NfcSensor.this.mCurrentTag.isConnected()) { NfcSensor.access$204(NfcSensor.this); if (NfcSensor.this.mTagConnectionFailures > 1) { NfcSensor.this.closeCurrentNfcTag(); NfcSensor.this.sendDisconnectionEvent(); } } } } }, 250L, 250L); } }
Example #20
Source File: WriteUtilityImpl.java From android-nfc-lib with MIT License | 4 votes |
@Override public boolean writeToNdef(NdefMessage message, Ndef ndef) throws ReadOnlyTagException, InsufficientCapacityException, FormatException { return mNdefWrite.writeToNdef(message, ndef); }
Example #21
Source File: WriteUtilityImpl.java From android-nfc-lib with MIT License | 4 votes |
@Override public boolean writeToNdefAndMakeReadonly(NdefMessage message, Ndef ndef) throws ReadOnlyTagException, InsufficientCapacityException, FormatException { return mNdefWrite.writeToNdefAndMakeReadonly(message, ndef); }
Example #22
Source File: NFCWriteFragment.java From android-nfc-tag-read-write with MIT License | 4 votes |
public void onNfcDetected(Ndef ndef, String messageToWrite){ mProgress.setVisibility(View.VISIBLE); writeToNfc(ndef,messageToWrite); }
Example #23
Source File: NfcTagHandler.java From 365browser with Apache License 2.0 | 4 votes |
NdefHandler(Ndef ndef) { mNdef = ndef; }
Example #24
Source File: Tag.java From android_9.0.0_r45 with Apache License 2.0 | 4 votes |
private String[] generateTechStringList(int[] techList) { final int size = techList.length; String[] strings = new String[size]; for (int i = 0; i < size; i++) { switch (techList[i]) { case TagTechnology.ISO_DEP: strings[i] = IsoDep.class.getName(); break; case TagTechnology.MIFARE_CLASSIC: strings[i] = MifareClassic.class.getName(); break; case TagTechnology.MIFARE_ULTRALIGHT: strings[i] = MifareUltralight.class.getName(); break; case TagTechnology.NDEF: strings[i] = Ndef.class.getName(); break; case TagTechnology.NDEF_FORMATABLE: strings[i] = NdefFormatable.class.getName(); break; case TagTechnology.NFC_A: strings[i] = NfcA.class.getName(); break; case TagTechnology.NFC_B: strings[i] = NfcB.class.getName(); break; case TagTechnology.NFC_F: strings[i] = NfcF.class.getName(); break; case TagTechnology.NFC_V: strings[i] = NfcV.class.getName(); break; case TagTechnology.NFC_BARCODE: strings[i] = NfcBarcode.class.getName(); break; default: throw new IllegalArgumentException("Unknown tech type " + techList[i]); } } return strings; }
Example #25
Source File: AppRunnerActivity.java From PHONK with GNU General Public License v3.0 | 4 votes |
/** * Listen to NFC incomming data */ @Override public void onNewIntent(Intent intent) { MLog.d(TAG, "New intent " + intent); if (intent.getAction() != null) { MLog.d(TAG, "Discovered tag with intent: " + intent); Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); String nfcID = StrUtils.bytetostring(tag.getId()); // if there is a message waiting to be written if (PNfc.nfcMsg != null) { MLog.d(TAG, "->" + PNfc.nfcMsg); PNfc.writeTag(this, tag, PNfc.nfcMsg); if (onNFCWrittenListener != null) onNFCWrittenListener.onNewTag(); onNFCWrittenListener = null; PNfc.nfcMsg = null; // read the nfc tag info } else { // get NDEF tag details Ndef ndefTag = Ndef.get(tag); if (ndefTag == null) { return; } int size = ndefTag.getMaxSize(); // tag size boolean writable = ndefTag.isWritable(); // is tag writable? String type = ndefTag.getType(); // tag type String nfcMessage = ""; // get NDEF message details NdefMessage ndefMesg = ndefTag.getCachedNdefMessage(); if (ndefMesg != null) { NdefRecord[] ndefRecords = ndefMesg.getRecords(); int len = ndefRecords.length; String[] recTypes = new String[len]; // will contain the // NDEF record types String[] recPayloads = new String[len]; // will contain the // NDEF record types for (int i = 0; i < len; i++) { recTypes[i] = new String(ndefRecords[i].getType()); recPayloads[i] = new String(ndefRecords[i].getPayload()); } nfcMessage = recPayloads[0]; } if (onNFCListener != null) onNFCListener.onNewTag(nfcID, nfcMessage); } } }
Example #26
Source File: BaseActivity.java From xmrwallet with Apache License 2.0 | 4 votes |
AsyncWriteTag(Ndef ndef, Uri uri) { this.ndef = ndef; this.uri = uri; }
Example #27
Source File: RunUrlActivity.java From android-nfc with MIT License | 4 votes |
/** * 往标签写数据的方法 * * @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) { } }
Example #28
Source File: RunAppActivity.java From android-nfc with MIT License | 4 votes |
/** * 往标签写数据的方法 * * @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) { } }
Example #29
Source File: NfcUtil.java From zap-android with MIT License | 4 votes |
public static String[][] TechFilters() { return new String[][]{new String[]{Ndef.class.getName()}}; }
Example #30
Source File: NFCHandler.java From timelapse-sony with GNU General Public License v3.0 | 3 votes |
public static Pair<String, String> getCameraWifiSettingsFromTag(Tag tag, Parcelable[] messages) throws Exception { Ndef ndef = Ndef.get(tag); ndef.connect(); NdefRecord record = ((NdefMessage) messages[0]).getRecords()[0]; Pair<String, String> cameraWifiSettings = decodeSonyPPMMessage(record); ndef.close(); return cameraWifiSettings; }