2011-03-04 55 views
7

我想測試ForegroundDispatch(http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/nfc/ForegroundDispatch。 html)在模擬器API 10(Android 2.3.3)中。NfcAdapter.getDefaultAdapter(this)在模擬器中返回null

當我調用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> 

任何想法?

回答

0

我猜測模擬器根本沒有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,看起來像你可以生成標籤的假掃描。

+0

所以沒有辦法在Android模擬器與NFC玩 – DixieFlatline 2011-03-04 17:47:23

+0

@DixieFlatline貌似可以嘲笑它更新了答案 – 2011-03-04 17:50:12

+0

我alredy使用fakeTagsActivity?。來模擬標籤,但我有問題獲得nfc適配器(=抽象的芯片,讀取標籤) – DixieFlatline 2011-03-04 17:55:31

2

你真的不能做任何事情與仿真器和NFC有趣。您不希望使用TAG_DISCOVERED操作,因爲這是最後一招的操作。在真實設備上生成的意圖不能像NFCDemo演示中那樣僞造。 NFCDemo在2.3.3發佈之前就已經發布了,而在2.3.3更好地支持NFC之前。這太糟糕了。也許未來會有一些選擇,但現在我們都堅持不得不購買一臺支持NFC的設備來處理NFC。

0

您可以修改NFCDemo代碼(在清單和Eclipse項目中將其打到API級別10),然後讓它生成NDEF_DISCOVERED意圖,並通過附加內容將NDEF消息附加到意圖。

這可以讓你開發一些NFC(特別是NDEF等),而沒有真正的硬件。

2

我認爲你正在尋找這個NFC Emulator for android。 您需要創建一個新的avd作爲目標。這看起來很有希望,請看看它。

編輯:工作最好/只適用於Windows環境:(

相關問題