Read/Write NFC Tag With Kivy on Android

2,279 views
Skip to first unread message

hci...@gmail.com

unread,
Oct 3, 2013, 7:37:00 PM10/3/13
to kivy-...@googlegroups.com
I'm designing an app for Android that will need to read and write NFC tags using pyjnius. I have found a tutorial explaining how to do this in java, but I'm wanting to implement this code into pyjnius for use with Kivy and I'm not sure where to start. The link to the tutorial is here: http://www.creativebloq.com/android/getting-started-nfc-android-5122811. How would I implement this? I'm especially confused about how I can pass 'this' or python's equivalent 'self' as an argument to java functions, considering that my app is not a java class inheriting the android 'Activity' class. NFC with Kivy would be awesome! If I'm given a good direction to go with this I would be happy to share this code as a utility package for others to use. I'm new to pyjnius and just need a little help to get going.

Thanks! 

hci...@gmail.com

unread,
Oct 7, 2013, 11:07:30 AM10/7/13
to kivy-...@googlegroups.com
Has anyone done this?

hci...@gmail.com

unread,
Oct 7, 2013, 5:44:32 PM10/7/13
to kivy-...@googlegroups.com
I'm considering writing a class completely in Java to do this and using pyjnius to interact with it. If I write a Java class how do I include that class in my project so that I can access it with pyjnius using autoclass?

hci...@gmail.com

unread,
Oct 8, 2013, 5:17:18 PM10/8/13
to kivy-...@googlegroups.com
I'm still searching for a solution to this. NFC is really cool so I'm sure someone has tried it with kivy;)

Akshay Arora

unread,
Oct 17, 2013, 8:56:33 AM10/17/13
to kivy-...@googlegroups.com


On Wed, Oct 9, 2013 at 2:47 AM, <hci...@gmail.com> wrote:
I'm still searching for a solution to this. NFC is really cool so I'm sure someone has tried it with kivy;)

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Message has been deleted

hci...@gmail.com

unread,
Oct 30, 2013, 2:50:30 PM10/30/13
to kivy-...@googlegroups.com
Thank you! This is the final part of our n-tier project. Now we can get going!!

hci...@gmail.com

unread,
Dec 3, 2013, 5:22:26 PM12/3/13
to kivy-...@googlegroups.com
Well I tried the NFC example at that link and I get this error:

"main.py", line 22, in <module>
I/python  (16769):  ImportError: cannot import name activity


Does this mean that 'activity' is not present? I specified 'android' when I ran 'distribute.sh' so 'android' is included. I even tried replacing 'from android import activity' with 'from android import * ' and it  imports fine but then later down the line of course there's issues referencing 'activity'. Any ideas?

qua-non non

unread,
Dec 4, 2013, 5:50:55 AM12/4/13
to kivy-...@googlegroups.com
Take a look it this gist tito did for nfc as a guide https://gist.github.com/tito/9e2308a4c942ddb2342b


--

hci...@gmail.com

unread,
Dec 18, 2013, 3:16:12 PM12/18/13
to kivy-...@googlegroups.com
Yeah that just gives me this error when i compile and run it:

I/python  ( 2669): Traceback (most recent call last):
I/python  ( 2669):   File "/home/kivy/nfctest/main.py", line 4, in <module>
I/python  ( 2669): ImportError: No module named runnable

What this tells me is that the "android" package is not properly installed. I re-ran the distribute.sh on my vbox instance of kivy and included "android" as a parameter and recompiled, but I still get this error. There's a lot of documentation on pyjnius but I can't find any on how to install the "android" package. Does anyone know how to get all the necessary packages included so this example will run? 



On Thursday, October 3, 2013 4:37:00 PM UTC-7, hci...@gmail.com wrote:

Ben Rousch

unread,
Dec 18, 2013, 3:47:16 PM12/18/13
to kivy-...@googlegroups.com
The app in the gist compiles and runs for me. I can't verify the NFC behavior, but it does not crash when I push the Enable/Disable buttons on my Galaxy S3. https://dl.dropboxusercontent.com/u/101667/NFCP2PChat-1.0-debug.apk

You are trying to run this on an Android device, correct?


--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
 Ben Rousch
   bro...@gmail.com
   http://clusterbleep.net/

hci...@gmail.com

unread,
Dec 18, 2013, 4:48:14 PM12/18/13
to kivy-...@googlegroups.com
Lol! Of course. I noticed that the android recipe that comes with the vbox image is completely different from the current one on the repository though. android.runnable and android.activity are new modules. I downloaded the newest devel version and am giving that a go now.

hci...@gmail.com

unread,
Dec 18, 2013, 5:15:47 PM12/18/13
to kivy-...@googlegroups.com
It builds now with the current python-for-android. Doesn't seem to do anything with NFC though. If I put a tag to the device, the device picks up the tag and closes the program. If you put two devices together the android system asks you if you want to beam, then when you press the screen the device on the other side doesn't know what to do with the message. This is probably because "hello world!" doesn't mean anything to the android.

  The thing is though I don't want to beam from one device to another, I want to read/write on an nfc tag in plain text. how would I get that working in kivy?

hci...@gmail.com

unread,
Dec 18, 2013, 5:50:10 PM12/18/13
to kivy-...@googlegroups.com
I kinda got it to work. NFC permissions have to be turned on (go figure). The only thing is that when the record gets made I get this error:

I/python  ( 8650):  Exception ValueError: ValueError('Invalid shape in axis 0: 0.',) in 'jnius.jnius.ByteArray.set_buffer' ignored

I assume this has to do with the record that is created "Hello World" text not formatting correctly as a byte array. The other side picks up but only displays the format the message was supposed to be in but not the message itself. Probably because the message couldn't even be generated.  Is this an issue I will run into using this module? Or is there a simple way around this?

hci...@gmail.com

unread,
Dec 18, 2013, 6:19:54 PM12/18/13
to kivy-...@googlegroups.com
Correction on that. The middle parameter in this line was throwing a java exception:
ndef_record = NdefRecord(NdefRecord.TNF_MIME_MEDIA, 'text/plain', '', 'hello world')

Strangely though if both the last two are set to blank it works. I switched it around to look like this and it sends 'hello world' to the other device:
ndef_record = NdefRecord(NdefRecord.TNF_MIME_MEDIA, 'hello world', '', '')
I guess that's a step in the right direction.

hci...@gmail.com

unread,
Dec 19, 2013, 5:14:44 PM12/19/13
to kivy-...@googlegroups.com
   I've been messing around with this for hours and all I can get it to do is push to another device or to launch when a 'text/plain' tag is touched to the device but I can't get it to read off of the tag. The 'on_new_intent()' method is supposed to print ndef messages to the console but there is only a line in the program that binds it to 'activity' when 'on resume' is fired but the 'on_new_intent' never gets executed even though the system is sending an intent from the tag. It's almost like the listener in activity.py is never executed. How do I get it to process an incoming intent from a tag? All I want is to do is read plain text off an nfc tag just like you would read it out of a file. I've read the google docs on nfc and have not found it or any examples I've seen to be helpful. Anyone know of a simple example on how to do this? I could even translate a Java example.

knuckles625

unread,
Sep 24, 2015, 4:30:54 PM9/24/15
to Kivy users support
Sorry for bringing this post back from the dead, but I'm looking to do the same thing as the original author-read plain text data from an NFC tag into Kivy.

Were you ever successful?
Would you mind sharing the furthest you were able to get, as a starting place for future folks?
Does anyone else have additional relevant info on this?
This seems to be one of the only places I could find through the mighty google search which discussed getting NFC tag info into python for android. 

Many thanks!

Pkore

unread,
Sep 24, 2015, 8:44:16 PM9/24/15
to Kivy users support
I actually never got it working in kivy. I hate to say it but I had to develop a secondary app in.... cringe.... Java... Ugh... to do the reading part. No worries if you're having trouble though, when I was trying this before I was a kivy noob and couldn't get anything to work in PyJnius. That project has a lot of access to device hardware. Nfc reading is hard in the Android SDK anyway so using a binding to it from another language compounds the difficulty. However, if you have a good grip on NFC with Java and are familiar with pyjnius it should be pretty straight forward for you. 

qua non

unread,
Sep 25, 2015, 3:57:10 PM9/25/15
to kivy-...@googlegroups.com
Communication through NFC using kivy is actually straight forward, there is even a example in the docs for pyjnius. The one I linked to earlier, in this discussion, however that was for nfc comm b/w devices.

For communicating with tags you just need to adjust and call the appropriate api instead.
For what it's worth if you follow the android documentation using pyjnius just works(Yes, I have tried it).

If you are having trouble then post what you tried show your code and we can point out what to fix.

On Fri, Sep 25, 2015 at 6:14 AM, Pkore <koresk...@gmail.com> wrote:
I actually never got it working in kivy. I hate to say it but I had to develop a secondary app in.... cringe.... Java... Ugh... to do the reading part. No worries if you're having trouble though, when I was trying this before I was a kivy noob and couldn't get anything to work in PyJnius. That project has a lot of access to device hardware. Nfc reading is hard in the Android SDK anyway so using a binding to it from another language compounds the difficulty. However, if you have a good grip on NFC with Java and are familiar with pyjnius it should be pretty straight forward for you. 

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

knuckles625

unread,
Oct 14, 2015, 12:08:25 PM10/14/15
to Kivy users support
Thanks for both of the replies. I did take a look at the previous example and spent a good bit of time going through android documentation. I'm ashamed to say I was able to get a relatively simple demo of what I wanted working in MIT's app inventor because there was already an NFC tag example ready made.

Ultimately kivy is the real answer, but I don't have the personal availability to go from android docs to pyjinius to kivy. I'm likely going to put up a bountysource bounty for some well commented example functionality code. Will post back in this series when I put it up. Thanks all

Reply all
Reply to author
Forward
0 new messages