當前位置: 首頁>>代碼示例>>Java>>正文


Java NdefFormatable類代碼示例

本文整理匯總了Java中android.nfc.tech.NdefFormatable的典型用法代碼示例。如果您正苦於以下問題:Java NdefFormatable類的具體用法?Java NdefFormatable怎麽用?Java NdefFormatable使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


NdefFormatable類屬於android.nfc.tech包,在下文中一共展示了NdefFormatable類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: formatTag

import android.nfc.tech.NdefFormatable; //導入依賴的package包/類
private void formatTag(Tag tag, NdefMessage ndefMessage)
{
    try
    {
        NdefFormatable ndefFormatable = NdefFormatable.get(tag);

        if (ndefFormatable == null)
        {
            Toast.makeText(this, "Tag is not formatable", Toast.LENGTH_LONG).show();
        }

        ndefFormatable.connect();
        ndefFormatable.format(ndefMessage);
        ndefFormatable.close();
    }
    catch (Exception e)
    {
        Log.e("formatTag", e.getMessage());
    }
}
 
開發者ID:ThomasDelaney,項目名稱:TapIn,代碼行數:21,代碼來源:add3.java

示例2: writeTAG

import android.nfc.tech.NdefFormatable; //導入依賴的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);
        }
    }
}
 
開發者ID:gtom1984,項目名稱:Eulen,代碼行數:19,代碼來源:nfctagbase.java

示例3: writeTag

import android.nfc.tech.NdefFormatable; //導入依賴的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();
        }
    }
}
 
開發者ID:The-Scrum-Masters,項目名稱:archive-carro-inteligente,代碼行數:27,代碼來源:NFCHandler.java

示例4: writeTag

import android.nfc.tech.NdefFormatable; //導入依賴的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

示例5: formatTag

import android.nfc.tech.NdefFormatable; //導入依賴的package包/類
private void formatTag(Tag tag, NdefMessage ndefMessage) {
    try{
        NdefFormatable ndefFormatable = NdefFormatable.get(tag);

        if(ndefFormatable == null) {
            Toast.makeText(this, "Tag is not NdefFormatable!!", Toast.LENGTH_SHORT).show();
            return;
        }

        ndefFormatable.connect();
        ndefFormatable.format(ndefMessage);
        ndefFormatable.close();

        Toast.makeText(this, "Tag Written!! :) ", Toast.LENGTH_SHORT).show();

    } catch (Exception e) {
        Log.e("formatTag", e.getMessage());
    }
}
 
開發者ID:JimSeker,項目名稱:nfc,代碼行數:20,代碼來源:MainActivity.java

示例6: setForegroundListener

import android.nfc.tech.NdefFormatable; //導入依賴的package包/類
private void setForegroundListener() {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    boolean handleFormatable = preferences.getBoolean("format_ndef_formatable_tags", false);

    pi = PendingIntent.getActivity(this, 0, new Intent(this,getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    intentFiltersArray = null;
    if(handleFormatable)
        techList = new String[][]{ new String[]{ NfcA.class.getName(),Ndef.class.getName()},
                new String[]{ NfcB.class.getName(),Ndef.class.getName()},
                new String[]{ NfcF.class.getName(),Ndef.class.getName()},
                new String[]{ NfcV.class.getName(),Ndef.class.getName()},
                new String[]{ NfcA.class.getName(),NdefFormatable.class.getName()},
                new String[]{ NfcB.class.getName(),NdefFormatable.class.getName()},
                new String[]{ NfcF.class.getName(),NdefFormatable.class.getName()},
                new String[]{ NfcV.class.getName(),NdefFormatable.class.getName()}};
    else
        techList = new String[][]{ new String[]{ NfcA.class.getName(),Ndef.class.getName()},
                new String[]{ NfcB.class.getName(),Ndef.class.getName()},
                new String[]{ NfcF.class.getName(),Ndef.class.getName()},
                new String[]{ NfcV.class.getName(),Ndef.class.getName()}};
}
 
開發者ID:OlivierGonthier,項目名稱:CryptoNFC,代碼行數:22,代碼來源:MainActivity.java

示例7: writeNFCTag

import android.nfc.tech.NdefFormatable; //導入依賴的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

示例8: handleIntent

import android.nfc.tech.NdefFormatable; //導入依賴的package包/類
public void handleIntent(Intent intent) {
    String action = intent.getAction();
    if (action == null) return;
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    if (tag == null) {
        sendError(TAG_READING_ERROR);
    }else {
        resetTechnologyFlags();
        switch (action) {
            case NfcAdapter.ACTION_NDEF_DISCOVERED:
                setCurrentTag(tag);
                if (!getNdefMaxSize().hasError() && !getTagId().hasError()) {
                    isTagSupported = true;
                    isNdef_Flag = true;
                    displayData();
                    sendNewTagFrame();
                } else {
                    sendError(TAG_READING_ERROR);
                }
                break;
            case NfcAdapter.ACTION_TECH_DISCOVERED:
                setCurrentTag(tag);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) {
                    String[] techList = tag.getTechList();
                    for (String tech : techList) {
                        if (!isTagSupported) {
                            if (Ndef.class.getName().equals(tech)) {
                                DataReply maxSize = getNdefMaxSize();
                                if (!maxSize.hasError() && !getTagId().hasError()) {
                                    isNdef_Flag = false;
                                    isTagSupported = true;
                                    DataReply recordCount = getNdefRecordCount();
                                    if (recordCount.getIntegerData() > 0 && recordCount.getIntegerData() < 256) {
                                        isNdef_Flag = true;
                                        displayData();
                                        sendNewTagFrame();
                                    }else if (recordCount.getIntegerData() == 0){
                                        sendNewEmptyTagFrame();
                                    }else if(recordCount.getError() != 0){
                                        sendError(recordCount.getError());
                                    }
                                } else if (maxSize.hasError()){
                                    sendError(maxSize.getError());
                                }
                            } else if (NdefFormatable.class.getName().equals(tech)) {
                                /*isNdef_Flag = false;
                                isTagSupported = true;
                                if (!getNdefMaxSize().hasError()&& !getTagId().hasError()) {
                                    sendNewEmptyTagFrame();
                                    displayData();
                                } else {
                                    sendError(TAG_READING_ERROR);
                                }*/
                            }
                        } else
                            break;
                    }
                    if (!isTagSupported)
                        sendError(TAG_NOT_SUPPORTED);
                }
                break;
            case NfcAdapter.ACTION_TAG_DISCOVERED:
                setCurrentTag(tag);
                break;
        }
    }
}
 
開發者ID:Dnet3,項目名稱:CustomAndroidOneSheeld,代碼行數:68,代碼來源:NfcShield.java

示例9: doInBackground

import android.nfc.tech.NdefFormatable; //導入依賴的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;
}
 
開發者ID:wesleydebruijn,項目名稱:android-nfc-wrapper,代碼行數:33,代碼來源:NFCWriteTask.java

示例10: enableForegroundDispatch

import android.nfc.tech.NdefFormatable; //導入依賴的package包/類
public void enableForegroundDispatch()
{
    Intent nfcIntent = new Intent(activity, getClass());
    nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, nfcIntent, 0);
    IntentFilter[] intentFiltersArray = new IntentFilter[] {};
    String[][] techList = new String[][] { { android.nfc.tech.Ndef.class.getName() }, { android.nfc.tech.NdefFormatable.class.getName() } };


    adapter.enableForegroundDispatch(activity, pendingIntent, intentFiltersArray, techList);
    Toast.makeText(activity, "enabled dispatch", Toast.LENGTH_SHORT).show();
}
 
開發者ID:The-Scrum-Masters,項目名稱:archive-carro-inteligente,代碼行數:13,代碼來源:NFCHandler.java

示例11: create

import android.nfc.tech.NdefFormatable; //導入依賴的package包/類
/**
 * Factory method that creates NfcTagHandler for a given NFC Tag.
 *
 * @param tag @see android.nfc.Tag
 * @return NfcTagHandler or null when unsupported Tag is provided.
 */
public static NfcTagHandler create(Tag tag) {
    if (tag == null) return null;

    Ndef ndef = Ndef.get(tag);
    if (ndef != null) return new NfcTagHandler(ndef, new NdefHandler(ndef));

    NdefFormatable formattable = NdefFormatable.get(tag);
    if (formattable != null) {
        return new NfcTagHandler(formattable, new NdefFormattableHandler(formattable));
    }

    return null;
}
 
開發者ID:mogoweb,項目名稱:365browser,代碼行數:20,代碼來源:NfcTagHandler.java

示例12: onCreate

import android.nfc.tech.NdefFormatable; //導入依賴的package包/類
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
    setContentView(R.layout.writeurl);
    final EditText urlEditText = (EditText)this.findViewById(R.id.urltext);

    Button writeUrlButton = (Button)this.findViewById(R.id.writeurlbutton);
    writeUrlButton.setOnClickListener(new View.OnClickListener() 
    {
    	public void onClick(View view) 
    	{
    		urlAddress = urlEditText.getText().toString();
     		TextView messageText = (TextView)findViewById(R.id.URL);
     		messageText.setText("Touch NFC Tag to write app to open " +urlAddress);
    		        		
    	}
    });

   
 	 mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 
 	 mPendingIntent = PendingIntent.getActivity(this, 0,
     new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
	 IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
	 	//ndef.addDataScheme("http");
mFilters = new IntentFilter[] {ndef,};
     mTechLists = new String[][] { new String[] { Ndef.class.getName() },
		   new String[] { NdefFormatable.class.getName() }};

 	
}
 
開發者ID:ShravanJ,項目名稱:easyNFC-dev,代碼行數:31,代碼來源:WriteApp.java

示例13: onCreate

import android.nfc.tech.NdefFormatable; //導入依賴的package包/類
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
    setContentView(R.layout.writeurl);
    final EditText urlEditText = (EditText)this.findViewById(R.id.urltext);

    Button writeUrlButton = (Button)this.findViewById(R.id.writeurlbutton);
    writeUrlButton.setOnClickListener(new View.OnClickListener() 
    {
    	public void onClick(View view)
    	{
    		urlAddress = urlEditText.getText().toString();
     		TextView messageText = (TextView)findViewById(R.id.URL);
     		messageText.setText("Touch NFC Tag to write http://www."+urlAddress);
    		        		
    	}
    });

   
 	 mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 
 	 mPendingIntent = PendingIntent.getActivity(this, 0,
             new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
	 IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
	 	//ndef.addDataScheme("http");
mFilters = new IntentFilter[] {ndef,};
     mTechLists = new String[][] { new String[] { Ndef.class.getName() },
		   new String[] { NdefFormatable.class.getName() }};

 	
}
 
開發者ID:ShravanJ,項目名稱:easyNFC-dev,代碼行數:31,代碼來源:WriteUrlActivity.java

示例14: initFields

import android.nfc.tech.NdefFormatable; //導入依賴的package包/類
/**
 * Initializes which intents and NfcTechnologies to filter for
 */
private void initFields() {
    pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    mIntentFilters = new IntentFilter[]{new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED)};
    mTechLists = new String[][]{new String[]{Ndef.class.getName()},
            new String[]{NdefFormatable.class.getName()}};
}
 
開發者ID:appfoundry,項目名稱:android-nfc-lib,代碼行數:11,代碼來源:NfcActivity.java

示例15: writeToNdefFormatableAndMakeReadonly

import android.nfc.tech.NdefFormatable; //導入依賴的package包/類
/**
 * {@inheritDoc}
 */
@Override
public boolean writeToNdefFormatableAndMakeReadonly(NdefMessage message, NdefFormatable ndefFormat) throws FormatException {
    setReadOnly(true);
    boolean result = writeToNdefFormatable(message, ndefFormat);
    setReadOnly(false);

    return result;

}
 
開發者ID:appfoundry,項目名稱:android-nfc-lib,代碼行數:13,代碼來源:NdefWriteImpl.java


注:本文中的android.nfc.tech.NdefFormatable類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。