android.nfc.Tag Java Examples

The following examples show how to use android.nfc.Tag. 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: WriteMUActivity.java    From android-nfc with MIT License 6 votes vote down vote up
@Override
public void onNewIntent(Intent intent) {
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    String[] techList = tag.getTechList();
    boolean haveMifareUltralight = false;
    for (String tech : techList) {
        if (tech.indexOf("MifareUltralight") >= 0) {
            haveMifareUltralight = true;
            break;
        }
    }
    if (!haveMifareUltralight) {
        Toast.makeText(this, "不支持MifareUltralight数据格式", Toast.LENGTH_SHORT).show();
        return;
    }
    writeTag(tag);
}
 
Example #2
Source File: WriteUriActivity.java    From android-nfc with MIT License 6 votes vote down vote up
/**
 * 写入标签
 *
 * @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 #3
Source File: TagDispatcher.java    From nordpol with MIT License 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
private void enableReaderMode(NfcAdapter adapter) {
    Bundle options = new Bundle();
    if(broadcomWorkaround) {
        /* This is a work around for some Broadcom chipsets that does
         * the presence check by sending commands that interrupt the
         * processing of the ongoing command.
         */
        options.putInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY, DELAY_PRESENCE);
    }
    NfcAdapter.ReaderCallback callback = new NfcAdapter.ReaderCallback() {
            public void onTagDiscovered(Tag tag) {
                dispatchTag(tag);
            }
        };
    int flags = NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_NFC_B;
    if(disableSounds) {
        flags = flags | NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS;
    }
    if(disableNdefCheck) {
        flags = flags | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK;
    }
    adapter.enableReaderMode(activity, callback, flags, options);
}
 
Example #4
Source File: ReadMUActivity.java    From android-nfc with MIT License 6 votes vote down vote up
@Override
public void onNewIntent(Intent intent) {
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    String[] techList = tag.getTechList();
    boolean haveMifareUltralight = false;
    for (String tech : techList) {
        if (tech.indexOf("MifareUltralight") >= 0) {
            haveMifareUltralight = true;
            break;
        }
    }
    if (!haveMifareUltralight) {
        Toast.makeText(this, "不支持MifareUltralight数据格式", Toast.LENGTH_SHORT).show();
        return;
    }
    String data = readTag(tag);
    if (data != null)
        Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
}
 
Example #5
Source File: TagDispatcher.java    From nordpol with MIT License 6 votes vote down vote up
private void dispatchTag(final Tag tag) {
if(dispatchOnUiThread) {
    if(Looper.myLooper() != Looper.getMainLooper()) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    tagDiscoveredListener.tagDiscovered(tag);
                }
            });
    } else {
        tagDiscoveredListener.tagDiscovered(tag);
    }

} else {
    new AsyncTask<Void, Void, Void>() {
 
Example #6
Source File: NfcReaderDispatcher.java    From yubikit-android with Apache License 2.0 6 votes vote down vote up
/**
 * Start intercepting nfc events
 * @param activity activity that is going to receive nfc events
 * Note: invoke that while activity is in foreground
 */
private void enableReaderMode(Activity activity, final NfcConfiguration nfcConfiguration) {
    NfcAdapter.ReaderCallback callback = new NfcAdapter.ReaderCallback() {
        public void onTagDiscovered(Tag tag) {
            handler.onTag(tag);
        }
    };
    Bundle options = new Bundle();
    options.putInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY, 50);
    int READER_FLAGS = NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_NFC_B;
    if (nfcConfiguration.isDisableNfcDiscoverySound()) {
        READER_FLAGS |= NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS;
    }

    if (nfcConfiguration.isSkipNdefCheck()) {
        READER_FLAGS |= NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK;
    }
    adapter.enableReaderMode(activity, callback, READER_FLAGS, options);
}
 
Example #7
Source File: RequestLoginActivity.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void tagDiscovered(final Tag t) {
    Log.d(TAG, "tagDiscovered " + t);
    mTag = t;
    if (mTransportFuture == null)
        return;

    final BTChipTransport transport = getTransport(t);
    if (transport == null)
        return;

    if (mTransportFuture.set(transport)) {
        if (mNfcWaitDialog == null)
            return;

        runOnUiThread(new Runnable() { public void run() { mNfcWaitDialog.hide(); } });
    }
}
 
Example #8
Source File: NFCReaderX.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void tagFound(Activity context, Intent data) {

        if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(data.getAction())) {
            Tag tag = data.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            doTheScan(context, tag, true);
        }
    }
 
Example #9
Source File: Abbott.java    From FreeStyleLibre-NFC-Reader with GNU General Public License v3.0 5 votes vote down vote up
private void handleIntent(Intent intent) {
    String action = intent.getAction();
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {

        Log.d("socialdiabetes", "NfcAdapter.ACTION_TECH_DISCOVERED");
        // In case we would still use the Tech Discovered Intent
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        String[] techList = tag.getTechList();
        String searchedTech = NfcV.class.getName();
        new NfcVReaderTask().execute(tag);

    }
}
 
Example #10
Source File: NFCReaderX.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
private static synchronized void doTheScan(final Activity context, Tag tag, boolean showui) {
    synchronized (tag_lock) {
        if (!tag_discovered) {
            if (!useNFC()) return;
            if ((!last_read_succeeded) && (JoH.ratelimit("nfc-debounce", 5)) || (JoH.ratelimit("nfc-debounce", 60))) {
                tag_discovered = true;
                Home.staticBlockUI(context, true);
                last_tag_discovered = JoH.tsl();
                if (showui) {
                    context.startActivity(new Intent(context, NFCScanningX.class));
                } else {
                    NFCReaderX.vibrate(context, 0);
                    JoH.static_toast_short(gs(R.string.scanning));
                }
                if (d)
                    Log.d(TAG, "NFC tag discovered - going to read data");
                new NfcVReaderTask(context).executeOnExecutor(xdrip.executor, tag);
            } else {
                if (JoH.tsl() - last_tag_discovered > 5000) {
                    vibrate(context, 4);
                    JoH.static_toast_short(gs(R.string.not_so_quickly_wait_60_seconds));
                }
            }
        } else {
            Log.d(TAG, "Tag already discovered!");
            if (JoH.tsl() - last_tag_discovered > 60000)
                tag_discovered = false; // don't lock too long
        }
    } // lock
}
 
Example #11
Source File: NfcTagHandler.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #12
Source File: NFCReaderX.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void tagFound(Activity context, Intent data) {

        if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(data.getAction())) {
            Tag tag = data.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            doTheScan(context, tag, true);
        }
    }
 
Example #13
Source File: NfcManager.java    From NFCard with GNU General Public License v3.0 5 votes vote down vote up
public boolean readCard(Intent intent, ReaderListener listener) {
	final Tag tag = (Tag) intent.getParcelableExtra(EXTRA_TAG);
	if (tag != null) {
		ReaderManager.readCard(tag, listener);
		return true;
	}
	return false;
}
 
Example #14
Source File: TagDispatcher.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public boolean interceptIntent(Intent intent) {
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    if(tag != null) {
        listener.tagDiscovered(tag);
        return true;
    } else {
        return false;
    }
}
 
Example #15
Source File: WriteGeolocationSucceedsTests.java    From android-nfc-lib with MIT License 5 votes vote down vote up
boolean writeGeoLocation(Double latitude, Double longitude, String technology, boolean readonly) throws IllegalAccessException, NoSuchFieldException, ClassNotFoundException, InsufficientCapacityException, FormatException, ReadOnlyTagException, TagNotPresentException {
    final Tag mockTag = mTestUtilities.mockTag(technology);

    final Intent intent = new Intent().putExtra(NfcAdapter.EXTRA_TAG, mockTag);
    NfcWriteUtility nfcMessageUtility = mTestUtilities.determineMockType(technology);

    return (readonly ? nfcMessageUtility.makeOperationReadOnly().writeGeolocationToTagFromIntent(latitude, longitude, intent) : nfcMessageUtility.writeGeolocationToTagFromIntent(latitude, longitude, intent));
}
 
Example #16
Source File: ZannenNfcWriter.java    From effective_android_sample with Apache License 2.0 5 votes vote down vote up
@Override
protected void onNewIntent(Intent intent) {
    // NDEF exchange mode
    if (!mWriteMode && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        NdefMessage[] msgs = getNdefMessages(intent);
        promptForContent(msgs[0]);
    }

    // Tag writing mode
    if (mWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        writeTag(getNoteAsNdef(), detectedTag);
    }
}
 
Example #17
Source File: NFCReader.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * 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 #18
Source File: WriteUriFailsTests.java    From android-nfc-lib with MIT License 5 votes vote down vote up
boolean writeUri(String uri, String technology, boolean readonly) throws IllegalAccessException, NoSuchFieldException, ClassNotFoundException, InsufficientCapacityException, FormatException, ReadOnlyTagException, TagNotPresentException {
    final Tag mockTag = mTestUtilities.mockTag(technology);
    final Intent intent = new Intent().putExtra(NfcAdapter.EXTRA_TAG, mockTag);
    NfcWriteUtility nfcWriteUtility = mTestUtilities.determineMockType(technology);

    return writeUriCustomHeader(uri, technology, NfcPayloadHeader.HTTP_WWW, false);
}
 
Example #19
Source File: PGPClipperResultShowActivity.java    From PGPClipper with Apache License 2.0 5 votes vote down vote up
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    if (Objects.equals(intent.getAction(), NfcAdapter.ACTION_TAG_DISCOVERED)) {
        final Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

        tryNfcDecryption(NFCEncryptionUtils.byteArrayToHex(tag.getId()));
        waitingNFC = false;
        disableTagReading(adapter);
    }
}
 
Example #20
Source File: NFCBaseActivity.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * 起動用 Intent から NFC タグを取得します.
 *
 * @return NFCタグ
 */
Tag getTagFromNfc() {
    Intent intent = getIntent();
    if (intent != null) {
        return intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    } else {
        return null;
    }
}
 
Example #21
Source File: NfcWriteUtilityImpl.java    From android-nfc-lib with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean writeEmailToTagFromIntent(@NotNull String recipient, String subject, String message, @NotNull Intent intent) throws FormatException, ReadOnlyTagException, InsufficientCapacityException, TagNotPresentException {
    final NdefMessage ndefMessage = mNfcMessageUtility.createEmail(recipient, subject, message);
    final Tag tag = retrieveTagFromIntent(intent);
    return writeToTag(ndefMessage, tag);
}
 
Example #22
Source File: NFCReaderX.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
private static synchronized void doTheScan(final Activity context, Tag tag, boolean showui) {
    synchronized (tag_lock) {
        if (!tag_discovered) {
            if (!useNFC()) return;
            if ((!last_read_succeeded) && (JoH.ratelimit("nfc-debounce", 5)) || (JoH.ratelimit("nfc-debounce", 60))) {
                tag_discovered = true;
                Home.staticBlockUI(context, true);
                last_tag_discovered = JoH.tsl();
                if (showui) {
                    context.startActivity(new Intent(context, NFCScanningX.class));
                } else {
                    NFCReaderX.vibrate(context, 0);
                    JoH.static_toast_short(gs(R.string.scanning));
                }
                if (d)
                    Log.d(TAG, "NFC tag discovered - going to read data");
                new NfcVReaderTask(context).executeOnExecutor(xdrip.executor, tag);
            } else {
                if (JoH.tsl() - last_tag_discovered > 5000) {
                    vibrate(context, 4);
                    JoH.static_toast_short(gs(R.string.not_so_quickly_wait_60_seconds));
                }
            }
        } else {
            Log.d(TAG, "Tag already discovered!");
            if (JoH.tsl() - last_tag_discovered > 60000)
                tag_discovered = false; // don't lock too long
        }
    } // lock
}
 
Example #23
Source File: NFCReaderX.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static void tagFound(Activity context, Intent data) {

        if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(data.getAction())) {
            Tag tag = data.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            doTheScan(context, tag, true);
        }
    }
 
Example #24
Source File: NFCReaderX.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static void tagFound(Activity context, Intent data) {

        if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(data.getAction())) {
            Tag tag = data.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            doTheScan(context, tag, true);
        }
    }
 
Example #25
Source File: NFCReaderX.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
private static synchronized void doTheScan(final Activity context, Tag tag, boolean showui) {
    synchronized (tag_lock) {
        if (!tag_discovered) {
            if (!useNFC()) return;
            if ((!last_read_succeeded) && (JoH.ratelimit("nfc-debounce", 5)) || (JoH.ratelimit("nfc-debounce", 60))) {
                tag_discovered = true;
                Home.staticBlockUI(context, true);
                last_tag_discovered = JoH.tsl();
                if (showui) {
                    context.startActivity(new Intent(context, NFCScanningX.class));
                } else {
                    NFCReaderX.vibrate(context, 0);
                    JoH.static_toast_short(gs(R.string.scanning));
                }
                if (d)
                    Log.d(TAG, "NFC tag discovered - going to read data");
                new NfcVReaderTask(context).executeOnExecutor(xdrip.executor, tag);
            } else {
                if (JoH.tsl() - last_tag_discovered > 5000) {
                    vibrate(context, 4);
                    JoH.static_toast_short(gs(R.string.not_so_quickly_wait_60_seconds));
                }
            }
        } else {
            Log.d(TAG, "Tag already discovered!");
            if (JoH.tsl() - last_tag_discovered > 60000)
                tag_discovered = false; // don't lock too long
        }
    } // lock
}
 
Example #26
Source File: NFCWriterActivity.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * NFC に書き込むを確認するダイアログを表示します.
 *
 * @param tag 書き込み先のNFCタグ
 */
private void openWriteTag(final Tag tag) {
    runOnUiThread(() -> new AlertDialog.Builder(NFCWriterActivity.this)
            .setTitle(R.string.activity_nfc_write_dialog_title)
            .setMessage(R.string.activity_nfc_write_dialog_message)
            .setPositiveButton(R.string.activity_nfc_setting_error_btn_ok, (dialogInterface, i) -> {
                try {
                    writeTag(tag);
                    postTagWriterActivityResult(TagConstants.RESULT_SUCCESS);
                } catch (NFCWriteException e) {
                    switch (e.getCode()) {
                        default:
                        case IO_ERROR:
                            postTagWriterActivityResult(TagConstants.RESULT_FAILED);
                            break;
                        case INVALID_FORMAT:
                            postTagWriterActivityResult(TagConstants.RESULT_INVALID_FORMAT);
                            break;
                        case NOT_WRITABLE:
                            postTagWriterActivityResult(TagConstants.RESULT_NOT_WRIATEBLE);
                            break;
                    }
                }
            })
            .setNegativeButton(R.string.activity_nfc_setting_error_btn_cancel, (dialogInterface, i) -> finish())
            .show());
}
 
Example #27
Source File: WriteUriActivity.java    From android-nfc with MIT License 5 votes vote down vote up
public void onNewIntent(Intent intent) {
    Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    NdefMessage ndefMessage = new NdefMessage(new NdefRecord[]{createUriRecord(mUri)});
    boolean result = writeTag(ndefMessage, detectedTag);
    if (result) {
        Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show();
    }
}
 
Example #28
Source File: NfcBankomatCardReader.java    From bankomatinfos with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Constructor
 * 
 * @param _nfcTag
 */
public NfcBankomatCardReader(Tag nfcTag, Context ctx) {
	super();
	this._nfcTag = nfcTag;
	this._ctl = AppController.getInstance();
	this._tagList = new ArrayList<TagAndValue>();
	this._ctx = ctx;
}
 
Example #29
Source File: WriteTextActivity.java    From android-nfc with MIT License 5 votes vote down vote up
/**
 * 写数据
 *
 * @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 #30
Source File: WriteTextActivity.java    From android-nfc with MIT License 5 votes vote down vote up
@Override
public void onNewIntent(Intent intent) {
    if (mText == null)
        return;
    //获取Tag对象
    Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    NdefMessage ndefMessage = new NdefMessage(
            new NdefRecord[]{createTextRecord(mText)});
    boolean result = writeTag(ndefMessage, detectedTag);
    if (result) {
        Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show();
    }
}