Need help for communication between SCL3711 & Nexus S‏

Asked by Giovanni Palmer

Dear All,

a few days ago I started using the nfcpy library and I'm having trouble understanding how it works.

My need is to send the ID of the smartphone Nexux S to SCL3711 and then to receive an ACK from SCL3711 on my smartphone.

Is there any ready example (in the "examples" folder) that could help me to do that?
Could you suggest me a way to do that or at least where to focus my attention?

Thank you for the attention

Question information

Language:
English Edit question
Status:
Answered
For:
nfcpy Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Stephen Tiedemann (stephen-tiedemann) said :
#1

You can encapsulate your ID into an NDEF text record and send it using Android Beam (see http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#p2p to learn about Android Beam). Implement a SNEP server with nfcpy to receive that message (the nfcpy documentation explains that at http://nfcpy.readthedocs.org/en/latest/api/snep.html). To send back some sort of acknowledgement in the same session you can start possibly with the code at http://pastebin.com/up6d7H9b and look up the discussion at https://answers.launchpad.net/nfcpy/+question/235366 which lead to that example.

Revision history for this message
Giovanni Palmer (giovanni-palama) said :
#2

I used the "Android-Beam-Sample" on my Nexux-S and now I'm able to transfer the ID from my phone to the SCL3711.

In particular, I use the "snep-test-server.py" example to do that and I launch it by typing the follow instruction on my linux shell:

 sudo python snep-test-server.py --mode t

and I obtain the Nexus ID on my SCL3711.

I've tried to understand and work with the Pastebin code you've posted above, but I don't understand how to send a kind of ACK or a simple "Hello" string from the SCL3711 to my phone.

Should I modify a little bit the "snep-test-server.py" or find a way to execute first "snep-test-server.py" and after "snep-test-client.py" on linux side? (where my SCL3711 is attached)

Should I find a way to incapsulate the "snep-test-server.py" into my Android Code?

Please guide me into understanding how to do that and which is the simplest way to obtain what I posted above.

Any help is much appreciated!!

Revision history for this message
Stephen Tiedemann (stephen-tiedemann) said :
#3

If you can wait a little, there'll be a new beam.py example script coming next week. That'll include a receive-something/send-back-something-else function.

Revision history for this message
Giovanni Palmer (giovanni-palama) said :
#4

Ok, I'll wait.

Meanwhile, could you give me an example of Android snippet code which receives an NFC Text record (sent through nfcpy) and visualize it on the smartphone's screen?

Thank you Mr. Tiedemann

Revision history for this message
Nacho Álvarez (neonigma) said :
#5

Please, take a look to Android Beam Demo. Check it in your $ANDROID_SDK_DIR/examples, or online here: http://www.androidside.com/docs//resources/samples/AndroidBeamDemo/index.html

In Beam.java, change this:

        String text = ("Beam me up!\n\n" +
                "Beam Time: " + time.format("%H:%M:%S"));
        NdefMessage msg = new NdefMessage(
                new NdefRecord[] { createMimeRecord(
                        "application/com.example.android.beam", text.getBytes())

with this:

String text = "*MYTAG*-Hello World!";
        NdefMessage msg = new NdefMessage(
                new NdefRecord[] { createMimeRecord(
                        "text/plain", text.getBytes())

Then in snep-test-server.py, I think in the put method of DefaultServer, get this message like this (I can't check this right now):

ndef_message_received = str(ndef_message)
data_received = ndef_message_received.split("*MYTAG*-")[1]
print ndef_message_received

Revision history for this message
Giovanni Palmer (giovanni-palama) said :
#6

Good morning neonigma, I've just tried to modify both Android and Python files with your snippets but it's the same as before, that is, I only obtain the transfer of the ID from my Nexus to the SCL3711 and I get the "Message sent" toast on my Nexus.

My need is to get the a "hello" message from the SCL3711 to the smartphone and visualize it on my Nexus's screen.

If I launch the "snep-receive-and-put-twice" on my bash console, adding the "--mode i" at the end, my nexus receive the NDEF message but asks to me "Open with".

It seems like my smartphone doesn't know which action has to perform when receives a simple NDEF message.

How can I change the behaviour of my Nexus when receiveing a simple NDEF message from the SCL3711?

Thank you in advance

Revision history for this message
Nacho Álvarez (neonigma) said :
#7

You have to correctly define the intent filter as this:

<intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="application/com.example.android.beam" />
                <data android:mimeType="text/plain" />
</intent-filter>

Important here: data mimeType. I've defined com.example.android.beam as my package and text/plain as my interchange format. On this way, your Nexus must print the NDEF message automatically in a textview or whatever.

Cheers!

Revision history for this message
Giovanni Palmer (giovanni-palama) said :
#8

I've tried the changes you posted above, but nothing seems to happen.

Now it seems like the app knows what to do, but visualize an empty screen and after a little while it comes back to the main screen and visualize the "Message sent!" toast.

I post the Python code snippet modified as you suggested:
(i launch it on the bash console in this way: "sudo python snep-test-server.py --mode i" )

import logging
log = logging.getLogger('main')
import os
import sys
import time
import argparse

sys.path.insert(1, os.path.split(sys.path[0])[0])
from cli import CommandLineInterface

import nfc
import nfc.snep
import nfc.ndef

class DefaultServer(nfc.snep.SnepServer):
    def __init__(self, llc):
        service_name = 'urn:nfc:sn:snep'
        super(DefaultServer, self).__init__(llc, service_name)

    def put(self, ndef_message):

 ndef_message_received = str(ndef_message)
 data_received = ndef_message_received.split("*MYTAG*-")[1]
 print ndef_message_received
        log.info("default snep server got put request")
        log.info("ndef message length is {0} octets".format(len(ndef_message)))
        log.info(nfc.ndef.Message(ndef_message).pretty())
        return nfc.snep.Success

What do you think could be the problem?

Thank You

Revision history for this message
Stephen Tiedemann (stephen-tiedemann) said :
#9

Please have a look at the new examples/beam.py script, specifically the "recv send" action should be close to what you like to achieve.

Revision history for this message
Giovanni Palmer (giovanni-palama) said :
#10

Could you provide a command line example of how to invoke the "beam.py" file?

I use the following command on my Ubuntu Shell:

    sudo python beam.py --mode t --lto 600 --listen-time 300 send print

but it doesn't work. What's wrong with me?

Revision history for this message
Stephen Tiedemann (stephen-tiedemann) said :
#11

With beam.py you could do:

$ echo -n "My Nexus ID" > id.txt
$ ndeftool.py pack -n '' id.txt -o id.ndef
$ echo -n "Got it!" > ack.txt
$ ndeftool.py pack -n '' ack.txt -o ack.ndef
$ cat id.ndef ack.ndef > translations.ndef
$ beam.py recv send translations.ndef

This will send back a text record with "Got it!" whenever a text record with "My Nexus ID" is received. To adapt this to your needs look up the add_recv_send_parser() and run_recv_send_action() in beam.by and change as needed.

Can you help with this problem?

Provide an answer of your own, or ask Giovanni Palmer for more information if necessary.

To post a message you must log in.