NfcAdapter.getDefaultAdapter (this) returns null in the emulator

I am trying to check ForegroundDispatch (http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/nfc/ForegroundDispatch.html) in API 10 emulator (Android 2.3. 3).

When I call NfcAdapter.getDefaultAdapter (this), I get null. Why is this so?

the code:

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

My manifest:

 <?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> 

Any ideas?

+7
source share
4 answers

You really can't do anything with the emulator and NFC. You do not want to use the TAG_DISCOVERED action, as this is the last resort action. Ideas that are generated on a real device cannot be faked, as in the NFCDemo demo. NFCDemo was released from 2.3, to the best support for NFC in 2.3.3. This is too bad. There may be some options in the future, but for now, we are all stuck in acquiring an NFC-enabled device to do something with NFC.

+2
source

I think you are looking for this NFC Emulator for android . You will need to create a new avd with this as a target. It seems promising, take a look at it.

Edit: works best / only on Windows :(

+2
source

I would venture to suggest that the emulator does not have NFC or NFC adapters at all.

public static NfcAdapter getDefaultAdapter (context context) Because: API Level 10

Helper to get the default NFC adapter.

Most Android devices will have one NFC adapter (NFC controller).

This helper is equivalent:

Manager NfcManager = (NfcManager) context.getSystemService (Context.NFC_SERVICE); Adapter NfcAdapter = manager.getDefaultAdapter ();

Context call options application context Returns

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

Edit:

It looks like you can do something about it. Check out NFCDemo , it looks like you can create fake tag checks.

0
source

You can change the NFCDemo code (bring it to API level 10 in the manifest and the Eclipse project), and then generate NDEF_DISCOVERED Intents also with NDEF messages attached to the Intent through additional functions.

This may allow you to develop something else for NFC (specifically NDEF, etc.) without real HW.

0
source

All Articles