Android Read/Write to Multiple Records NDEF NFC

我与影子孤独终老i 提交于 2019-12-13 02:51:35

问题


I'm trying to write and read to/from multiple records in a NDEFMessage. Is my code here right? Also, my process terminates itself when I'm reading the tag. I'm not sure where I went wrong..

Writing part:

private NdefMessage getTagAsNdef() {  
           String text123="Hello";
            //boolean addAAR = true;  
            String uniqueId = "starbucks.com";      
         byte[] uriField = uniqueId.getBytes(Charset.forName("US-ASCII"));  
         byte[] payload = new byte[uriField.length + 1];       //add 1 for the URI Prefix  
         payload[0] = 0x01;                        //prefixes http://www. to the URI  
         System.arraycopy(uriField, 0, payload, 1, uriField.length); //appends URI to payload  
         NdefRecord rtdUriRecord = new NdefRecord(  
           NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], payload);  
         //Wi-FI test writing code
         byte[] textbytes = text123.getBytes();
         NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, 
                 text123.getBytes(), new byte[]{}, textbytes);


         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {  
              // note: returns AAR for different app (couponmanager)  
              return new NdefMessage(new NdefRecord[] {  
           rtdUriRecord, textRecord, NdefRecord.createApplicationRecord("com.example.ponpon")  
         });   
         } else {  
              return new NdefMessage(new NdefRecord[] {  
                   rtdUriRecord,textRecord});  
         }  
       }  

Reading part:

@Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);      
            SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
            int storedPreference = preferences.getInt("storedInt", 0);

            if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
                NdefMessage[] messages = null;
                Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
                if (rawMsgs != null) {
                    messages = new NdefMessage[rawMsgs.length];
                    for (int i = 0; i < rawMsgs.length; i++) {
                        messages[i] = (NdefMessage) rawMsgs[i];
                    }
                }
                if(messages[0] != null) {
                    String result="";
                    byte[] payload = messages[0].getRecords()[0].getPayload();



                    // this assumes that we get back am SOH followed by host/code
                    for (int b = 1; b<payload.length; b++) { // skip SOH
                        result += (char) payload[b];
                    }


                    //grabbing 2nd payload
                    String result2="";
                    byte[] payload2 = messages[0].getRecords()[1].getPayload();
                    for (int test = 1; test<payload2.length; test++) { // skip SOH
                        result2 += (char) payload2[test];
                        Toast.makeText(this,result2,Toast.LENGTH_SHORT).show();  

                    }

回答1:


You're skipping the first byte i.e. the H. In the first record you're pre-pending a byte which you correctly skip when reading.

As for speed I thats just the OS buffering the toasts



来源:https://stackoverflow.com/questions/13633383/android-read-write-to-multiple-records-ndef-nfc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!