本文整理匯總了Java中android.nfc.tech.Ndef.writeNdefMessage方法的典型用法代碼示例。如果您正苦於以下問題:Java Ndef.writeNdefMessage方法的具體用法?Java Ndef.writeNdefMessage怎麽用?Java Ndef.writeNdefMessage使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.nfc.tech.Ndef
的用法示例。
在下文中一共展示了Ndef.writeNdefMessage方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: writeTag
import android.nfc.tech.Ndef; //導入方法依賴的package包/類
/**
* 寫入標簽
*
* @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;
}
示例2: writeTAG
import android.nfc.tech.Ndef; //導入方法依賴的package包/類
protected void writeTAG(Tag tag) throws IOException, FormatException {
Ndef ndefTag = Ndef.get(tag);
byte[] stringBytes = passphrase.getBytes();
NdefRecord data = NdefRecord.createMime(CONST.NFC_MIME_LOGIN, stringBytes);
NdefMessage message = new NdefMessage(data);
if (ndefTag != null) { //write to formatted tag
ndefTag.connect();
ndefTag.writeNdefMessage(message);
} else { //format the tag
NdefFormatable format = NdefFormatable.get(tag);
if(format != null) {
format.connect();
format.format(message);
}
}
}
示例3: writeToNfc
import android.nfc.tech.Ndef; //導入方法依賴的package包/類
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);
}
}
}
示例4: writeTag
import android.nfc.tech.Ndef; //導入方法依賴的package包/類
public void writeTag(Tag tag, NdefMessage message)
{
if (tag != null) {
try {
Ndef ndefTag = Ndef.get(tag);
if (ndefTag == null) {
// Let's try to format the Tag in NDEF
NdefFormatable nForm = NdefFormatable.get(tag);
if (nForm != null) {
nForm.connect();
nForm.format(message);
nForm.close();
}
}
else {
ndefTag.connect();
ndefTag.writeNdefMessage(message);
ndefTag.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
示例5: writeTag
import android.nfc.tech.Ndef; //導入方法依賴的package包/類
/**
* 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");
}
}
}
示例6: doInBackground
import android.nfc.tech.Ndef; //導入方法依賴的package包/類
@Override
protected String doInBackground(Tag... params) {
Tag tag = params[0];
Ndef ndef = Ndef.get(tag);
if (ndef == null) {
// NDEF is not supported by this Tag.
return null;
}
try {
NdefMessage msg = new NdefMessage(
new NdefRecord[] { NdefRecord.createMime(
"application/pubk.com.sawyer.easypgp", publicKeyBytes) });
ndef.connect();
ndef.writeNdefMessage(msg);
ndef.close();
return "Success";
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例7: writeTag
import android.nfc.tech.Ndef; //導入方法依賴的package包/類
/**
* 將數據寫入標簽中
* @param msg
* @param tag
* @return
*/
private boolean writeTag(NdefMessage msg,Tag tag){
try {
Ndef ndef = Ndef.get(tag);
ndef.connect();
ndef.writeNdefMessage(msg);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
示例8: doInBackground
import android.nfc.tech.Ndef; //導入方法依賴的package包/類
@Override
protected Boolean doInBackground(final TagWriteParam... params) {
final Ndef ndef = params[0].mTag;
final NdefMessage message = params[0].mMessage;
try {
ndef.connect();
ndef.writeNdefMessage(message);
Log.d(TAG, "Wrote tag");
return true;
} catch (IOException | FormatException e) {
mException = e;
}
return false;
}
示例9: writeMessage
import android.nfc.tech.Ndef; //導入方法依賴的package包/類
private void writeMessage(Tag tag, NdefMessage ndefMessage)
{
try
{
if (tag == null)
{
Toast.makeText(this, "An Error has Occurred, Please Try Again", Toast.LENGTH_LONG).show();
}
Ndef ndef = Ndef.get(tag);
if (ndef == null)
{
formatTag(tag, ndefMessage);
}
else
{
ndef.connect();
if (!ndef.isWritable())
{
Toast.makeText(this, "Tag is not Writable", Toast.LENGTH_LONG).show();
ndef.close();
return;
}
ndef.writeNdefMessage(ndefMessage);
ndef.close();
}
}
catch (Exception e)
{
Log.e("writeMessage", e.getMessage());
}
}
示例10: writeNFCTag
import android.nfc.tech.Ndef; //導入方法依賴的package包/類
/**
* 往標簽寫數據的方法
*
* @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) {
}
}
示例11: writeTag
import android.nfc.tech.Ndef; //導入方法依賴的package包/類
/**
* 寫數據
*
* @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;
}
示例12: writeNdefMessage
import android.nfc.tech.Ndef; //導入方法依賴的package包/類
private void writeNdefMessage(Tag tag, NdefMessage ndefMessage) {
try {
if (tag == null) {
Toast.makeText(this, "Tag object cannot be null", Toast.LENGTH_SHORT).show();
return;
}
Ndef ndef = Ndef.get(tag);
if (ndef == null) {
// format tag with the ndef format and writes the message.
formatTag(tag, ndefMessage);
} else {
ndef.connect();
if (!ndef.isWritable()) {
Toast.makeText(this, "Tag is not writable!", Toast.LENGTH_SHORT).show();
ndef.close();
return;
}
ndef.writeNdefMessage(ndefMessage);
ndef.close();
Toast.makeText(this, "Tag writen!", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.e("writeNdefMessage", e.getMessage());
}
}
示例13: doInBackground
import android.nfc.tech.Ndef; //導入方法依賴的package包/類
@Override
protected Boolean doInBackground(Intent... params) {
Intent intent = params[0];
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NdefMessage message = this.tag.encode();
if (tag != null) {
try {
Ndef ndefTag = Ndef.get(tag);
if (ndefTag == null) {
NdefFormatable ndefForm = NdefFormatable.get(tag);
if (ndefForm != null) {
ndefForm.connect();
ndefForm.format(message);
ndefForm.close();
}
}
else {
ndefTag.connect();
ndefTag.writeNdefMessage(message);
ndefTag.close();
}
return true;
}
catch(Exception e) {
e.printStackTrace();
}
}
return false;
}
示例14: write
import android.nfc.tech.Ndef; //導入方法依賴的package包/類
private void write(String text, Tag tag) throws IOException, FormatException {
/*
http://stackoverflow.com/questions/11427997/android-app-to-add-mutiple-record-in-nfc-tag
*/
// We want to include a reference to the app, for those who don't have one.
// This way, their phones will open this app when a tag encoded with this app is used.
String arrPackageName = "com.briankhuu.nfcmessageboard";
final int AAR_RECORD_BYTE_LENGTH = 50; // I guess i suck at byte counting. well at least this should still work. This approach does lead to wasted space however.
//infoMsg = "\n\n---\n To post here. Use the "NFC Messageboard" app: https://play.google.com/store/search?q=NFC%20Message%20Board ";
// Trim to size (for now this is just a dumb trimmer...) (Later on, you want to remove whole post first
// Seem that header and other things takes 14 chars. For safety. Lets just remove 20.
// 0 (via absolute value) < valid entry size < Max Tag size
final int NDEF_RECORD_HEADER_SIZE = 6;
final int NDEF_STRING_PAYLOAD_HEADER_SIZE = 4;
int maxTagByteLength = Math.abs(tag_size - NDEF_RECORD_HEADER_SIZE - NDEF_STRING_PAYLOAD_HEADER_SIZE - AAR_RECORD_BYTE_LENGTH);
if (text.length() >= maxTagByteLength ){ // Write like normal if content to write will fit without modification
// Else work out what to remove. For now, just do a dumb trimming. // Unicode characters may take more than 1 byte.
text = truncateWhenUTF8(text, maxTagByteLength);
}
// Write tag
//NdefRecord[] records = { createRecord(text), aarNdefRecord };
NdefMessage message = new NdefMessage(new NdefRecord[]{
createRecord(text)
,NdefRecord.createApplicationRecord(arrPackageName)
});
Ndef ndef = Ndef.get(tag);
ndef.connect();
ndef.writeNdefMessage(message);
ndef.close();
}
示例15: writeNdefMessage
import android.nfc.tech.Ndef; //導入方法依賴的package包/類
private void writeNdefMessage(Tag tag, NdefMessage ndefMessage) {
try {
if(tag == null) {
Toast.makeText(this, "Tag object cannot be null", Toast.LENGTH_SHORT).show();
return;
}
Ndef ndef = Ndef.get(tag);
if(ndef == null) {
//format tag with ndef format and writes message
formatTag(tag,ndefMessage);
} else {
ndef.connect();
if(!ndef.isWritable()) {
Toast.makeText(this, "Tag is not writable! ", Toast.LENGTH_SHORT).show();
ndef.close();
return;
}
ndef.writeNdefMessage(ndefMessage);
ndef.close();
Toast.makeText(this, "Tag Written!! :) ", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.e("writeNdefMessage", e.getMessage());
}
}