当前位置:编程学习 > wap >>

最近在研究NFC beam,有个疑问能不能发送端在一个程序内运行,然后接受端也是在这个程序内,两个手机之间传递简单的字符信息呢?如果能实现的话,能不能教教我?

我已经实现了发送端的代码,但是接收端应该怎样写呢?不解
package cn.swufe.spay.spayphone;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;

import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;

import android.nfc.NfcEvent;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import java.nio.charset.Charset;
import java.util.Locale;

public class P2PbeamActivity extends Activity implements
CreateNdefMessageCallback {
NfcAdapter mNfcAdapter;
private EditText e1;
private TextView t1;
private CheckBox c1;
private CheckBox c2;
private CheckBox c3;
boolean bool;
private int i;
private NdefMessage msg;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
e1 = (EditText) findViewById(R.id.editText1);
t1 = (TextView) findViewById(R.id.textView);
c1 = (CheckBox) findViewById(R.id.checkBox1);
c2 = (CheckBox) findViewById(R.id.checkBox2);
c3 = (CheckBox) findViewById(R.id.checkBox3);
bool = true;
i = 0;

c1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (c1.isChecked()) {
i = 1;
}

}

});

c2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
i = 2;
}
});

c3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
i = 3;
}
});

mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null) {

}
// Register callback to set NDEF message
mNfcAdapter.setNdefPushMessageCallback(this, this);
// Register callback to listen for message-sent success
}

public NdefMessage createNdefMessage(NfcEvent event) {

String text = e1.getText().toString();

switch (i) {
case 1:

NdefMessage msg = new NdefMessage(
new NdefRecord[] { createMimeRecord("", text.getBytes())

// ,NdefRecord.createApplicationRecord("com.example.android.nfc")
});
return msg;
case 2:

NdefMessage msg1 = new NdefMessage(
new NdefRecord[] { creatUriRecord("01", text.getBytes())

// ,NdefRecord.createApplicationRecord("com.example.android.nfc")
});
return msg1;

case 3:

// Locale English = null;
NdefMessage msg2 = new NdefMessage(
new NdefRecord[] { createTextRecord(text, Locale.ENGLISH,
bool)

// ,NdefRecord.createApplicationRecord("com.example.android.nfc")
});
return msg2;
}
;

return msg;

}

public NdefRecord createTextRecord(String payload, Locale locale,
boolean encodeInUtf8) {
byte[] langBytes = locale.getLanguage().getBytes(
Charset.forName("US-ASCII"));
Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset
.forName("UTF-16");
byte[] textBytes = payload.getBytes(utfEncoding);
int utfBit = encodeInUtf8 ? 0 : (1 << 7);
char status = (char) (utfBit + langBytes.length);
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);
return record;
}

private 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;

}

public NdefRecord creatUriRecord(String uriType, byte[] payload) {
byte[] uriBytes = uriType.getBytes(Charset.forName("US-ASCII"));
NdefRecord creatUriRecord = new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI,
uriBytes, new byte[0], new byte[0]);
return creatUriRecord;
}
@Override
public void onNewIntent(Intent intent) {
// onResume gets called after this to handle the intent
setIntent(intent);
}
}
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,