2017년 5월 25일 목요일

NFC difficulties

Trying to create a simple, beginner level NFC read/write app just to figure out the basics.
Started with App inventor pre-made BallShuffle app (NFC game) but it doesn't work well. Receiving a message the tag is empty (attached please see App_msg1 phone screenshot). The app is downloaded as the "apk" file (not working in the dev/test environment.

Created simple test app (write/read button) and it does read and write but during the read session it throws a Runtime error (image App_msg2) attached) saying that "API can not be called while activity is paused" ?  
I do not use any API in my super simple read/write test application
It reads/writes to NTAG213 but can't do it with NTAG216

Available documentation on the App Inventor 2 site is not sufficient for me to figure out what I am doing wrong.
Any help in form of an working app, direction to a good paper about the NFC for AppInventor or any other suggestion is more than welcome.
I use Android Samsung S3 with 5Gb of free memory space.
Reinstalled App inventor on it, cleared cash, reset all connections, reload the apps etc.. - no luck

Thank you in advance for your feedback

Is the Ball Shuffle app the same as the Cup Game?

Post a downloaded blocks image for more ideas.

It reads/writes to NTAG213 but can't do it with NTAG216

Factor out AI2 and try a different NFC app -
See this thread ...

Yes, the Ball Shuffle app is the same app as the Cup Game [NFC Cup Game for AI2 ]
I am in process of posting the "aia" of my test app, screens of the blocks involved and also the designer's view of the same.

Started to think that I might need:
A) some time delay between switching from the "read" to "write" 
B) some mechanism to "control" interruptions that could occur while my app is working with the NFC 

P.S. learning this interface, trying to attach the rest...


OK, good idea.
Will test different NFC apps and different tags to see if it happens with any other combination.
Than will post here and turn back to my test app

Is your new tag type text-only?

Not sure about the tag type, Downloading some NFC "tools" to see "deeper" into the tags' structure. I thought it is just a memory space (generally an address space of a defined size) and it would depend only on what one writes into it. From the data structure entered - yes, I am entering only text. Will post back after I "scan" the tags for their structure 

Downloaded an "api" file from this forum from an user who reported similar issues in 2013 (the original post is closed but I attached the app - called NFCtest3). His appwill try to write but will throw the "empty tag" message in that process and also a nasty Run-Time error (android phone screen shot attached in jpg).  Tag specs (of the tag used in this scenario) are below (acquired with an NFC scan tool). The further I go with testing the more errors are coming up.
Working on finding a stable application that can consistently write / read - just to weed out possible bad tags and start with MIT App Inventor NFC feature testing. 

Tag Specs attached this time.
Note: the android phone screen shot is from my phone (today) after I run other forum member's app (downloaded from a 2013 post)

Manufacturer: NXP Semiconductors
IC Type = NTAG213
NFC Forum NDEF-compliant tag = Type 2 Tag
Capacitance = 50pF
Protocol = ISO/IEC 14443-3
Encoding = UTF-8

Maybe some manufacturer NFC reader apps from the Play Store might help
with the finger pointing.

What do they say about your reader and tags?

For that error, did you fix the red triangle error in your blocks before you tried to run it?
See attached.

no, that was the original App (from 2013) I downloaded to compare. Left it as it was just to run it few times. Will fix the error tonight.

In the meantime I tried 4 free apps (downloaded from the Google Play store) and only 1 can read and write consistently to the 3 tags I am using as a test group (two NTAG216 and one NTAG213). So, that would mean I can exclude the smart device I am using (was thinking I need to update the "OS") and the tags as a root cause for my problems (?)

Basically, back to the AI2 and figuring out what is triggering the problems
Knowing that delays (timers/counters) might not work well in the AI2 I still have a feeling that timing of the "code" and syncing it with phone/tag proximity (bring tags in for read/write + move a phone away when done) are playing a role here. 
Difficult to troubleshoot (trying to figure out appropriate test code I could run step by step)..

OK, 3rd day.
Searching for the root cause I created a super simple 2 blocsk app. (images attached). One button to enable "read" and color changing block to signal a tag has been read. Created "apk" file and installed it on my cell phone (Android samsung S3)
In addition, I am using one stable NFC write/read app downloaded off of the Google Store which is not showing any issues
Also I decided not to swing and move the phone around but rather;
- place it over (on) the tag
- keep it there, static for a while
- only _then_ interact with my app UI (click the "read" button) to enable the read thread.  Here is what I observed;

1) placed a phone over the tag
2) waited 1/2 -1 sec (before I would touch the 'read' button)
3) before I had a change to touch the UI (the "read" button), the phone interacted with the tag and popped up the message "Empty Tag" ... "unknown tag type"...(attached: Image 1)
4) repeated the above several times and got the same results

At this point I wanted to understand if this is some issue due to the apps I created or tags or the cell phone ? So I continued:
5) shut off my NFC app (that I created), left the cell phone NFC = "ON" and put it over a tag (repeated for several tags)
6) received the same "Empty Tag" ".. "Unknown tag type" message for the NTAG216 which had its memory wiped with some "Tag Tool"
7) received a different popup message for NTAG213 and NTAG216 that had some txt on them - asking me whihc app I want to use to interact with them (see Image 2  attached)

Now it seemed clear the issue of the "Empty Tag" messasge was not caused by my apps but rather by the cell phone NFC feature - or better to say by some system level process related to the NFC being set "ON" (in the  PC world I would say - "NFC driver issue" to express what I think is messign up with me here). To conclude I did the following:
8) power cycled the phone, set NFC= OFF, placed the phone on a tag -- > no reaction, nothing
9) set the NFC= ON, placed the phone on a tag -- > got the same messages and behavior described under 6 and 7 above....

A) Some "default" app / feature (unknown to me), related to my phone's NFC settings (when NFC=ON) coexists with the NFC apps I created and installed and is interacting with the tag, regardles of my installed app (see 5, 6, 7 above). This could help explain intermittent success /error episodes observed previously during read / write sessions.
B) The tags are not the issue (NTAG213, NTAG 216) but the interaction between that NFC generic/built in "process" and my own apps

Now, ideas are needed to find out what that "hidden" process is and how to neutralize it ?
Given the structure of the message presented on the image 2 -

Attaching app blocks, just because I referenced them in the above message

Two ideas ...

The NFC TagRead event has a message  local variable that ostensibly should contain the
newly arrived message.  You don't show it.  (Too obvious)

Does your phone have a section in its Settings app for control of NFC operations?
That might prove informative.

A search for NFC using this board's search box found this idea ...

The tags only work after they have been initialized by the app mentioned in that post .

Saw those posts earlier but only now I think I can interpret them. The phone seems to be behaving as designed (?).
1. The app mentioned in the link you provided is called "Tags" and that is the one which seems to be jumping out every time the NFC is set to ON and a tag is brought close to the phone, regardless of what my own NFC apps are doing.
2. When I bring a pre-formatted tag to the phone, it asks me which of the NFC apps I want to use (sounds OK)
3. When a tag is unformatted or its memory is wiped out - the "Tags" app (in the phone Application Manager menu "ALL") throws the "unknown tag type" messasge

It seems I need to test the following scenarios to conclude this case';
A) Disable that "bult in" app called "Tags" (your link) and record what will change due to that
B) If that helps reduce popups etc - I need to see if I can shut off that app ("Tags") from within my AI2 app to make sure that my app works fine on other phones
C) Figure out a way to create time delays or loops betwen the NFC "actions" - why ? - because it seems the NFC is firing continuously as long as it is set to "ON" on the phone (perhaps it is that app "Tags" making it do so) and also when (within the AI2 app) the "read mode" is set to "true".

It is not clear to me if the NFC "write" event stops automatically wiht the "TagWritten" event or if it continues firing. Also, will the app stop reading after the TagRead event "happens" or does it continue reading untill we set TagRead to False ?

Would you know of any additional literature about the AI2 NFC ?
What is available on the AI2 web site is pretty minimalistic - at least for me.
Thank you for your help so far !

So I turned off that application (Settings - Application Manager / "tab ALL / Tags app--> see attached images) but it didn;t help.
If the NFC =ON and I bring a tag close to the phone it now either makes a sound (in case of an unformatted tag) or opens a window asling me to select which NFC app I want to use (in case of a configured tag).

I downloaded a 3rd party NFC tag which used to be very stable and has only 2 buttons: Read from and Write to the tag. I start the app and am not touching neither button. If I bring a configured tag to the phone, the app suddently reads it without me asking it to do that.

Trying to find how to stop this unwanted behavior because it affects even 3rd party, stable apps by taking the control over the NFC feature.
Looking into apps that are Running (screnshot attached) and the only one that seems suspicious to me is the SmartCardService. I don;t see any NFC related app/name ?

Also, I am trying to understand what is the "WriteType" property and how to use it ?

C) Figure out a way to create time delays or loops betwen the NFC "actions"
- why ? - because it seems the NFC is firing continuously as long as it is
set to "ON" on the phone (perhaps it is that app "Tags" making it do
so) and also when (within the AI2 app) the "read mode" is set to "true".

AI2 is event driven, unlike Arduinos .

You should never loop, only respond to events firing.
The only exception is when you are looping thru a data structure.
Otherwise you will freeze the app.

The documentation is indeed sparse...


Non-visible component to provide NFC capabilities. For now this component supports the reading and writing of text tags only (if supported by the device)
In order to read and write text tags, the component must have its ReadMode property set to True or False respectively.
Note: This component will only work on Screen1 of any App Inventor app.




TagRead(text message)
Indicates that a new tag has been detected. Currently this is only a plain text tag, as specified in the manifest. See Compiler.java.
Event for TagWritten



Google is your friend.

Some surprising results, at least surprising for me
Set my phone NFC to ON.
Created super simple app to read a tag (images attached).
As you can see- I did not even address the "NearField Read Mode" parameter (not in the app)

Even though the literature says the Read Mode should be set to True/False in order to read/write respectively, my app reads tags just fine. NTAG213 and NTAG216 alike. So, it seems the NFC circuit is firing all the time (set by the phone NFC=ON) and the When Near Field TagRead event works/reads regardless of the Read Mode parameter not being set to True..

Next test  = create a simple write app, without any reference to the "NearField Read Mode" parameter to see if I will be able to write to a tag in that environment. It will be strange if that shows to be possible 

Forgot to ask - can we activate / disactivate phone's NFC setting from within AI2 app?
Reading about the Activity Starter but can't tell if it can interact with the OS / Phone Settings ?


Forgot to ask - can we activate / disactivate phone's NFC setting from within AI2 app ?
Reading about the Activity Starter but can't tell if it can interact with the OS / Phone Settings ?

Here's a pretty full catalog
Maybe it will answer your question.


This app is for all developers who are developing a app using App Inventor and want to learn more about the activity starter component. App Inventor ActivityStarter lets you play with intent activities and has more than 150 example demo activities.
This app is actually a kind of Activity Starter component. You can make your own activity with all the fields you have in the Activity Starter component in App Inventor and execute the activity. If you want to learn more about the activity starter component in app inventor or are you looking for example activities, then this app is for you!

In this app you will find example intent activities such as:
-Action and classes to all android settings.
-open, edit and view content such as image, audio, video or a file.
-Search for content on web or phone.
-Call, Dial, Email, SMS, MMS. Pick, Call or edit contact
-Geo-location activities for Google Maps, Street view or other route app.
-Photo and video activities
-Google play and install and uninstall apps activities
-Save text to sdcart, copy text to clipboard. Share text with Whatsapp, Hangouts, Drive, Dropbox, Mega.
-External App inventor tools activity demos for AsUtil, Phone Picker for App Inventor, vkTools

* More about App Inventor maintained by the Massachusetts Institute of Technology (MIT).

* Note that this app has a lot of permissions because it has to be able to do any activity that you can make in app inventor.

* You can also download the source code of this app, see http://code.google.com/p/appinvactivitystarter


FAQ for lists and TinyDB

How to set notifier to appear just once

I'm trying to build an app that allows the user to enter many values. In certain values, I want leave him/her a message through the notifier however, it is supposed to appear just once (moment after the user enter the info and right now it is appearing every time the screen initializes). Is there any way to do this? 

What you might do depends on what you told you app to do.  Since we do not know what conversation you had with your blocks you have to figure out when you want it to behave one way and when you want it to do something differently.  You might have seen this advice about how to let your app make a decision, 
Chapter 18. Programming Your App to Make Decisions
Since it is happening 'every time the screen initializes) you might have to use a Boolean value or something else to transfer how you want the notifier to show or not show (Notifier.Visible can be set to true or false). if for some reason you decided you could not use 'virtual' screens instead of switching between screens.  Switching between screen frequently a lot of necessary complex coding to make it work.possibly part of your issue.

Why not do a little reading about how you can tell your app what to do for itself and then try some blocks.   If you still cannot make this work, perhaps it would be a good idea to share you relevant blocks with the forum?

I think the notifier component can't be set to invisible... I used a clock to try stopping the notifier after the message is once showed, but it didn't work.
I send you here a print of my code. The block is inserted in the control lock 'When screen initialize".

You are right, Notifier cannot be set to visible=true or false...my mistake.

When you reopen your Screenxxxx the initialize block fires, so the app polls the last know glucose value you stored in the TinyDB.
You actually tell your app to show the Notifier every time you open this particular screen.

Setting the GlucoseLevel1  tag's value to 0 at when you close this screen might fix the issue when you reopen it, .  If GlucoseLevel1 is >= 110  the Notififer2 will fire.A value of 110 ensures you will see the 
notifier every time you open the screen unless the value is less than 110.

But my intention is to store the values so I can't change them to 0, I'm not really sure if I understand what you are saying

OK then you are going to have to move the block calling Notifier2  or rewrite your code because the REASON the notifier keeps showing is because at present the if statement tells it to display the notifier.  You have to change your if statement.  To what I do not know because I do not know what else you are doing.

What I say is that if the TinyDB has a value of 110 or more, your block says that you want to show the notifier.   I suggest 0 because that won't trigger the If statement condition.

You could define a global variable notifiershown (or similar) initialized to false. Then,place the notifier in an if block: if not notifiershown, then show the notifier and set the global variable notifiershown to true.
You must reset the global when for some reason you want the notifier to be shown again.

And how exactly I can associate the global variable with the notifier? 


Like this. After the notifier is shown once, the global variable notifierShown is true and you will skip the then part of the condition.
If you later decide that the notifier should be shown again (once), then set the global to false.

It's still not working :( The notifier shows every time I enter the screen

The Notifier shows up because your code tells it to open  every time you open this particular screen.  If you followed Ghica's advice, you might be part way to a 
solution.  I suspect the issue is more than the Notifier there are other hints in your code that you are possibly misusing the TinyDB too.

 You did not post an image of how you modified your blocks. So, who is to know?

My code right now is the same but with Ghica's hint. The only difference is that Ghica used a label and I'm using a TinyDB block in the if statement but I don't think that's the problem. Possibly is related to the fact that I put this code in the block "When screen incialize" but I'm not seeing another way. 
The idea is make the notifier appear when the value is 110 or more, but just one time because after that the user will eventually put more values and they may not correspond with that warning that is showing up.

Maybe you should pay more attention to what Steve says and maybe show more of your blocks (because we do not see the relevant ones) and/or post an .aia

You should go through Screen1.Initialize only once.
If that does not happen, this is because you are closing another screen and then opening Screen1 again.
Look here to find out how to do this right: http://puravidaapps.com/manager.php


I didn't show the rest because the code is really huge and basically the same over and over again and I was thinking that code was enough. Anyway, here I send the app in aia. The screen that I was talking about is called "Register_Glucose" and the values that appear in this labels come from the screen "AddNewBGResult".

I really appreciate your help, thanks for your tips. If you find out what's going on let me know. I'll read what you guys recommend!

I opened your project, but there is no way I am going to find out what is wrong.
Please take some time to learn more about app inventor, especially about procedures in this case.
If you can avoid repetition by using procedures, your app will become a lot clearer. As a rule, a block that is longer than my screen, smells like something could be simplified.

Ok thanks anyway!

I looked at your blocks.

You set it up so that you need to add components and blocks for each new
piece of data.  Your current hard wired limit for readings is 10 readings,
so at 20 readings you would have doubled the size of your app.

If this is just for personal use, consider switching to Fusion Tables.

Do the tutorials for lists and TinyDB in this FAQ for examples
of how to deal with an unlimited amount of data:

I should have added that I admire all the energy you have put into this. I just wish that you would have asked for help much earlier.
Maybe you can appreciate Abrahams advice much better now though.

This can be made to work without the code Ghica provided. Your original code could work properly.   When you use the Register_Glucose screen  Initialize, you check all the Log number  items .  For example, if any 
of the 10 log number arrangements show a value that triggered  a display of the Notifier2   , perhaps  saved as a @ 2 log slot then you have a problem when you log an OK value in log number1.   Why?  because when the info is updated,  the notifier for log number2 will display when you log the new OK BG value into log number 1.   The more log numbers that are 'filled' and the problem gets worse.

If you only used a single screen, this would be an easy fix. You only really want to update the log number you are working on.  This is available on the data entry screen from the ListPIcker.   Because you use multiple screens it is difficult to tell the Register_Glucose screen you are only updating the log number 1.

This is why you have issues with the Notifier popping up.   You can either eliminate the Notifier2 ... since you color code the BG value  or you can figure out how to only update screen Register_Glucose with the Log number you select on the data info screen.

I already solve the problem, thank you all!