当前位置:网站首页>NFC read / write mode development - book summary

NFC read / write mode development - book summary

2022-06-25 09:53:00 TowerOs

Chapter Five: NFC Read write mode

Read write mode : The main function is to complete NFC equipment Yes NFC label The operation of

NFC Introduction to read-write mode

  • Also known as NFC Reader mode
  • Need familiarity NDEF agreement and RTD agreement

Tag Read operations (NDEF data )

  • NFC The device is scanned with NDEF When labeling data ,Android It provides support for message parsing
  • Learning goals : For any standard Tag Read it

Tag Read operation steps

When NFC The application found a NFC Tag when , It will start the corresponding Activity, stay activity Perform corresponding operations in : For example, using label scheduling system , Define specific IntentFilter

if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction)){
    
	// 1.  Definition Tag object 
	Tag detectedTag = getIntent().getParceableExtra(NfcAdapter.EXTRA_TAG);
	// 2.  obtain NDEF news 
	// 3. NDEF  Message parsing 
	// 4.  The final real data display and further operation 

}

Intent Object can get the following information :

  • EXTRA_TAG( necessary ): Representing the scanned label Tag object :Intent.getParcelableExtra(NfcAdapter.EXTRA_TAG) obtain
  • EXTRA_NDEF_MESSAGES( Optional ): Is a machine from the label NDEF An array of messages . This additional message is mandatory in Intent On the object :Intent.getParcebleArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES) Acquired
  • EXTRA_ID( Optional ): Low level of label ID

The above information is guaranteed :Activity It was scanned NFC Intent Object started , Can be in onResume() Add the above code to

NDEF Access to information

String action = intent.getAction();
if(action.equals(NfcAdapter.ACYION_NDEF_DISCOVERED)){
    
	NdefMessage[] message = null;
	//  take Intent The data in is converted to Parcelable data 
	Parcelable[] rawMsgs = intent.getParcelbleArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
	if(rawMsgs != null){
    
		messages = new NdefMessage[rawMsgs.lenth];
		for (int i = 0; i < rawMsgs.length; i++){
    
			//  take  intent The data in is parsed into the corresponding array 
			messages[i] = (NdefMesage)rawMsgs[i];
		}
	}else{
    
		byte[] empty = new byte[];
		NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
		NdefMessage msg = new NdefMessage(new NdefRecord[]{
     record });
		messages = new NdefMessage[]{
    msg};
	}
}else if (action.equals(NfcAdapter.ACYION_TECH_DISCOVERED){
    }
else if (action.equals(NfcAdapter.ACYION_TAG_DISCOVERED){
    }
else {
    }

NDEF Message parsing

//TNF_ABSOLUTE_URI Format   analysis 
byte[] payLoad = record.getPayload();
Uri uri = Uri.parse(new String(payLoad, Charset.forName("UTF-8")));
record.getType();
record.getId();
recode.getTnf();


// TNF_WELL_KNOWN  Format   analysis 

//  Verify that the current record is  RTD_URI  type 
Preconditions.checkArgument(Arrays.equals(record.getType(), NdefRecord.RTD_URI));
//  analysis  payload  data 
byte[] payload = record.getPayload();
String prefix = URI_PREFIX_MAP.get(payload[0]);

//  Splicing the complete recorded data content 
byte[] fulluri = Bytes.concat(prefix.getBytes(
	Charset.forName("UTF-8")), 
	Arrays.copyofRange(payload, 1,1 payload.length));
//  Convert to URI Encapsulated object 
Uri uri = Uri.parse(new String(fulluri, Charset.forName("UTF-8")));

//  Other formats are similar , It's all based on  NDEF  agreement   Data analysis 
// P96  Page has a complete code example 

For the time being

原网站

版权声明
本文为[TowerOs]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200547375254.html