Search in sources :

Example 11 with NdefRecord

use of android.nfc.NdefRecord 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 12 with NdefRecord

use of android.nfc.NdefRecord 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

NdefRecord (android.nfc.NdefRecord)12 NdefMessage (android.nfc.NdefMessage)9 NfcAdapter (android.nfc.NfcAdapter)3 NfcEvent (android.nfc.NfcEvent)2 Tag (android.nfc.Tag)2 Parcelable (android.os.Parcelable)2 Intent (android.content.Intent)1 FormatException (android.nfc.FormatException)1 Ndef (android.nfc.tech.Ndef)1 IOException (java.io.IOException)1