本文整理匯總了Java中android.nfc.NdefRecord類的典型用法代碼示例。如果您正苦於以下問題:Java NdefRecord類的具體用法?Java NdefRecord怎麽用?Java NdefRecord使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
NdefRecord類屬於android.nfc包,在下文中一共展示了NdefRecord類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: readNfcTag
import android.nfc.NdefRecord; //導入依賴的package包/類
/**
* 讀取NFC標簽文本數據
*/
private void readNfcTag(Intent intent) {
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage msgs[] = null;
int contentSize = 0;
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
contentSize += msgs[i].toByteArray().length;
}
}
try {
if (msgs != null) {
NdefRecord record = msgs[0].getRecords()[0];
String textRecord = parseTextRecord(record);
mTagText += textRecord + "\n\ntext\n" + contentSize + " bytes";
}
} catch (Exception e) {
}
}
}
示例2: createTextRecord
import android.nfc.NdefRecord; //導入依賴的package包/類
/**
* 創建NDEF文本數據
*
* @param text
* @return
*/
public static NdefRecord createTextRecord(String text) {
byte[] langBytes = Locale.CHINA.getLanguage().getBytes(Charset.forName("US-ASCII"));
Charset utfEncoding = Charset.forName("UTF-8");
//將文本轉換為UTF-8格式
byte[] textBytes = text.getBytes(utfEncoding);
//設置狀態字節編碼最高位數為0
int utfBit = 0;
//定義狀態字節
char status = (char) (utfBit + langBytes.length);
byte[] data = new byte[1 + langBytes.length + textBytes.length];
//設置第一個狀態字節,先將狀態碼轉換成字節
data[0] = (byte) status;
//設置語言編碼,使用數組拷貝方法,從0開始拷貝到data中,拷貝到data的1到langBytes.length的位置
System.arraycopy(langBytes, 0, data, 1, langBytes.length);
//設置文本字節,使用數組拷貝方法,從0開始拷貝到data中,拷貝到data的1 + langBytes.length
//到textBytes.length的位置
System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);
//通過字節傳入NdefRecord對象
//NdefRecord.RTD_TEXT:傳入類型 讀寫
NdefRecord ndefRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_TEXT, new byte[0], data);
return ndefRecord;
}
示例3: tryUpdateIntentFromBeam
import android.nfc.NdefRecord; //導入依賴的package包/類
/**
* Checks to see if the activity's intent ({@link android.app.Activity#getIntent()}) is
* an NFC intent that the app recognizes. If it is, then parse the NFC message and set the
* activity's intent (using {@link Activity#setIntent(android.content.Intent)}) to something
* the app can recognize (i.e. a normal {@link Intent#ACTION_VIEW} intent).
*/
public static void tryUpdateIntentFromBeam(Activity activity) {
Intent originalIntent = activity.getIntent();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(originalIntent.getAction())) {
Parcelable[] rawMsgs = originalIntent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage msg = (NdefMessage) rawMsgs[0];
// Record 0 contains the MIME type, record 1 is the AAR, if present.
// In iosched, AARs are not present.
NdefRecord mimeRecord = msg.getRecords()[0];
if (ScheduleContract.makeContentItemType(
ScheduleContract.Sessions.CONTENT_TYPE_ID).equals(
new String(mimeRecord.getType()))) {
// Re-set the activity's intent to one that represents session details.
Intent sessionDetailIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(new String(mimeRecord.getPayload())));
activity.setIntent(sessionDetailIntent);
}
}
}
示例4: getTextFromNdefRecord
import android.nfc.NdefRecord; //導入依賴的package包/類
private String getTextFromNdefRecord(NdefRecord ndefRecord)
{
String content = null;
try
{
byte[] payload = ndefRecord.getPayload();
String encoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTf-8";
int languageSize = payload[0] & 0063;
content = new String(payload, languageSize+1, payload.length - languageSize - 1, encoding);
}
catch (UnsupportedEncodingException e)
{
Log.e("createTextRecord", e.getMessage());
}
return content;
}
示例5: createTextRecord
import android.nfc.NdefRecord; //導入依賴的package包/類
private NdefRecord createTextRecord (String message)
{
try
{
byte[] language;
language = Locale.getDefault().getLanguage().getBytes("UTF-8");
final byte[] text = message.getBytes("UTF-8");
final int languageSize = language.length;
final int textLength = text.length;
final ByteArrayOutputStream payload = new ByteArrayOutputStream(1 + languageSize + textLength);
payload.write((byte) (languageSize & 0x1F));
payload.write(language, 0, languageSize);
payload.write(text, 0, textLength);
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload.toByteArray());
}
catch (UnsupportedEncodingException e)
{
Log.e("createTextRecord", e.getMessage());
}
return null;
}
示例6: onNewIntent
import android.nfc.NdefRecord; //導入依賴的package包/類
public void onNewIntent(Intent intent) {
Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NdefMessage ndefMessage = new NdefMessage(new NdefRecord[]{createUriRecord(mUri)});
boolean result = writeTag(ndefMessage, detectedTag);
if (result) {
Toast.makeText(this, "寫入成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "寫入失敗", Toast.LENGTH_SHORT).show();
}
}
示例7: createUriRecord
import android.nfc.NdefRecord; //導入依賴的package包/類
/**
* 將Uri轉成NdefRecord
*
* @param uriStr
* @return
*/
public static NdefRecord createUriRecord(String uriStr) {
byte prefix = 0;
for (Byte b : UriPrefix.URI_PREFIX_MAP.keySet()) {
String prefixStr = UriPrefix.URI_PREFIX_MAP.get(b).toLowerCase();
if ("".equals(prefixStr))
continue;
if (uriStr.toLowerCase().startsWith(prefixStr)) {
prefix = b;
uriStr = uriStr.substring(prefixStr.length());
break;
}
}
byte[] data = new byte[1 + uriStr.length()];
data[0] = prefix;
System.arraycopy(uriStr.getBytes(), 0, data, 1, uriStr.length());
NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], data);
return record;
}
示例8: createNdefMessage
import android.nfc.NdefRecord; //導入依賴的package包/類
/**
* Implementation for the CreateNdefMessageCallback interface
*/
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
Time time = new Time();
time.setToNow();
String text = ("Beam me up!\n\n" +
"Beam Time: " + time.format("%H:%M:%S"));
NdefMessage msg = new NdefMessage(NdefRecord.createMime(
"application/com.example.android.beam", text.getBytes())
/**
* The Android Application Record (AAR) is commented out. When a device
* receives a push with an AAR in it, the application specified in the AAR
* is guaranteed to run. The AAR overrides the tag dispatch system.
* You can add it back in to guarantee that this
* activity starts when receiving a beamed message. For now, this code
* uses the tag dispatch system.
*/
//,NdefRecord.createApplicationRecord("com.example.android.beam")
);
return msg;
}
示例9: readNFCTag
import android.nfc.NdefRecord; //導入依賴的package包/類
private String readNFCTag(Intent intent){
String response=null;
if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())){
//從標簽讀取數據
Parcelable[] nfcMsgs=intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES
);
NdefMessage msgs[]=null;
int contentSize=0;
if(nfcMsgs!=null){
msgs=new NdefMessage[nfcMsgs.length];
// 標簽可能存儲多個NdefMessage對象,一般情況下隻有一個
for(int i=0;i<nfcMsgs.length;i++){
// 轉換為NdefMessage對象
msgs[i]= (NdefMessage) nfcMsgs[i];
// 計算數據的總長度
contentSize+=msgs[i].toByteArray().length;
}
}
try{
if(msgs!=null){
// 隻讀第一個信息
NdefRecord record=msgs[0].getRecords()[0];
response=parseTextRecord(record);
System.out.println(response);
}
}
catch (Exception e){
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}
}
return response;
}
示例10: parseTextRecord
import android.nfc.NdefRecord; //導入依賴的package包/類
private String parseTextRecord(NdefRecord record){
String response=null;
// 驗證TNF是否為TNF_WELL_KNOWN
if(record.getTnf()!=NdefRecord.TNF_WELL_KNOWN) {
System.out.println("不是TNF_WELL_KNOW");
return null;
}
byte[] payload=record.getPayload();
Byte statusByte=payload[0];
String textCoding=((statusByte&0x80)==0)?"utf-8":"utf-16";
int codeLength=statusByte&0x3f;
try{
response=new String(payload,codeLength+1,payload.length-codeLength-1,textCoding);
}
catch(UnsupportedEncodingException e){
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}
return response;
}
示例11: setBeamSessionUri
import android.nfc.NdefRecord; //導入依賴的package包/類
/**
* Sets this activity's Android Beam message to one representing the given session.
*/
public static void setBeamSessionUri(Activity activity, Uri sessionUri) {
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
if (nfcAdapter == null) {
// No NFC :-(
return;
}
nfcAdapter.setNdefPushMessage(new NdefMessage(
new NdefRecord[]{
new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
ScheduleContract.makeContentItemType(
ScheduleContract.Sessions.CONTENT_TYPE_ID).getBytes(),
new byte[0],
sessionUri.toString().getBytes())
}), activity);
}
示例12: eraseTag
import android.nfc.NdefRecord; //導入依賴的package包/類
private void eraseTag(CallbackContext callbackContext) throws JSONException {
Tag tag = savedIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NdefRecord[] records = {
new NdefRecord(NdefRecord.TNF_EMPTY, new byte[0], new byte[0], new byte[0])
};
writeNdefMessage(new NdefMessage(records), tag, callbackContext);
}
示例13: onCreate
import android.nfc.NdefRecord; //導入依賴的package包/類
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cinemas = DBHelper.getInstance(getContext()).getCinemas();
watcher = new Gson().fromJson(getArguments().getString("watcher"), Watcher.class);
nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
if(nfcAdapter != null) {
nfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() {
@Override
public NdefMessage createNdefMessage(NfcEvent nfcEvent) {
return new NdefMessage(
new NdefRecord[] {
NdefRecord.createUri(BuildConfig.SERVER_BASE_URL + "w/" + watcher.getID()),
NdefRecord.createApplicationRecord(BuildConfig.APPLICATION_ID)
}
);
}
}, getActivity());
}
}
示例14: write
import android.nfc.NdefRecord; //導入依賴的package包/類
/**
* Write text to a tag
*
* @param textToWrite the text to write
*/
public void write(String textToWrite) {
Locale locale = Locale.US;
final byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("UTF-8"));
final byte[] textBytes = textToWrite.getBytes(Charset.forName("UTF-8"));
final int utfBit = 0;
final char status = (char) (utfBit + langBytes.length);
final byte[] data = new byte[1 + langBytes.length + textBytes.length];
data[0] = (byte) status;
System.arraycopy(langBytes, 0, data, 1, langBytes.length);
System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);
NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
NdefRecord[] records = {record};
messageToWrite = new NdefMessage(records);
}
示例15: readText
import android.nfc.NdefRecord; //導入依賴的package包/類
private void readText(NdefRecord record) {
int start;
String payload = record.toString();
String MAC;
start = payload.indexOf("payload=");
MAC = payload.substring(start + 12);
MACs[mac_cnt] = MAC.substring(10, 12) + ":";
MACs[mac_cnt] += MAC.substring(8, 10) + ":";
MACs[mac_cnt] += MAC.substring(6, 8) + ":";
MACs[mac_cnt] += MAC.substring(4, 6) + ":";
MACs[mac_cnt] += MAC.substring(2, 4) + ":";
MACs[mac_cnt] += MAC.substring(0, 2);
mac_cnt++;
}