Search in sources :

Example 16 with NdefMessage

use of android.nfc.NdefMessage in project weiciyuan by qii.

the class AbstractAppActivity method initNFC.

private void initNFC() {
    NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter == null) {
        return;
    }
    mNfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() {

        @Override
        public NdefMessage createNdefMessage(NfcEvent event) {
            String text = (GlobalContext.getInstance().getCurrentAccountName());
            NdefMessage msg = new NdefMessage(new NdefRecord[] { createMimeRecord("application/org.qii.weiciyuan.beam", text.getBytes()), NdefRecord.createApplicationRecord(getPackageName()) });
            return msg;
        }
    }, this);
}
Also used : NdefRecord(android.nfc.NdefRecord) NfcEvent(android.nfc.NfcEvent) NdefMessage(android.nfc.NdefMessage) NfcAdapter(android.nfc.NfcAdapter)

Example 17 with NdefMessage

use of android.nfc.NdefMessage in project android_frameworks_base by ParanoidAndroid.

the class Ndef method getNdefMessage.

/**
     * Read the current {@link android.nfc.NdefMessage} on this tag.
     *
     * <p>This always reads the current NDEF Message stored on the tag.
     *
     * <p>Note that this method may return null if the tag was in the
     * INITIALIZED state as defined by NFC Forum, as in that state the
     * tag is formatted to support NDEF but does not contain a message yet.
     *
     * <p>This is an I/O operation and will block until complete. It must
     * not be called from the main application thread. A blocked call will be canceled with
     * {@link IOException} if {@link #close} is called from another thread.
     *
     * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
     *
     * @return the NDEF Message, can be null
     * @throws TagLostException if the tag leaves the field
     * @throws IOException if there is an I/O failure, or the operation is canceled
     * @throws FormatException if the NDEF Message on the tag is malformed
     */
public NdefMessage getNdefMessage() 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)) {
            NdefMessage msg = tagService.ndefRead(serviceHandle);
            if (msg == null && !tagService.isPresent(serviceHandle)) {
                throw new TagLostException();
            }
            return msg;
        } else {
            return null;
        }
    } catch (RemoteException e) {
        Log.e(TAG, "NFC service dead", e);
        return null;
    }
}
Also used : INfcTag(android.nfc.INfcTag) NdefMessage(android.nfc.NdefMessage) IOException(java.io.IOException) RemoteException(android.os.RemoteException) TagLostException(android.nfc.TagLostException)

Example 18 with NdefMessage

use of android.nfc.NdefMessage in project platform_frameworks_base by android.

the class Ndef method getNdefMessage.

/**
     * Read the current {@link android.nfc.NdefMessage} on this tag.
     *
     * <p>This always reads the current NDEF Message stored on the tag.
     *
     * <p>Note that this method may return null if the tag was in the
     * INITIALIZED state as defined by NFC Forum, as in that state the
     * tag is formatted to support NDEF but does not contain a message yet.
     *
     * <p>This is an I/O operation and will block until complete. It must
     * not be called from the main application thread. A blocked call will be canceled with
     * {@link IOException} if {@link #close} is called from another thread.
     *
     * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
     *
     * @return the NDEF Message, can be null
     * @throws TagLostException if the tag leaves the field
     * @throws IOException if there is an I/O failure, or the operation is canceled
     * @throws FormatException if the NDEF Message on the tag is malformed
     */
public NdefMessage getNdefMessage() 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)) {
            NdefMessage msg = tagService.ndefRead(serviceHandle);
            if (msg == null && !tagService.isPresent(serviceHandle)) {
                throw new TagLostException();
            }
            return msg;
        } else if (!tagService.isPresent(serviceHandle)) {
            throw new TagLostException();
        } else {
            return null;
        }
    } catch (RemoteException e) {
        Log.e(TAG, "NFC service dead", e);
        return null;
    }
}
Also used : INfcTag(android.nfc.INfcTag) NdefMessage(android.nfc.NdefMessage) IOException(java.io.IOException) RemoteException(android.os.RemoteException) TagLostException(android.nfc.TagLostException)

Example 19 with NdefMessage

use of android.nfc.NdefMessage in project android_frameworks_base by AOSPA.

the class Ndef method getNdefMessage.

/**
     * Read the current {@link android.nfc.NdefMessage} on this tag.
     *
     * <p>This always reads the current NDEF Message stored on the tag.
     *
     * <p>Note that this method may return null if the tag was in the
     * INITIALIZED state as defined by NFC Forum, as in that state the
     * tag is formatted to support NDEF but does not contain a message yet.
     *
     * <p>This is an I/O operation and will block until complete. It must
     * not be called from the main application thread. A blocked call will be canceled with
     * {@link IOException} if {@link #close} is called from another thread.
     *
     * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
     *
     * @return the NDEF Message, can be null
     * @throws TagLostException if the tag leaves the field
     * @throws IOException if there is an I/O failure, or the operation is canceled
     * @throws FormatException if the NDEF Message on the tag is malformed
     */
public NdefMessage getNdefMessage() 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)) {
            NdefMessage msg = tagService.ndefRead(serviceHandle);
            if (msg == null && !tagService.isPresent(serviceHandle)) {
                throw new TagLostException();
            }
            return msg;
        } else if (!tagService.isPresent(serviceHandle)) {
            throw new TagLostException();
        } else {
            return null;
        }
    } catch (RemoteException e) {
        Log.e(TAG, "NFC service dead", e);
        return null;
    }
}
Also used : INfcTag(android.nfc.INfcTag) NdefMessage(android.nfc.NdefMessage) IOException(java.io.IOException) RemoteException(android.os.RemoteException) TagLostException(android.nfc.TagLostException)

Example 20 with NdefMessage

use of android.nfc.NdefMessage in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class WriteWifiConfigToNfcDialog method handleWriteNfcEvent.

private void handleWriteNfcEvent(Tag tag) {
    Ndef ndef = Ndef.get(tag);
    if (ndef != null) {
        if (ndef.isWritable()) {
            NdefRecord record = NdefRecord.createMime(NFC_TOKEN_MIME_TYPE, hexStringToByteArray(mWpsNfcConfigurationToken));
            try {
                ndef.connect();
                ndef.writeNdefMessage(new NdefMessage(record));
                getOwnerActivity().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        mProgressBar.setVisibility(View.GONE);
                    }
                });
                setViewText(mLabelView, R.string.status_write_success);
                setViewText(mCancelButton, com.android.internal.R.string.done_label);
            } catch (IOException e) {
                setViewText(mLabelView, R.string.status_failed_to_write);
                Log.e(TAG, "Unable to write Wi-Fi config to NFC tag.", e);
                return;
            } catch (FormatException e) {
                setViewText(mLabelView, R.string.status_failed_to_write);
                Log.e(TAG, "Unable to write Wi-Fi config to NFC tag.", e);
                return;
            }
        } else {
            setViewText(mLabelView, R.string.status_tag_not_writable);
            Log.e(TAG, "Tag is not writable");
        }
    } else {
        setViewText(mLabelView, R.string.status_tag_not_writable);
        Log.e(TAG, "Tag does not support NDEF");
    }
}
Also used : NdefRecord(android.nfc.NdefRecord) Ndef(android.nfc.tech.Ndef) NdefMessage(android.nfc.NdefMessage) IOException(java.io.IOException) FormatException(android.nfc.FormatException)

Aggregations

NdefMessage (android.nfc.NdefMessage)22 NdefRecord (android.nfc.NdefRecord)9 IOException (java.io.IOException)8 INfcTag (android.nfc.INfcTag)7 RemoteException (android.os.RemoteException)7 TagLostException (android.nfc.TagLostException)6 Parcelable (android.os.Parcelable)5 NfcAdapter (android.nfc.NfcAdapter)3 Tag (android.nfc.Tag)3 Intent (android.content.Intent)2 FormatException (android.nfc.FormatException)2 NfcEvent (android.nfc.NfcEvent)2 PaymentIntent (de.schildbach.wallet.data.PaymentIntent)2 Uri (android.net.Uri)1 Ndef (android.nfc.tech.Ndef)1 Handler (android.os.Handler)1 HandlerThread (android.os.HandlerThread)1 BinaryInputParser (de.schildbach.wallet.ui.InputParser.BinaryInputParser)1 UserBean (org.qii.weiciyuan.bean.UserBean)1