当前位置: 首页>>代码示例>>Java>>正文


Java Ndef.isWritable方法代码示例

本文整理汇总了Java中android.nfc.tech.Ndef.isWritable方法的典型用法代码示例。如果您正苦于以下问题:Java Ndef.isWritable方法的具体用法?Java Ndef.isWritable怎么用?Java Ndef.isWritable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在android.nfc.tech.Ndef的用法示例。


在下文中一共展示了Ndef.isWritable方法的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;
}
 
开发者ID:jopenbox,项目名称:android-nfc,代码行数:27,代码来源:WriteUriActivity.java

示例2: 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");
        }
    }
}
 
开发者ID:Power-Switch,项目名称:PowerSwitch_Android,代码行数:26,代码来源:NfcHandler.java

示例3: writableTag

import android.nfc.tech.Ndef; //导入方法依赖的package包/类
public static boolean writableTag(Tag tag) {
    try {
        Ndef ndef = Ndef.get(tag);
        if (ndef != null) {
            ndef.connect();
            if (!ndef.isWritable()) {
                ndef.close();
                return false;
            }
            ndef.close();
            return true;
        }
    } catch (Exception e) {
    }
    return false;
}
 
开发者ID:legendmohe,项目名称:LEHomeMobile_android,代码行数:17,代码来源:NFCHelper.java

示例4: readTagFromIntent

import android.nfc.tech.Ndef; //导入方法依赖的package包/类
private void readTagFromIntent(Intent intent) {
    if (intent != null){
        String action = intent.getAction();
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
            tagReaded = true;

            // get NDEF tag details
            Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            if (tag != null) {
                Ndef ndefTag = Ndef.get(tag);
                //int tagSize = ndefTag.getMaxSize();         // tag size
                tagIsWritable = ndefTag.isWritable();   // is tag writable?
                //String tagType = ndefTag.getType();            // tag type

                Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
                if (rawMsgs != null) {
                    NdefRecord[] records = ((NdefMessage) rawMsgs[0]).getRecords();
                    String text = ndefRecordToString(records[0]);
                    onTagReadListener.onTagRead(text);
                }
            }
        }
    }
}
 
开发者ID:henrichg,项目名称:PhoneProfilesPlus,代码行数:25,代码来源:NFCTagReadWriteManager.java

示例5: writableTag

import android.nfc.tech.Ndef; //导入方法依赖的package包/类
private boolean writableTag(Tag tag) {

		try {
			Ndef ndef = Ndef.get(tag);
			if (ndef != null) {
				ndef.connect();
				if (!ndef.isWritable()) {
					ToastUtils.makeWarningToast(this, "Tag is Read-Only.");

					ndef.close();
					return false;
				}
				ndef.close();
				return true;
			}
		} catch (Exception e) {
			Logger.e(TAG, e.getLocalizedMessage());
			ToastUtils.makeWarningToast(this, "Failed to Read Tag");
		}

		return false;
	}
 
开发者ID:MikeFot,项目名称:Android--Event-Triggered-Skype-Caller,代码行数:23,代码来源:WriteNFCActivity.java

示例6: programTag

import android.nfc.tech.Ndef; //导入方法依赖的package包/类
@Override
public ThereWas programTag(ProgramableTag tag) {
    Intent intent = parentActivity.getIntent();

    if (aTagWeCanHandleWasScanned(intent)) {
        Ndef ndefTag = getNdefTagFrom(intent);

        if (!ndefTag.isWritable()) {
            throw new TagIsNotWritable();
        }

        program(ndefTag, tag);
        return ThereWas.A_TAG_TO_PROGRAM;
    }

    return ThereWas.NO_TAG_TO_PROGRAM;
}
 
开发者ID:jthurne,项目名称:kanban-salad,代码行数:18,代码来源:NfcProgramer.java

示例7: writeNfcTag

import android.nfc.tech.Ndef; //导入方法依赖的package包/类
private void writeNfcTag(@NonNull final Tag tag) {
    final Ndef ndef = Ndef.get(tag);

    if (ndef == null) {
        showWriteError(getText(R.string.nfc_error_no_ndef));
        return;
    }

    if (!ndef.isWritable()) {
        showWriteError(getText(R.string.nfc_error_not_writable));
        return;
    }

    final NdefMessage message = NdefUtils.toNdefMessage(mPassword);

    final int messageLength = message.getByteArrayLength();
    final int maxSize = ndef.getMaxSize();

    if (messageLength > maxSize) {
        showWriteError(
                getString(R.string.nfc_error_not_enough_space_format, messageLength, maxSize));
        return;
    }

    new TagWriteTask(this).execute(new TagWriteParam(ndef, message));
}
 
开发者ID:xxv,项目名称:SuperGenPass,代码行数:27,代码来源:NfcWriteFragment.java

示例8: 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());
    }
}
 
开发者ID:ThomasDelaney,项目名称:TapIn,代码行数:36,代码来源:add3.java

示例9: 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) {
    }
}
 
开发者ID:jopenbox,项目名称:android-nfc,代码行数:48,代码来源:RunAppActivity.java

示例10: 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());
        }

    }
 
开发者ID:tekesan,项目名称:TraiNFCUI,代码行数:37,代码来源:Tap.java

示例11: 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());
    }
}
 
开发者ID:JimSeker,项目名称:nfc,代码行数:34,代码来源:MainActivity.java

示例12: 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
            .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) {
    }
}
 
开发者ID:jopenbox,项目名称:android-nfc,代码行数:49,代码来源:RunUrlActivity.java

示例13: onNewIntent

import android.nfc.tech.Ndef; //导入方法依赖的package包/类
/**
 * Listen to NFC incomming data
 */
@Override
public void onNewIntent(Intent intent) {
    MLog.d(TAG, "New intent " + intent);

    if (intent.getAction() != null) {
        MLog.d(TAG, "Discovered tag with intent: " + intent);

        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        String nfcID = StrUtils.bytetostring(tag.getId());

        // if there is a message waiting to be written
        if (PNfc.nfcMsg != null) {
            MLog.d(TAG, "->" + PNfc.nfcMsg);
            PNfc.writeTag(this, tag, PNfc.nfcMsg);
            if (onNFCWrittenListener != null) onNFCWrittenListener.onNewTag();
            onNFCWrittenListener = null;
            PNfc.nfcMsg = null;

        // read the nfc tag info
        } else {
            // get NDEF tag details
            Ndef ndefTag = Ndef.get(tag);
            if (ndefTag == null) {
                return;
            }

            int size = ndefTag.getMaxSize(); // tag size
            boolean writable = ndefTag.isWritable(); // is tag writable?
            String type = ndefTag.getType(); // tag type

            String nfcMessage = "";

            // get NDEF message details
            NdefMessage ndefMesg = ndefTag.getCachedNdefMessage();
            if (ndefMesg != null) {
                NdefRecord[] ndefRecords = ndefMesg.getRecords();
                int len = ndefRecords.length;
                String[] recTypes = new String[len]; // will contain the
                // NDEF record types
                String[] recPayloads = new String[len]; // will contain the
                // NDEF record types
                for (int i = 0; i < len; i++) {
                    recTypes[i] = new String(ndefRecords[i].getType());
                    recPayloads[i] = new String(ndefRecords[i].getPayload());
                    MLog.d(TAG, "qq " + i + " " + recTypes[i] + " " + recPayloads[i]);

                }
                nfcMessage = recPayloads[0];
            }
            if (onNFCListener != null) onNFCListener.onNewTag(nfcID, nfcMessage);
        }
    }
}
 
开发者ID:victordiaz,项目名称:phonk,代码行数:57,代码来源:AppRunnerActivity.java

示例14: writeNFCTag

import android.nfc.tech.Ndef; //导入方法依赖的package包/类
private 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 {
		Ndef ndef = Ndef.get(tag);
		//���ж�һ�������ǩ�Dz���NDEF��
		if(ndef != null){ //�����NDEF��ʽ��
			ndef.connect();
			//�����ж������ǩ�Ƿ��ǿ�д��
			if( ! ndef.isWritable()){ //����Dz���д�ģ�ֱ�ӾͿ��Խ�����
				Toast.makeText(this , "��NFC��ǩ����д!" , Toast.LENGTH_SHORT).show();
				return;
			}
			//�����жϵ�ǰ��ǩ����������Ƿ���װ������Ҫд�����Ϣ
			if(ndef.getMaxSize() < size){
				Toast.makeText(this , "��NFC��ǩ������д����̫С!" , Toast.LENGTH_SHORT).show();
				return;
			}
			//����Ϊֹ���Ϳ��Է��ĵİѶ���д��NFC��ǩ����
			ndef.writeNdefMessage(ndefMessage);
			Toast.makeText(this , "NFC��ǩд�����ݳɹ�" , Toast.LENGTH_SHORT).show();
		}
		else{ //�������NDEF��ʽ��
			//���Խ������NDEF��ǩ��ʽ����NDEF��ʽ��
			NdefFormatable format = NdefFormatable.get(tag);
			//��Ϊ��Щ��ǩ��ֻ���ģ�����������Ҫ�ж�һ��
			//���format��Ϊnull����ʾ�����ǩ�ǿ��Խ��ܸ�ʽ����
			if(format != null){
				format.connect();
				format.format(ndefMessage); //ͬʱ����˸�ʽ����д����Ϣ�IJ���
				Toast.makeText(this , "NFC��ǩ��ʽ��д��ɹ�" , Toast.LENGTH_SHORT).show();
			}
			else{
				Toast.makeText(this , "��NFC��ǩ�޷�����ʽ��" , Toast.LENGTH_SHORT).show();
			}
		}
	} 
	catch (Exception e) {
		Toast.makeText(this , "�޷���ȡ��NFC��ǩ" , Toast.LENGTH_SHORT).show();
	}
}
 
开发者ID:ZhaoYukai,项目名称:NFCApp2,代码行数:47,代码来源:AutoOpenUriActivity.java

示例15: handleIntent

import android.nfc.tech.Ndef; //导入方法依赖的package包/类
private void handleIntent(Intent intent) {
    /*
    *   Else just read tag as usual.
    * */
    /*
        This detects the intent and calls an nfc reading task
        I modified this to keep the tag object exposed in this class.
        This is since we will want to write to the same tag later.
     */
    String action = intent.getAction();

    // We want to read only valid html tags
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))
    {
        boolean mime_type_found = false;
        String type = intent.getType();

        if (MIME_TEXT_HTML.equals(type))
        {
            Toast.makeText(this, "Reading Html Tag", Toast.LENGTH_SHORT).show();
            tag_compression_mode = CompressionMode_Enum.NONE;
            mime_type_found = true;
        }
        else
        if (MIME_TEXT_HTML_GZIP.equals(type))
        {
            Toast.makeText(this, "Reading Gzipped Html Tag", Toast.LENGTH_SHORT).show();
            tag_compression_mode = CompressionMode_Enum.GZIP;
            mime_type_found = true;
        }


        // if plain/html
        if (mime_type_found) {
            tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            new NdefReaderTask().execute(tag);
        } else {
            //Log.d(TAG, "Wrong mime type: " + type);
        }
    }
    else
    {
        Log.e(LOGGER_TAG, "Skipping Tag as it is not NDEF");
    }

    /*
        So some useful tag info
        http://stackoverflow.com/questions/9971820/how-to-read-detected-nfc-tag-ndef-content-details-in-android
     */
    if (tag != null) {
        //Toast.makeText(this, "Reading Tag Info", Toast.LENGTH_SHORT).show();
        // get NDEF tag details
        Ndef ndefTag = Ndef.get(tag);

        tag_writable = ndefTag.isWritable(); // is tag writable?

        // get NDEF message details
        NdefMessage ndefMesg = ndefTag.getCachedNdefMessage();
        NdefRecord[] ndefRecords = ndefMesg.getRecords();
        int len = ndefRecords.length;
        recTypes = new String[len];     // will contain the NDEF record types
        for (int i = 0; i < len; i++) {
            recTypes[i] = new String(ndefRecords[i].getType());
        }

        // Get Tag ID
        tagID_string = bytesToHex(tag.getId());
        tag_size = ndefTag.getMaxSize();     // tag size
        tag_type = ndefTag.getType();         // tag type
    }
}
 
开发者ID:mofosyne,项目名称:NFCMessageBoard,代码行数:72,代码来源:ReadHtmlTags.java


注:本文中的android.nfc.tech.Ndef.isWritable方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。