1

I have an NFC reader ACS ACR122U and I want to send data from my Android phone to it.

I have found many examples on how to connect my two devices in P2P mode and how to send data from my phone to the ACR122U, but my desktop server (ACR122U) always receives a SNEP PUT request. I would send a GET request but I can find only examples that use Android built in NDEF system without specifing PUT or GET request.

How can I package and send a SNEP GET request from my Android device to an ACR122U?

Michael Roland
  • 39,663
  • 10
  • 99
  • 206
firegloves
  • 5,581
  • 2
  • 29
  • 50

1 Answers1

2

Short answer: You can't.

The Android API does not allow you to implement custom LLCP services. The only LLCP services implemented on Android devices are the NFC Forum Default SNEP Server (service name urn:nfc:sn:snep, service access point address 4) and, for legacy reasons, the Android NDEF push protocol (similar to SNEP but dates back to times before the SNEP specification was published).

While the SNEP protocol specification defines a GET request to pull an NDEF message from another device, the NFC Forum Default SNEP Server is defined to only accept PUT requests. GET requests must be rejected by that SNEP server.

From the NFC Forum Simple NDEF Exchange Protocol technical specification (Version 1.0):

The default SNEP server provides a logical inbox. A client connected to the default server can place NDEF messages into the inbox using Put request messages. [...]

The default server SHALL NOT accept Get requests. The appropriate response for a Get request message is Not Implemented.

So you can only push NDEF messages to the NFC Forum Default SNEP Server and not the other way round.

Hence, if you want to send an NDEF message to an Android device through peer-to-peer mode, you need to implement a SNEP client on the ACR122U side. You can then let that client connect to the SNEP server on the Android device (at service name urn:nfc:sn:snep) and push the NDEF message to it using a PUT request.

If you want to receive an NDEF message from an Android device through peer-to-peer mode, you need to implement a SNEP server on the ACR122U side. The Android SNEP client will then connect to your SNEP server and push the waiting NDEF message (e.g. registered through NfcAdapter.setNdefPushMessage()) to it using, again, a PUT request.

Michael Roland
  • 39,663
  • 10
  • 99
  • 206
  • Ok, but if I implement SNEP Server on ACR122U side? – firegloves Jan 02 '17 at 10:19
  • I think in an android beam communication between two android devices, one of them must act as server and the other one must act as client, right? So if an android device can act as SNEP client, why in my usecase I can't use my ACR122U as server and my Android phone as client? Otherwise, do you know any example on how to start implementing my SNEP server on Android? – firegloves Jan 02 '17 at 11:29
  • @firegloves On top of LLCP, both can act as servers and clients on the same link. In fact, Android impements both the client and the server for SNEP. As I wrote in my post above, if you want to transfer an NDEF message from ACR122U to Android, you need to implement the SNEP client at the ACR122U side that connects and PUTs to the SNEP server that's already there on Android devices. – Michael Roland Jan 02 '17 at 13:33
  • Ok thank you for your reply, but what I want is to send data from Android and retrieve the response from ACR122U. For this reason I don't want to use a PUT, and don't want to use Android as server. So, the answer is that with Android I can't package a GET request also having a custom SNEP server on my ACR122U? – firegloves Jan 02 '17 at 14:49
  • 1
    @firegloves In that case, you need to implement both, a SNEP server and a client. First receive the NDEF message from Android through your SNEP server, then connect with your SNEP client to the SNEP server on the Android side and push your "response" message to it. – Michael Roland Jan 02 '17 at 14:54
  • Ok thank you, I'll try this way. But I can't understand why they have locked this functionality – firegloves Jan 02 '17 at 16:45
  • @firegloves One can only speculate on why the NFC Forum limited this (maybe [Stephen Tiedemann](http://stackoverflow.com/users/2622050) reads this and can provide some more definite answer). In my opinion, they probably did this to simplify implementations. After all, the SNEP specification does not define any actions to treat received NDEF messages (hence, there is no need for bidirectional NDEF exchange in a linked manner). – Michael Roland Jan 02 '17 at 17:28
  • I have understood your reasoning, but I can't undestand why I can't send GET requests from Android. I can read in NFC Forum Snep spec that GET request is already defined. I think the default server MUST implement at least PUT requests management, because a generic implementation is not useful, but it is implementable. So if I can manage GET requests from my server I may want to send these request. May be there is a generic java solution to make and send them? – firegloves Jan 02 '17 at 18:44
  • @firegloves Not on the Android side since there is no API for peer-to-peer (LLCP) communication. – Michael Roland Jan 02 '17 at 19:06
  • I have found this resource https://android.googlesource.com/platform/packages/apps/Nfc/+/525c260303268a83da4c3413b953d13c9084e834/src/com/android/nfc/snep/SnepClient.java have you seen that? – firegloves Jan 02 '17 at 19:18
  • @firegloves Yes, that's the Android SNEP client that PUTs messages to the SNEP server on another device (e.g. for messages registered through `NfcAdapter.setNdefPushMessage()`) as I wrote in my post. It's used from [P2pLinkManager](https://android.googlesource.com/platform/packages/apps/Nfc/+/master/src/com/android/nfc/P2pLinkManager.java). – Michael Roland Jan 02 '17 at 19:42
  • The reason why this version also seems to implement GET requests is explained here: [P2pLinkManager.java#896](https://android.googlesource.com/platform/packages/apps/Nfc/+/794585174b5812f47c4fbc3cc34184f4379bdbb8/src/com/android/nfc/P2pLinkManager.java#896). But that seems to be only usable for connection handover. – Michael Roland Jan 02 '17 at 19:43
  • Worked with multiple PUT requests. It's a bit more complex because I had to augment complexity of my exchange protocol, but it works. Thanks – firegloves Jan 05 '17 at 18:49
  • I wan to send multiple ndef messages with put from android? Did you manage to do it with the custom code or just by NfcAdapter.setNdefPushMessage() – user257980 Jan 15 '20 at 12:38