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


Java Ndef类代码示例

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


Ndef类属于android.nfc.tech包,在下文中一共展示了Ndef类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: registerTagEvent

import android.nfc.tech.Ndef; //导入依赖的package包/类
@ReactMethod
  private void registerTagEvent(String alertMessage, Boolean invalidateAfterFirstRead, Callback callback) {
      Log.d(LOG_TAG, "registerTag");
isForegroundEnabled = true;

// capture all mime-based dispatch NDEF
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
	ndef.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
	throw new RuntimeException("fail", e);
   }
intentFilters.add(ndef);

// capture all rest NDEF, such as uri-based
      intentFilters.add(new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED));
techLists.add(new String[]{Ndef.class.getName()});

// for those without NDEF, get them as tags
      intentFilters.add(new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED));

if (isResumed) {
	enableDisableForegroundDispatch(true);
}
      callback.invoke();
	}
 
开发者ID:whitedogg13,项目名称:react-native-nfc-manager,代码行数:27,代码来源:NfcManager.java

示例2: 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

示例3: getNdefUsedSize

import android.nfc.tech.Ndef; //导入依赖的package包/类
private DataReply getNdefUsedSize() {
    DataReply dataReply = new DataReply();
    int size = 0;
    if (currentTag != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) {
            Ndef ndef = Ndef.get(currentTag);
            if (ndef != null && ndef.getCachedNdefMessage() != null)
                size = ndef.getCachedNdefMessage().toByteArray().length;
            else {
                dataReply.setError(TAG_READING_ERROR);
                return dataReply;
            }
            try {
                if (ndef != null) ndef.close();
            } catch (IOException e) {
            }
        }
    }
    dataReply.setIntegerData(size);
    return dataReply;
}
 
开发者ID:Dnet3,项目名称:CustomAndroidOneSheeld,代码行数:22,代码来源:NfcShield.java

示例4: getNdefMaxSize

import android.nfc.tech.Ndef; //导入依赖的package包/类
private DataReply getNdefMaxSize() {
    DataReply dataReply = new DataReply();
    int maxSize = 0;
    if (currentTag != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) {
            Ndef ndef = Ndef.get(currentTag);
            if (ndef != null) {
                maxSize = ndef.getMaxSize();
                if (maxSize <= 0){
                    dataReply.setError(TAG_READING_ERROR);
                    return dataReply;
                }
            }else {
                dataReply.setError(TAG_READING_ERROR);
                return dataReply;
            }
            try {
                if (ndef != null) ndef.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    dataReply.setIntegerData(maxSize);
    return dataReply;
}
 
开发者ID:Dnet3,项目名称:CustomAndroidOneSheeld,代码行数:27,代码来源:NfcShield.java

示例5: 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);
        }
    }
}
 
开发者ID:gtom1984,项目名称:Eulen,代码行数:19,代码来源:nfctagbase.java

示例6: nfcRead

import android.nfc.tech.Ndef; //导入依赖的package包/类
public String nfcRead(Tag t)
{
    Tag tag = t;
    Ndef ndef = Ndef.get(tag);
    if (ndef == null) {
        return null;
    }
    NdefMessage ndefMessage = ndef.getCachedNdefMessage();
    NdefRecord[] records = ndefMessage.getRecords();
    for (NdefRecord ndefRecord : records)
    {
        if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT))
        {
            try {return readText(ndefRecord);} catch (UnsupportedEncodingException e) {}
        }
    }

    return null;

}
 
开发者ID:tawaasalage,项目名称:RxBluetoothAuto,代码行数:21,代码来源:Home.java

示例7: doInBackground

import android.nfc.tech.Ndef; //导入依赖的package包/类
@Override
protected NFCTag doInBackground(Intent... params) {
    Intent intent = params[0];

    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    Ndef ndef = Ndef.get(tag);

    if (ndef == null) {
        return null;
    }

    NdefMessage ndefMessage = ndef.getCachedNdefMessage();

    if(ndefMessage != null) {
        NdefRecord[] records = ndefMessage.getRecords();
        for (NdefRecord ndefRecord : records) {
            try {
                return new NFCTag(ndefRecord).decode();
            } catch (UnsupportedEncodingException e) {
                Log.e("NFC", "Unsupported Encoding", e);
            }
        }
    }

    return null;
}
 
开发者ID:wesleydebruijn,项目名称:android-nfc-wrapper,代码行数:27,代码来源:NFCReadTask.java

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

        }
    }
 
开发者ID:Learn2Crack,项目名称:android-nfc-tag-read-write,代码行数:24,代码来源:NFCWriteFragment.java

示例9: onNewIntent

import android.nfc.tech.Ndef; //导入依赖的package包/类
@Override
protected void onNewIntent(Intent intent) {
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

    Log.d(TAG, "onNewIntent: "+intent.getAction());

    if(tag != null) {
        Toast.makeText(this, getString(R.string.message_tag_detected), Toast.LENGTH_SHORT).show();
        Ndef ndef = Ndef.get(tag);

        if (isDialogDisplayed) {

            if (isWrite) {

                String messageToWrite = mEtMessage.getText().toString();
                mNfcWriteFragment = (NFCWriteFragment) getFragmentManager().findFragmentByTag(NFCWriteFragment.TAG);
                mNfcWriteFragment.onNfcDetected(ndef,messageToWrite);

            } else {

                mNfcReadFragment = (NFCReadFragment)getFragmentManager().findFragmentByTag(NFCReadFragment.TAG);
                mNfcReadFragment.onNfcDetected(ndef);
            }
        }
    }
}
 
开发者ID:Learn2Crack,项目名称:android-nfc-tag-read-write,代码行数:27,代码来源:MainActivity.java

示例10: 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();
        }
    }
}
 
开发者ID:The-Scrum-Masters,项目名称:archive-carro-inteligente,代码行数:27,代码来源:NFCHandler.java

示例11: getTagText

import android.nfc.tech.Ndef; //导入依赖的package包/类
public String getTagText() throws Exception
{
    if (tag == null)
    {
        throw new Exception("Please call handle new read as tag is null");
    }
    Ndef ndef = Ndef.get(tag);
    NdefMessage ndefMessage = ndef.getCachedNdefMessage();

    NdefRecord[] records = ndefMessage.getRecords();
    for (NdefRecord ndefRecord : records)
    {
        if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT))
        {
            try
            {
                return readText(ndefRecord);
            }
            catch (UnsupportedEncodingException e)
            {
                Toast.makeText(activity, "Unsupported encoding", Toast.LENGTH_SHORT).show();
            }
        }
    }
    return null;
}
 
开发者ID:The-Scrum-Masters,项目名称:archive-carro-inteligente,代码行数:27,代码来源:NFCHandler.java

示例12: 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

示例13: doInBackground

import android.nfc.tech.Ndef; //导入依赖的package包/类
@Override
protected ArrayList<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;
    }

    ArrayList<String> results = new ArrayList<>();
    NdefMessage ndefMessage = ndef.getCachedNdefMessage();
    NdefRecord[] records = ndefMessage.getRecords();
    for (NdefRecord ndefRecord : records) {
        if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
            try {
                results.add(readText(ndefRecord));
            } catch (UnsupportedEncodingException e) {
                Log.e(TAG, "Unsupported Encoding", e);
            }
        }
    }

    return results;
}
 
开发者ID:legendmohe,项目名称:LEHomeMobile_android,代码行数:26,代码来源:NfcReadNdefAsyncTask.java

示例14: 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

示例15: readTag

import android.nfc.tech.Ndef; //导入依赖的package包/类
private void readTag(Tag t) {
    byte[] id = t.getId();

    // get NDEF tag details
    Ndef ndefTag = Ndef.get(t);

    // get NDEF message details
    NdefMessage ndefMesg = ndefTag.getCachedNdefMessage();
    if (ndefMesg == null) {
        return;
    }
    NdefRecord[] ndefRecords = ndefMesg.getRecords();
    if (ndefRecords == null) {
        return;
    }
    for (NdefRecord record : ndefRecords) {
        short tnf = record.getTnf();
        String type = new String(record.getType());
        if (tnf == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(type.getBytes(), NdefRecord.RTD_URI)) {
            String url = new String(record.getPayload());
            recordBadge(url);
        }
    }
}
 
开发者ID:The-WebOps-Club,项目名称:saarang-iosched,代码行数:25,代码来源:NfcBadgeActivity.java


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