Create NFC Apps Using RhoMobile

April 04, 2012

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. :P
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

$ rhodes app sample_app
$ cd sample_app
$ rhodes model nfc

2. Enable NFC in build.yml

android:
  version: 2.3.3
  extensions:
  - nfc

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.

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

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,

Rho::NFCManager.make_payload_with_well_known_text("en", "HELLO WORLD")

puts payload message into hash of records.Create hash of record Using Ndef (NFC data Exchange Format) as follows,

hash = {
      'id'      = [0],
      'type'    = Rho::NdefRecord:: RTD_TEXT,
      'tnf'     = Rho::NdefRecord::TNF_WELL_KNOWN,
      'payload' = payload
      }

Then use Rho::NFCManager.make_NdefRecord_from_hash(hash) method to create message record.Convert record array into message using following method

record= Rho::NFCManager.make_NdefMessage_from_array_of_NdefRecord(records)

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.

3 Comments. Leave new

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

can you provide me the entire code pls….?

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 *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

%d bloggers like this: