Software Development

PhoneGap: Utilizing Native Mobile Features with Plugins

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.
 

Keyhole Software

Keyhole is a midwest-based consulting firm with a tight-knit technical team. We work primarily with Java, JavaScript and .NET technologies, specializing in application development. We love the challenge that comes in consulting and blog often regarding some of the technical situations and technologies we face.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
PhoneGap Development
9 years ago

I am PhoneGap developer, please let me know how PhoneGap plug-in communicate with native controls, i mean i use JavaScript but how JavaScript is able to use native camera, barcode scanner & all.

Shai Almog
8 years ago

That’s a nice introduction. A while ago we introduced PhoneGap/Cordova support in Codename One, one of the main benefits there is the ability to write all the native code (e.g. plugins) in Java and have it translated to all platforms. Check this out here: http://www.javacodegeeks.com/2015/11/phonegapcordova-compatibility-for-codename-one.html

Back to top button