8

我正在尝试在模拟器 API 10(Android 2.3.3)中测试 ForegroundDispatch(http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/nfc/ForegroundDispatch.html)。

当我调用 NfcAdapter.getDefaultAdapter(this) 时,我得到空值。为什么会这样?

代码:

public class ForegroundDispatch extends Activity {
private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;
private IntentFilter[] mFilters;
private String[][] mTechLists;
private TextView mText;
private int mCount = 0;

@Override
public void onCreate(Bundle savedState) {
    super.onCreate(savedState);

    setContentView(R.layout.foreground_dispatch);
    mText = (TextView) findViewById(R.id.text);
    mText.setText("Scan a tag");

    mAdapter = NfcAdapter.getDefaultAdapter(this);

    // Create a generic PendingIntent that will be deliver to this activity. The NFC stack
    // will fill in the intent with the details of the discovered tag before delivering to
    // this activity.
    mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    // Setup an intent filter for all MIME based dispatches
    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndef.addDataType("*/*");
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }
    mFilters = new IntentFilter[] {
            ndef,
    };

    // Setup a tech list for all NfcF tags
    mTechLists = new String[][] { new String[] { NfcF.class.getName() } };
}

@Override
public void onResume() {
    super.onResume();
    mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists); //CRASHES HERE BECAUSE mAdapter IS NULL
}

@Override
public void onNewIntent(Intent intent) {
    Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);
    mText.setText("Discovered tag " + ++mCount + " with intent: " + intent);
}

@Override
public void onPause() {
    super.onPause();
    mAdapter.disableForegroundDispatch(this);
 }
  }

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.neka.znacka"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.NFC"></uses-permission>
<uses-feature android:name="android.hardware.nfc" android:required="true" />

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

    <activity android:name="Simulator">
    </activity>

</application>

有任何想法吗?

4

4 回答 4

2

你真的不能用模拟器和 NFC 做任何有趣的事情。您不想使用 TAG_DISCOVERED 操作,因为这是不得已的操作。在真实设备上生成的意图不能像 NFCDemo 演示中那样被伪造。NFCDemo 是在 2.3 中发布的,在 2.3.3 中更好地支持 NFC 之前。这太糟糕了。也许将来会有一些选择,但现在我们都被困在必须购买支持 NFC 的设备才能使用 NFC 做任何事情。

于 2011-03-15T17:45:33.317 回答
2

I think you are looking for this NFC Emulator for android. You'll need to create a new avd with this as the target. This appears to be promising, have a look at it.

Edit: Works best/only on windows environment :(

于 2011-07-19T17:50:52.630 回答
1

You can modify the NFCDemo code (bump it to API level 10 in the manifest and Eclipse project) and then have it generate NDEF_DISCOVERED Intents also, with NDEF messages attached to the Intent through extras.

That can allow you to develop some more for NFC (specifically NDEF etc) without having real HW.

于 2011-04-25T18:21:56.113 回答
0

我敢猜测模拟器根本没有 NFC 适配器或 NFC 功能。

public static NfcAdapter getDefaultAdapter (Context context) 自:API 级别 10

获取默认 NFC 适配器的助手。

大多数 Android 设备只有一个 NFC 适配器(NFC 控制器)。

这个助手相当于:

NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE); NfcAdapter 适配器 = manager.getDefaultAdapter();

参数 context 调用应用程序的上下文 返回

* the default NFC adapter, or null if no NFC adapter exists

编辑:

看起来你可以做一些事情来玩它。查看NFCDemo,看起来您可以生成虚假的标签扫描。

于 2011-03-04T17:42:19.687 回答