【问题标题】:NfcAdapter.getDefaultAdapter(this) returns null in emulatorNfcAdapter.getDefaultAdapter(this) 在模拟器中返回 null
【发布时间】:2011-07-09 00:14:09
【问题描述】:

我正在尝试在模拟器 API 10(Android 2.3. 3).

当我调用 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>

有什么想法吗?

【问题讨论】:

    标签: android android-emulator nfc


    【解决方案1】:

    我想你正在寻找这个NFC Emulator for android。 您需要创建一个以此为目标的新 avd。这看起来很有希望,看看吧。

    编辑:在 windows 环境下效果最好/仅 :(

    【讨论】:

      【解决方案2】:

      您可以修改 NFCDemo 代码(在清单和 Eclipse 项目中将其提升到 API 级别 10),然后让它生成 NDEF_DISCOVERED Intents,并通过附加功能将 NDEF 消息附加到 Intent。

      这可以让您在没有真正硬件的情况下为 NFC(特别是 NDEF 等)开发更多内容。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

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

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

          帮助获取默认 NFC 适配器。

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

          这个助手相当于:

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

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

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

          编辑:

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

          【讨论】:

          • 那么安卓模拟器里就没有办法玩NFC了?
          • @DixieFlatline 看起来你可以模拟它。更新了答案。
          • 我已经在使用 fakeTagsActivity 来模拟标签,但是我在获取 nfc 适配器时遇到了问题(=读取标签的芯片的抽象)
          猜你喜欢
          • 2016-10-06
          • 1970-01-01
          • 1970-01-01
          • 2020-04-29
          • 2012-12-05
          • 1970-01-01
          • 1970-01-01
          • 2021-12-24
          • 2022-01-11
          相关资源
          最近更新 更多