Android NFC Broadcast Tag

I am trying to catch an NFC tag in a broadcast receiver, so I wrote a simple BR that prints "asd" in onReceive (). In the xml manifest, it is described like this:

and I get only this and don’t print at all ....

01-31 16:37:18.980: ERROR/MediaPlayer(990): setAudioStream called in state 8 01-31 16:37:18.980: ERROR/MediaPlayer(990): error (-38, 0) 01-31 16:37:18.980: ERROR/MediaPlayer(990): start called in state 0 01-31 16:37:18.980: ERROR/MediaPlayer(990): error (-38, 0) 01-31 16:37:18.988: ERROR/MediaPlayer(990): Error (-38,0) 

When I use the operation to process the intent as follows:

 <activity android:name="TagViewer" android:theme="@android:style/Theme.NoTitleBar"> <intent-filter> <action android:name="android.nfc.action.TAG_DISCOVERED"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> 

The activity starts and works fine, so how can I get it to work with BroadcastReceiver?

+7
source share
2 answers

You can not. As you pointed out, the NFC adapter uses something very similar to startActivity () to send an intent with tag information in it. This is not exactly what we can do in the Android SDK, as NFC tags are special. For example, you cannot emulate startActivity () on your own for anything other than TAG_DISCOVERED, which is the last action and not very useful.

I think the reason for this is due to the special handling of NFC intentions. When a tag is detected by NFC equipment, it looks for information about what the tag will process. First foreground attempt. He then tries to fulfill the NDEF_DISCOVERED intent, if possible, and looks for activity to accept it. If he cannot find it, he tries to set the intent using TECH_DISCOVERED. Again, if no activity is found, it finally tries TAG_DISCOVERED. If he used broadcast, how could he do this back logic to try to find something to handle the tag? How would he know if something acts on the intent of the tag? And how could he guarantee that only one thing would act on the tag?

+9
source

You can write a small action that does not display any user interface at all, sends a broadcast message, and then ends with a finish (). Using the flags in the manifest, you can avoid its appearance in the story or in the responses and be incapable of the invisible, hoping to get the same effect that you want with the braodcast receiver.

+8
source

All Articles