Android的读/写于多个记录NDEF NFC [英] Android Read/Write to Multiple Records NDEF NFC

查看:227
本文介绍了Android的读/写于多个记录NDEF NFC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写和在NDEFMessage多个记录读取/。是我的$ C $其中,C吧?另外,我的进程终止本身,当我读标签。我不知道我在哪里出了错。

写作部分:

 私人NdefMessage getTagAsNdef(){
           字符串text123 =你好;
            //布尔addAAR = TRUE;
            字符串UNIQUEID =starbucks.com;
         字节[] = uriField uniqueId.getBytes(Charset.forName(US-ASCII));
         字节[] =有效载荷新的字节[uriField.length + 1]; //加1为URI preFIX
         有效载荷[0] = 0×01; // prefixes HTTP:// WWW。的URI
         System.arraycopy(uriField,0,有效载荷,1,uriField.length); // URI追加有效载荷
         NdefRecord rtdUriRecord =新NdefRecord(
           NdefRecord.TNF_WELL_KNOWN,NdefRecord.RTD_URI,新的字节[0],负载);
         //无线网络连接测试写入code
         字节[] = textbytes text123.getBytes();
         NdefRecord textRecord =新NdefRecord(NdefRecord.TNF_WELL_KNOWN,
                 text123.getBytes(),新的字节[] {},textbytes);
         如果(android.os.Build.VERSION.SDK_INT> = android.os.Build.VERSION_ codeS.ICE_CREAM_SANDWICH){
              //注:返回AAR为不同的应用程序(couponmanager)
              返回新NdefMessage(新NdefRecord [] {
           rtdUriRecord,textRecord,NdefRecord.createApplicationRecord(com.example.ponpon)
         });
         }其他{
              返回新NdefMessage(新NdefRecord [] {
                   rtdUriRecord,textRecord});
         }
       }

阅读部分:

  @覆盖
        保护无效onNewIntent(意向意图){
            super.onNewIntent(意向);
            共享preferences preferences = getShared preferences(preF_FILE_NAME,MODE_PRIVATE);
            诠释存储preference = preferences.getInt(storedInt,0);            如果(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())){
                NdefMessage []消息= NULL;
                Parcelable [] = rawMsgs intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
                如果(rawMsgs!= NULL){
                    消息=新NdefMessage [rawMsgs.length]
                    的for(int i = 0; I< rawMsgs.length;我++){
                        消息[I] =(NdefMessage)rawMsgs [I]
                    }
                }
                如果(讯息[0]!= NULL){
                    字符串结果=;
                    字节[] =有效载荷的消息[0] .getRecords()[0] .getPayload();                    //这个假设,我们找回我SOH其次是主机/ code
                    对于(INT B = 1; B< payload.length; b ++){//跳过SOH
                        结果+ =(char)的有效载荷[B]。
                    }
                    //抓住第二个有效载荷
                    字符串RESULT2 =;
                    字节[] payload2 =消息[0] .getRecords()[1] .getPayload();
                    对于(INT测试= 1;测试< payload2.length;试验++){//跳过SOH
                        结果2 + =(char)的payload2 [测试]
                        Toast.makeText(这一点,结果2,Toast.LENGTH_SHORT).show();                    }


解决方案

你跳过第一个字节即H.
在第一个记录你pre-未决读取数据时,你正确地跳过一个字节。

至于速度我那只是操作系统缓冲祝酒词

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();  

                    }

解决方案

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

这篇关于Android的读/写于多个记录NDEF NFC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆