简单的Android NFC计划

时间:2012-07-31 15:09:08

标签: java android nfc nfc-p2p

我正在开发一个简单的Android API 14程序(4.0 - Ice Cream Sandwich)来测试NFC(我将在更大的程序中使用它)。该程序有两个EditTexts,一个标记输入,一个标记输出。目标是,当我点击两个同时运行应用程序的手机时,输入框中输入的文本将出现在另一部手机的输出框中(这应该是双向的)。代码运行正常,但当我一起点击手机时没有任何反应。代码如下。

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){}
}
}

这是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应用程序?我借用某人的手机进行测试(使用Nexus 7和Galaxy Nexus)。

提前致谢。

0 个答案:

没有答案