PhoneGap

PhoneGap: Utilizing Native Mobile Features with Plugins

by on November 4, 2013 8:22 am

Recently, I was tasked with developing a mobile application in PhoneGap that utilized the Bluetooth feature. Although PhoneGap provides basic functionality, PhoneGap lacks the ability to implement mobile features like Bluetooth. However, it provides the mechanism to implement a user-defined plugin and the means to access the native functionality in the PhoneGap application. In this blog I will outline the steps for implementing a plugin for PhoneGap to utilize native mobile features.

In my previous blog “Android, iOS, and Windows Mobile…Oh My: An Introduction to PhoneGap,” I summarized the basic functionality. I would recommend reviewing the post if you have not yet done so.

Implementing a Plugin

To implement a plugin, you need to implement a JavaScript interface, which allows the functions to be accessed through HTML pages in a PhoneGap application. The plugin interface must be implemented in the native language.

The cordova.exec function is used in JavaScript interface to access the native features through a user-defined native plugin. I have outlined the format and description for the function and parameters for reference:

cordova.exec(success, failure, "NativePluginName", "start", [Arg]);

The arguments:

  • “success” – The function that will be called on the successful completion of the native call.
  • “failure” – The function call that will be called on error of the native plugin and pass the message based on the plugin implementation.
  • “NativePluginName” – The service class to be called on native side. Typically it will be the class name of a native plugin.
  • “start” – The action to be called on native plugin. Normally it is the method name. However, a different action name can be used based on the action name used in a native plugin (a description is provided below).
  • “[Arg]” – The last parameter enables passing the array of arguments for the method in the native plugin implementation.

Make sure to include the JavaScript interface in a HTML webpage to access the JavaScript interface functions.

<script type="text/JavaScript" src="js/JavaScriptplugininterface.js"></script>

In the statement above, JavaScriptplugininterface is the name of the JavaScript interface name. Usually it is similar to the native plugin.

User-defined Native Plugin

For the user-defined native plugin, I will talk about the Android plugin which utilizes Java.

To implement the native plugin interface, your class needs to extend to “CordovaPlugin.” Android uses intents to communicate between processes. The Cordova plugin provides the access to the Cordova object, through which you can access current activity and context. Using the context you can create new intent and launch it or perform the functions on the context from the plugin. To access the current activity and context of the application in the plugin, you can use:

this.cordova.getActivity() and this.cordova.getActivity().getBaseContext() respectively.

For Cordova to find the plugin, it must be declared in res/xml/config.xml.

<feature name="NativePluginName">
        <param name="android-package" value="com.keyhole.cordova.NativePluginName" />
</feature>

The name of the feature is the service name that is used in JavaScript to call the service. The param value is the full path (including namespace) of the service class. Do not change the param name =”android-package”. If you fail to include this, application will compile but Cordova will not be able to find the plugin.

Finally, the cordova.exec function of JavaScript will be passed to the plugin’s execute method along with the arguments. The argument action is passed from JavaScript and they can be matched as follow to implement the function. Also, the arguments passed in JavaScript are available in Cordova args.

@Override
public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {

		boolean validAction = true;
		Log.d("Plugin executing action:",action);
		  if (action.equals("start")) {
			  theCallbackContext = callbackContext;
// Do work
	        } else {
	        	validAction = false;
	        }
		return validAction;
	}

Conclusion

A major benefit of using PhoneGap is that you can develop an application using HTML, jQuery, JavaScript without needing to get familiar with a variety of mobile native languages. But to implement a feature not yet available in PhoneGap like Bluetooth, a plugin needs to be implemented to provide access to the native functionality. Implementation of the plugin requires an interface to built in the specific mobile native language. Unfortunately, having to code in a specific mobile native language defeats one of the major the advantages of PhoneGap.

As demonstrated above, the interface code is a very small portion to be implemented in the native language. Thus, using PhoneGap lowers the amount of code needed in the native language. Because of this, I still believe PhoneGap is a good option to implement mobile applications for multiple platforms.

More information about PhoneGap can be located at phonegap.com.

— Jinal Patel, asktheteam@keyholesoftware.com

  • Share:

2 Responses to “PhoneGap: Utilizing Native Mobile Features with Plugins”

  1. Pranav Joshi says:

    Good technical share might be helpful for awareness thanks for sharing

    Keep it up

    All the best

  2. […] Update: Check out a related PhoneGap post by Jinal Patel written November 4, 2013 – PhoneGap: Utilizing Native Mobile Features with Plugins […]

Leave a Reply

Things Twitter is Talking About
  • In Part 2 of our series on creating your own #Java annotations, learn about processing them with the Reflection API - http://t.co/E1lr3RmjI7
    September 17, 2014 at 3:18 PM
  • We send out our free monthly tech newsletter tomorrow - dev tips/articles via email. Not on the list? Sign up: http://t.co/h8kpjn419s
    September 16, 2014 at 2:58 PM
  • Want to chuckle? If programming languages were vehicles -http://t.co/quqHsUFCtR #funny
    September 16, 2014 at 11:41 AM
  • In Part 2 of our series on creating your own annotations, learn about processing #Java annotations using Reflection: http://t.co/DJZvQuarkc
    September 16, 2014 at 9:06 AM
  • Don't miss @jhackett01's newest post on the Keyhole blog - Processing #Java Annotations Using Reflection: http://t.co/E1lr3RmjI7
    September 15, 2014 at 12:02 PM
  • We're pretty excited - Keyhole's #BikeMS team raised 158% of their fundraising goal to benefit @MidAmericaMS. Plus, they had a great ride!
    September 15, 2014 at 10:38 AM
  • A huge welcome to David Kelly (@rheomatic) who officially joins the Keyhole team today! :-)
    September 15, 2014 at 10:00 AM
  • Sending warm thoughts to @eastlack, @cdesalvo, @wdpitt & all participating in #BikeMS this AM. Thanks for helping in the fight against MS!
    September 13, 2014 at 8:10 AM
  • .@rheomatic We are so excited to have you joining the team! Welcome :-)
    September 12, 2014 at 4:11 PM
  • As the official holiday is a Saturday, we're celebrating today! Happy (early) #ProgrammersDay to you! http://t.co/1CvUfrzytE
    September 12, 2014 at 1:55 PM
  • Tomorrow @cdesalvo, @eastlack, & @wdpitt are riding #BikeMS to benefit @MidAmericaMS. You can get involved, too - http://t.co/9boQwEUxth
    September 12, 2014 at 11:00 AM
  • RT @AgileDevs: 5 tips for great code reviews http://t.co/9PdbtEv0z8
    September 11, 2014 at 2:53 PM
  • The BEMs of Structuring #CSS - http://t.co/159suYtfx6 A quick introduction to the Block Element Modifier methodology.
    September 10, 2014 at 2:49 PM
  • A huge welcome to Joseph Post (@jsphpst) who has joined the Keyhole team this week!
    September 10, 2014 at 9:52 AM
  • @TheGrisExplores Absolutely, and thanks for the compliment! Here's an article that you might find helpful, too - http://t.co/7oxpaohCS1
    September 9, 2014 at 2:22 PM
  • Express.js seems to be the top pick! MT @TheGrisExplores: "what's your fave server-side MVC framework for NodeJS when SPA is not an option?"
    September 9, 2014 at 1:56 PM
  • RT @TheGrisExplores: Yo @KeyholeSoftware dudes and dudettes: what is your favorite server-side MVC framework for NodeJS when SPA is not an …
    September 9, 2014 at 1:15 PM
  • There is a new post on the Keyhole blog by @brianletteri - The BEMs of Structuring CSS: http://t.co/159suYtfx6
    September 9, 2014 at 12:12 PM
  • Know a bright grad? We're looking for a business support / recruiting representative to join our team in Kansas City: http://t.co/GDvFVmoMF9
    September 8, 2014 at 11:31 AM
  • A huge welcome to @jeremygard who joins the Keyhole team today!
    September 8, 2014 at 8:40 AM
Keyhole Software
8900 State Line Road, Suite 455
Leawood, KS 66206
ph: 877-521-7769
© 2014 Keyhole Software, LLC. All rights reserved.