Java 简单的安卓NFC程序

Java 简单的安卓NFC程序,java,android,nfc,nfc-p2p,Java,Android,Nfc,Nfc P2p,我正在为Android API 14(4.0-冰淇淋三明治)开发一个简单的程序来测试NFC(我将在一个更大的程序中使用)。该程序有两个编辑文本,一个标记为输入,一个标记为输出。目标是,当我轻触两部同时运行应用程序的手机时,输入框中输入的文本将显示在另一部手机的输出框中(这应该是双向的)。代码运行正常,但当我一起轻触手机时,什么也没发生。代码如下 package com.sample.nfctest; import java.sql.Date; import android.app.Activ

我正在为Android API 14(4.0-冰淇淋三明治)开发一个简单的程序来测试NFC(我将在一个更大的程序中使用)。该程序有两个编辑文本,一个标记为输入,一个标记为输出。目标是,当我轻触两部同时运行应用程序的手机时,输入框中输入的文本将显示在另一部手机的输出框中(这应该是双向的)。代码运行正常,但当我一起轻触手机时,什么也没发生。代码如下

package com.sample.nfctest;

import java.sql.Date;

import android.app.Activity;
import android.content.Intent;
import android.nfc.FormatException;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.EditText;
import android.widget.Toast;

public class NFCTestActivity extends Activity {

EditText input;
EditText output;
NfcAdapter mAdapter;
NdefMessage msgs[];
NdefRecord msg;
NfcAdapter nfcAdapter;
boolean somethingHappened = false;
double initialTime;
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    input = (EditText) findViewById(R.id.editText1);
    output = (EditText) findViewById(R.id.editText2);
    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (nfcAdapter == null)
    {
        Toast.makeText(this, "No NFC.  Sucks to suck.", Toast.LENGTH_LONG).show();
        System.out.println("No NFC.  Sucks to suck.");
        return;
    }
    initialTime =  new Date(0).getTime();
    while(!somethingHappened)
    {
        if(30000 < new Date(0).getTime() - initialTime)
            somethingHappened = true;
        onResume();
    }
}
public void onResume() 
{
    super.onResume();
    msg = new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, "text/plain".getBytes(), 
            new byte[0], input.getText().toString().getBytes());
    input.setText("");
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        Intent intent = getIntent();
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if (rawMsgs != null) {
            somethingHappened = true;
            msgs = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; i++) {
                msgs[i] = (NdefMessage) rawMsgs[i];
                output.append(msgs[i].toString());
            }
        }           
    }
    //process the msgs array
    NdefRecord send[] = {msg};
    try{
        nfcAdapter.setNdefPushMessage(new NdefMessage(send), this);
        somethingHappened = true;
    }catch(Exception e){}
}
}
package com.sample.nfctest;
导入java.sql.Date;
导入android.app.Activity;
导入android.content.Intent;
导入android.nfc.FormatException;
导入android.nfc.NdefMessage;
导入android.nfc.NdefRecord;
导入android.nfc.NfcAdapter;
导入android.os.Bundle;
导入android.os.Parcelable;
导入android.widget.EditText;
导入android.widget.Toast;
公共类NFCTESTActive扩展活动{
编辑文本输入;
编辑文本输出;
NfcAdapter mAdapter;
NdefMessage msgs[];
味精;
NfcAdapter NfcAdapter;
布尔SomethingOccessed=false;
双初始时间;
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
输入=(EditText)findViewById(R.id.editText1);
输出=(EditText)findViewById(R.id.editText2);
nfcAdapter=nfcAdapter.getDefaultAdapter(此);
if(nfcAdapter==null)
{
Toast.makeText(这个“没有NFC.Sucks to Sucks.”,Toast.LENGTH_LONG.show();
System.out.println(“没有NFC.Sucks to Sucks.”);
回来
}
initialTime=新日期(0).getTime();
当(!发生了什么事)
{
if(30000<新日期(0).getTime()-initialTime)
发生的事=真的;
onResume();
}
}
恢复时公开作废()
{
super.onResume();
msg=新的NdefRecord(NdefRecord.TNF_ABSOLUTE_URI,“text/plain”.getBytes(),
新字节[0],input.getText().toString().getBytes());
input.setText(“”);
if(NfcAdapter.ACTION\u NDEF\u DISCOVERED.equals(getIntent().getAction())){
Intent=getIntent();
Parcelable[]rawMsgs=intent.getParcelableArrayExtra(NfcAdapter.EXTRA\u NDEF\u消息);
if(rawMsgs!=null){
发生的事=真的;
msgs=新的NdefMessage[rawMsgs.length];
对于(int i=0;i
这是main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="15dp"
        android:text="@string/Inputs"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="19dp" />


    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="19dp"
        android:text="@string/Output"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="18dp" />

</RelativeLayout>

</LinearLayout>

这是舱单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.nfctest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.NFC"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".NFCTestActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
</intent-filter>

<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme=""
    android:host="com.sample.nfctest"
    android:pathPrefix="" />
</intent-filter>

</manifest>

另一方面,有没有办法使用仿真器来测试这样的NFC应用程序?我借了别人的手机进行测试(使用Nexus7和GalaxyNexus)


提前谢谢。

是的,两部手机都启用了NFC。我可以推荐这个例子吗?关于你的最后一个问题,Android emulator上没有NFC,但是你可能想为@gmaster尝试一个名为OpenNFC的外部堆栈。你得到答案了吗。?如果是的话,你能发布它吗?谢谢,两部手机都支持NFC。我可以推荐这个例子吗?关于你的最后一个问题,Android emulator上没有NFC,但是你可能想为@gmaster尝试一个名为OpenNFC的外部堆栈。你得到答案了吗。?如果是,你能把它寄出去吗?谢谢