Monday, March 10, 2025

Matching Android Intents


Believe it or not, I feel I have learnt a few things over the course of doing this. When I first read this article on using Android NFC technology and intents a few weeks back, it made no sense at all. Now, it basically all makes sense and I am left asking questions such as "I wonder which of those intents will trigger in each of these various cases" and "I wonder what data I will receive along with the intent notification" and "I wonder how to configure my reader to make that work".

So what I'm going to do now is to actually start writing my own code. Shocking, I know. This is going to be an Android app that listens to all three intents: ACTION_NDEF_DISCOVERED, ACTION_TECH_DISCOVERED and ACTION_TAG_DISCOVERED.

I'm obviously going to use the AndroidBasicNfcReader app as a reference to build from and steal from unashamedly as necessary, but obviously I'm not going to be doing anything like the same thing, since I will not be using the Reader mode but will be listening for intents.

The Project Outline

In Android Studio, I selected "New Project" and then chose "Empty Activity" and filled in all the parameters to create a new activity. I've saved this in my IgnoranceBlog under android_nfc. For posterity, I've tagged this as ANDROID_NFC_GENERATED but I'm not going to go through all the files here.

I'm now going to add all the NFC intents as suggested by the article above, and then copy the activity view and some of the activity code from the AndroidBasicNfcReader code. In doing this, I've found myself fighting against Android Studio, since it wants to do everything "the new way" i.e. in Kotlin and graphically, rather than in Java with text files. Maybe if I weren't so lost, I might give in. Anyway, if you are struggling as well, there are buttons to select "Code" "Split View" or "Design View" in the bar across the top of the editor window.

Then I can add onNewIntent to MainActivity and I should have something I can test.

Once again, I feel I am fighting Android Studio. I feel it should just build the application and possibly even put it straight onto my phone for me, but I can't figure out how. Although it's probably easier to show what I've done from the command line anyway:
JAVA_HOME=/usr/lib/jvm/java-1.17.0-openjdk-amd64 ANDROID_HOME=HOME/Android/Sdk ./gradlew build
Starting a Gradle Daemon, 2 incompatible and 2 stopped Daemons could not be reused, use --status for details


> Task :app:lintReportDebug
Wrote HTML report to file:///home/gareth/Projects/IgnoranceBlog/android_nfc/app/build/reports/lint-results-debug.html


BUILD SUCCESSFUL in 1m 27s
90 actionable tasks: 90 executed
$ ls app/build/outputs/apk/debug/app-debug.apk
app/build/outputs/apk/debug/app-debug.apk
$ adb install app/build/outputs/apk/debug/app-debug.apk
Performing Streamed Install
Success
And yet, nothing works. I try tapping tags against it, cards, the reader. Everything is exactly the same: no output whatsoever. I don't know what I'm doing wrong, but it must be pretty serious.

Conclusion

I give up. Everything I have tried on the phone seems to end in almost complete disaster. I'm not even sure what else to try, but maybe inspiration will strike.

No comments:

Post a Comment