ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – Android Beam – 如何传输正确的数据?

2019-07-09 18:01:37  阅读:190  来源: 互联网

标签:java android nfc android-beam


我现在正在进行一个android梁项目.我从Android开发人员那里复制了一个测试类.

我的问题是发送正确的数据.如果我将(p2p)与另一台设备连接并按“Tab to beam”,则只有到Android市场的URI到达.
但是,如果我打开浏览器,连接并选项卡,浏览器中的当前URL将会到达.

如何告诉android应该发送哪个视图或文本?我没弄明白.我只想发一个简单的字符串“你好!”例如

这是代码:

package peer.to.peer;

import java.nio.charset.Charset;
import android.app.Activity;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcAdapter.OnNdefPushCompleteCallback;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class Peer2peerActivity extends Activity implements     
CreateNdefMessageCallback,     OnNdefPushCompleteCallback {
   private Button sendButton;
   private EditText textfield;
   NfcAdapter mNfcAdapter;
   private static final int MESSAGE_SENT = 1;


   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);

          //initialisierung
          sendButton=(Button) findViewById(R.id.button);
          textfield= (EditText) findViewById(R.id.textfield);

          //mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

          //if(mNfcAdapter==null) toast("NFC ist nicht vorhanden");
          //else toast("NFC ist  vorhandne");


          toast("start");

          sendButton.setOnClickListener(mTagWriter);
   }

   //lokaler OnClickListener für den Button
   private View.OnClickListener mTagWriter = new View.OnClickListener() {
    public void onClick(View arg0) {
        // Write to a tag for as long as the dialog is shown.
        //disableNdefExchangeMode();
        //enableTagWriteMode();

          toast(textfield.getText().toString());
    }
   };
   //notification-methode
   public void toast(String text){
          Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
   }

   public void onNdefPushComplete(NfcEvent arg0) {
          // A handler is needed to send messages to the activity when this
    // callback occurs, because it happens from a binder thread
    mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();         
   }

   public NdefMessage createNdefMessage(NfcEvent event) {
    String text = (textfield.toString());
    NdefRecord uriRecord = new NdefRecord(
              NdefRecord.TNF_ABSOLUTE_URI ,
              "http://www.google.de".getBytes(Charset.forName("US-ASCII")),
              new byte[0], new byte[0]);
    NdefMessage msg = new NdefMessage(
            new NdefRecord[] { uriRecord
    //createMimeRecord("text/plain", 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;

   }

   /**
 * Creates a custom MIME type encapsulated in an NDEF record
 *
 * @param mimeType
 */
public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
    byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
    NdefRecord mimeRecord = new NdefRecord(
            NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
    return mimeRecord;
}

/** This handler receives a message from onNdefPushComplete */
private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case MESSAGE_SENT:
            Toast.makeText(getApplicationContext(), "Message sent!",     Toast.LENGTH_LONG).show();
            break;
        }
    }
};
}

提前致谢

浆果

解决方法:

您正在获取URI,因为第一条记录是指向google.de的URI记录.将uriRecord替换为对createMimeRecord()的调用,您应该获得将mime类型设置为text / plain的文本.

标签:java,android,nfc,android-beam
来源: https://codeday.me/bug/20190709/1415585.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有