NfcAdapter.getDefaultAdapter(this)在模拟器中返回null

Dix*_*ine 7 android nfc android-emulator

我试图在模拟器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)时,我得到null.为什么会这样?

码:

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);
 }
  }
Run Code Online (Sandbox Code Playgroud)

我的清单:

<?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>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Rob*_*ond 0

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

公共静态 NfcAdapter getDefaultAdapter(上下文上下文)自:API 级别 10

帮助获取默认 NFC 适配器。

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

这个助手相当于:

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

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

* the default NFC adapter, or null if no NFC adapter exists
Run Code Online (Sandbox Code Playgroud)

编辑:

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