Home > other >  How to create a QR code that execute command?
How to create a QR code that execute command?

Time:12-10

I have seen some example that people code executable code on a QR code like this https://itsmattkc.com/etc/snakeqr/. I wanted to create a QR code that executes the command to open a specific phone application for any phone (android & IOS).
The process will be like this,

  1. Scan QR code with a phone.
  2. Phone executes the command that stores inside the QR code (code that does something like "start C:\...\specific app" in C language) and open the App or prompt user to download it if the application is not installed on the phone.
  3. Close the window that is used to execute the command(if any).

My question right now is,

  1. What language can be used to code the command that open the phone application like what I stated above?
  2. How can I turn the code into a QR code?

CodePudding user response:

Background A QR code doesn't itself execute anything, at a high level QR code is just a method of serializing an array of bytes into a visual form, the reader can de-serialize the image back into an array of bytes which must then be interpreted.

Generic QR readers will generally assume that the original data represented an encoded string and so will decode the bytes into a string.

The instructions in SnakeQR tell you to use a QR reader that allow you to save the bytes to disk, instead of decoding them into a string, which just demonstrates that the QR code contains binary data and that the data on its own doesn't do anything, the user needs to take that data and complete the next step to action it, but that action is up to the user.

This is when the magic starts to happen, if the QR code represents or contains a URL, then most reader applications will auto-launch a web browser and will navigate to that URL.

In the app development world we can hijack the default behaviour of these QR code readers to interact with the device in 3 ways:

  1. Write a web page that will execute scripts to complete your operation on the device that navigates to that page, host that somewhere globally accessible and store the URL to that page in your QR code.

  2. Use a URI that the device will recognise as an internal location, this post How to launch my android app from a QR reader is describing this process, in the android application API you can define these intents, effectively registering a URI with a specific action.

    • The specifics to this are out of scope, this question is about the QR code itself, but most operating systems will have a similar process where there are standard actions bound to URIs and there is usually a mechanism for you to create custom bindings.
  3. Write your own proprietary QR Reader application that can natively understand the commands in the QR code and can execute them direct.

In terms of languages that support this, its not a language specific thing, in Android applications the mechanism you want to exploit is Android Intents Tutorial (Android Developers - Intents).

In iOS there have been mixed version of URL bindings over the years, but SiriKit is probably the closest in terms of functionality, its far more restricted than android, but enough to launch apps and pass through arguments.

In Windows OS you can register URI schemes to do the same thing...

When it comes to proprietary logic, we can use all 3 methods to build a seemless experience for the users:

  1. Make a landing web page with a message like "If you see this page then you should install my app for an enhanced experience..." (on this page there should be instructions for how to find and install your app)
  2. Make the QR code a URL to the landing page with the parameters that you want passed to the app as URL parameters.
  3. Build and publish your app, in this app you should register the URI to the landing page with the native device OS so that after the device is installed, your app runs instead of the user going to the landing page. Don't forget to map any parameters as startup arguments for your app.
  • Related