StErMi
StErMi

Reputation: 5469

Android NFC not working with ICS

yesterday I watched Google IO Talk about NFC and today I'm trying to do some fun things with it.

At the moment I've a Galaxy Nexus (ICS 4.0.2) and Nexus S (ICS 4.0.3). I've installed the sample application of that talk: http://nfc.android.com/StickyNotes.zip

I'm also following this how-to in order to understand the flow with some comments: http://www.jessechen.net/blog/how-to-nfc-on-the-android-platform/

What I'm trying to do is to send in p2p way a message from a phone to another and change the edittext content (like the example has to do!).

I think that the NFC Message is going out but the other phone is not grabbing it... Someone know why?

This is the logcat from one of the phone (when those 2 phones is contacting each other):

03-03 15:37:47.842: W/NFC-LLC(446): bad LLC length byte d0
03-03 15:37:47.842: W/NFC-LLC(446): bad LLC length byte a6
03-03 15:37:47.842: W/NFC-LLC(446): bad LLC length byte 42
03-03 15:37:47.842: W/NFC-LLC(446): bad LLC length byte 1
03-03 15:37:47.842: W/NFC-LLC(446): bad LLC length byte fc
03-03 15:37:47.842: W/NFC-LLC(446): bad LLC length byte c1
03-03 15:37:48.264: D/NFC JNI(446): Discovered P2P Target
03-03 15:37:48.264: D/NfcService(446): LLCP Activation message
03-03 15:37:48.264: D/NfcService(446): NativeP2pDevice.MODE_P2P_TARGET
03-03 15:37:48.296: I/NFC JNI(446): LLCP Link activated (LTO=150, MIU=128, OPTION=0x00, WKS=0x01)
03-03 15:37:48.296: D/NfcService(446): Initiator Activate LLCP OK
03-03 15:37:48.296: I/NfcP2pLinkManager(446): LLCP activated
03-03 15:37:48.296: D/NfcP2pLinkManager(446): onP2pInRange()
03-03 15:37:48.467: D/dalvikvm(446): GC_FOR_ALLOC freed 3192K, 18% free 17750K/21575K, paused 19ms
03-03 15:37:48.467: I/dalvikvm-heap(446): Grow heap (frag case) to 20.515MB for 3265936-byte allocation
03-03 15:37:48.514: D/dalvikvm(446): GC_CONCURRENT freed <1K, 3% free 20939K/21575K, paused 4ms+3ms
03-03 15:37:51.264: I/NFC JNI(446): LLCP Link deactivated
03-03 15:37:51.264: D/NfcService(446): LLCP Link Deactivated message. Restart polling loop.
03-03 15:37:51.264: D/NfcService(446): disconnecting from target
03-03 15:37:51.264: I/NfcP2pLinkManager(446): LLCP deactivated.
03-03 15:37:52.022: D/NfcP2pLinkManager(446): Debounce timeout
03-03 15:37:52.022: D/NfcP2pLinkManager(446): onP2pOutOfRange()

UPDATE

I've made some changes to default Sticky Notes code in order to upgrade it to support ICS NFC API. Infact if you go to the documentation you can see this: http://developer.android.com/reference/android/nfc/NfcAdapter.html#enableForegroundNdefPush%28android.app.Activity,%20android.nfc.NdefMessage%29

This method is deprecated. use setNdefPushMessage(NdefMessage, Activity, Activity...) instead

Strongly recommend to use the new setNdefPushMessage(NdefMessage, Activity, Activity...) instead: it automatically hooks into your activity life-cycle, so you do not need to call enable/disable in your onResume/onPause.

Also I read this: For NDEF push to function properly the other NFC device must support either NFC Forum's SNEP (Simple Ndef Exchange Protocol), or Android's "com.android.npp" (Ndef Push Protocol). This was optional on Gingerbread level Android NFC devices, but SNEP is mandatory on Ice-Cream-Sandwich and beyond.

I do not understand if using setNdefPushMessage it will automatically support SNEP protocol of if I have to make other changes. At the moment as I said I'm using a Galaxy Nexus (4.0.2) and a Nexus S (4.0.3) and I've no other kind of devices to makes tests.

I've also implemented the onNdefPushComplete for the adapter that is sending messages and for what I can see its never called.. I don't understand if this is a problem about APIS, Devices, or my code. So what I don't understand is: if everything is working fine, when I change the text of the Sticky Note edit text and I link the 2 devices with NFC, the text will be sended to the other device? Is this the example?

The really strange thing is that I cannot find examples about how to do NFC with ICS and the official documentation doesn't explain it so well...

Someone know how to solve?

Upvotes: 0

Views: 3357

Answers (3)

StErMi
StErMi

Reputation: 5469

Thank you for your answer.

As I said in a answer's comment I was making a big mistake.

In ICS when you enable the NFC option it will enable your "read" function. When you enable the Android Beam feature it will also enable the "write" function.

I was disabling the Android Beam feature so it was the big problem!

Upvotes: 1

user1236001
user1236001

Reputation: 73

I tried out the StickyNotes Demo as it was (without any code changes) on two Galaxy Nexus phones (ICS 4.0.1) and it works well (if you do not touch the "Write to Tag"-Button !!!).

It simply sends the edited Text to the other phone, when you confirm the "Touch to Beam"-Screen by tapping on the screen.

I made no changes, it is still on android:minSdkVersion="10" (android 2.3.3).

Upvotes: 1

NFC guy
NFC guy

Reputation: 10228

The detection of NPP and/or SNEP is completely automatic, done inside the NFC software stack and hidden from apps. Apps don't need to be aware of this and setNdefPushMessage() and setNdefPushMessageCallback() will result in your NDEF message being sent via SNEP if both devices support it or NPP otherwise.

Your problem may have to do with the fact that the NFC antennas in the Nexus S and the Galaxy Nexus are in slightly different places. In the Nexus S it is more to towards the upper part of the backside (the antenna is located in the inside of the back of your phone; open it up to see it), while in the Galaxy Nexus it has been integrated in the battery, which is placed more towards the bottom part of the backside.

When the 2 phones connect over NFC, you should hear a sound (volume controlled by the ringer volume) and the screen should "zoom out", while showing the message "Touch to beam". (Of course, you should make sure that NFC and Beam are turned on in the settings of the phone.)

Upvotes: 1

Related Questions