Java Code Examples for android.nfc.tech.Ndef#writeNdefMessage()

The following examples show how to use android.nfc.tech.Ndef#writeNdefMessage() . 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: 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 2
Source File: NFCWriteFragment.java    From android-nfc-tag-read-write with MIT License 6 votes vote down vote up
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 3
Source File: NfcHandler.java    From PowerSwitch_Android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 4
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 5
Source File: BaseActivity.java    From xmrwallet with Apache License 2.0 5 votes vote down vote up
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 6
Source File: RunAppActivity.java    From android-nfc with MIT License 4 votes vote down vote up
/**
 * 往标签写数据的方法
 *
 * @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 7
Source File: RunUrlActivity.java    From android-nfc with MIT License 4 votes vote down vote up
/**
 * 往标签写数据的方法
 *
 * @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) {
    }
}