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
  • Have a happy & safe holiday weekend!
    August 29, 2014 at 3:55 PM
  • Useful #JAXB primer that illustrates the power from tools & frameworks that unobtrusively interact with POJOs - http://t.co/J1s5DpcsCp
    August 29, 2014 at 3:19 PM
  • The Keyhole team fantasy football league begins! Huge thanks to our commissioner @zachagardner. Good luck to all in the virtual gridiron!
    August 28, 2014 at 5:30 PM
  • Shout out to last year's winner of our Keyhole #fantasyfootball league - Adi Rosenblum (@adidas28). Will his reign continue? :-)
    August 28, 2014 at 5:30 PM
  • @dashaun That is the perfect way to put it - we are very excited! Great to meet you officially.
    August 28, 2014 at 4:53 PM
  • Check out a quick intro to Functional Reactive Programing and #JavaScript - http://t.co/4LSt6aPJvG #FRP http://t.co/m6G1Kqbwyi
    August 28, 2014 at 4:06 PM
  • When you pair #JAXB & #JPA, you can expect some "gotchas." Here are techniques to help you overcome the hurdles - http://t.co/J1s5DpcsCp
    August 27, 2014 at 1:56 PM
  • Interesting perspective - Famo.us talks big, but jQuery Foundation isn't worried: http://t.co/o9lLpPoh2G Thoughts?
    August 27, 2014 at 12:41 PM
  • We are delighted to say that RJ (@RJvXP) & Donna (@dkbdevlab) join Keyhole today. Welcome to the team!
    August 27, 2014 at 9:22 AM
  • RT @codeproject: Learning MVC - Part 5 Repository Pattern in MVC3 Application with Entity Framework by Akhil Mittal http://t.co/z603gpAH…
    August 27, 2014 at 9:15 AM
  • Know a bright new grad looking to learn? We're seeking a team member on our business side of the Keyhole house - http://t.co/GDvFVmoMF9
    August 26, 2014 at 3:29 PM
  • When you pair #JAXB & #JPA, you can expect to encounter some "gotchas." Techniques & learning to overcome hurdles - http://t.co/J1s5DpcsCp
    August 26, 2014 at 11:12 AM
  • Don't miss Mark Adelsberger's newest post on the Keyhole blog: #JAXB – A Newcomer’s Perspective, Part 2 http://t.co/J1s5DpcsCp
    August 25, 2014 at 1:21 PM
  • A huge welcome to Mike Schlatter who joins the KHS team today!
    August 25, 2014 at 12:33 PM
  • Never used JAXB? Check out a simple usage pattern that pairs #JAXB’s data binding capabilities with JPA - http://t.co/Ki9G04pLR6
    August 22, 2014 at 8:35 AM
  • Check out a quick intro to Functional Reactive Programing and #JavaScript - http://t.co/YGSsz5eynl #FRP http://t.co/m6G1Kqbwyi
    August 21, 2014 at 11:32 AM
  • Our team is riding #BikeMS to support the fight against Multiple Sclerosis. Get involved - http://t.co/GGObSd073P http://t.co/vZpWRXkQ3z
    August 21, 2014 at 9:09 AM
  • @dtkachev Thanks for the tweet! The server is back up now - sorry for the inconvenience. Have a good day!
    August 21, 2014 at 9:03 AM
  • RT @m_evans10: @joewalnes A SQL query goes into a bar, walks up to two tables and asks, "Can I join you?"
    August 20, 2014 at 3:55 PM
  • Have you read @wdpitt's newest book? - Web Essentials using #JavaScript & #HTML5. Free PDF download via @InfoQ: http://t.co/lesuPJ770I
    August 20, 2014 at 2:31 PM
Keyhole Software
8900 State Line Road, Suite 455
Leawood, KS 66206
ph: 877-521-7769
© 2014 Keyhole Software, LLC. All rights reserved.