Create NFC Apps Using RhoMobile

Visnupriya

1 min read

We just finished creating an NFC capable app using Rhomobile and here is our blog about it. By the way this is second blog do encourage me to write more. 😛
NFCNear Field Communication used for short range wireless communication between two Mobiles with secured communication up to distances of 4 to 5 cm. As per my knowledge NFC is supported in Android 2.3.3 and higher but mostly manufacturer specific.
NFC is commonly used in

Mobile phones and PDAs
Personal computers
Check-out cash registers or “point-of-sale” equipment
Turnstiles
Vending machines
Parking meters
ATMs
Applications around the office and house, e.g. garage doors

If you got some ideas (good crazy ones) on other usages, then please share it a comment so that we can develop it using RhoMobile under open source license.
RhoMobile does support NFC capability, which includes:

  • Reading NDEF data from an NFC tag
  • Beaming NDEF messages from one device to another

You can also check the RhoMobile NFC documentation at http://docs.rhomobile.com/rhodes/nfc
Let’s see how to develop NFC functionality based Rhodes application using Rhomobile.
1. Create sample rhodes app
[source language=”ruby”]
$ rhodes app sample_app
$ cd sample_app
$ rhodes model nfc
[/source]
2. Enable NFC in build.yml
[source language=”ruby”]
android:
version: 2.3.3
extensions:
– nfc
[/source]
3. In app/application.rb file, activate NFC on Android device.
Rho::NFCManager.enable is used to enable the NFC capability on device.Rho::NFCManager.set_nfc_callback(call back url) is called when NFC device receives NFC event(peer to peer).Rho::NFCManager.set_nfc_tech_callback(call back url) is called for reading and writing NFC tag.Rho:NFCManager.perform_open_application_event method is enable the application to receive NFC when application is running in the background.
[source language=”ruby”]
def on_nfc_app
Rho::NFCManager.enable
Rho::NFCManager.set_nfc_callback(“set_nfc_callback url”)
Rho::NFCManager.set_nfc_tech_callback(“nfc_tech_callback url”)
Rho::NFCManager.perform_open_application_event
end
[/source]
4. In app/Nfc/nfc_controller.rb
Let’s see how to start peer to peer data exchange using Rho::NFCManager API.You can push NFC tag to another nfc enabled mobile.First you need payload message as follows,
[source language=”ruby”]
Rho::NFCManager.make_payload_with_well_known_text(“en”, “HELLO WORLD”)
[/source]
puts payload message into hash of records.Create hash of record Using Ndef (NFC data Exchange Format) as follows,
[source language=”ruby”]
hash = {
‘id’ = [0],
‘type’ = Rho::NdefRecord:: RTD_TEXT,
‘tnf’ = Rho::NdefRecord::TNF_WELL_KNOWN,
‘payload’ = payload
}
[/source]
Then use Rho::NFCManager.make_NdefRecord_from_hash(hash) method to create message record.Convert record array into message using following method
[source language=”ruby”]
record= Rho::NFCManager.make_NdefMessage_from_array_of_NdefRecord(records)
[/source]
Now we can push message using Rho::NFCManager.p2p_enable_foreground_nde_push(record) and when you want to stop message use Rho::NFCManager.p2p_disable_foreground_nde_push.
Sample screenshots taken from Android Emulator:
      
We hope you liked this blog.
Comments are welcome.

Related posts:

3 Replies to “Create NFC Apps Using RhoMobile”

  1. Hi ,
    Nice blog . could you please help me out regrading NFC , i m stuck in error ” No file found -NFCapp” while running example file which is provided in rhomobile website.
    Regards,
    Amyth

  2. Hi, thanks for this blog, can you please do one about syncing and full CRUD using asynchttp instead of Rhosync or rhoconnect?

Leave a Reply

Your email address will not be published. Required fields are marked *