Click here to Skip to main content
15,893,381 members
Articles / Programming Languages / C#
Technical Blog

PhoneGap: Utilizing Native Mobile Features with Plugins

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
5 Nov 2013CPOL4 min read 8.6K   3  
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:

JavaScript
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 an HTML webpage to access the JavaScript interface functions.

JavaScript
<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.

HTML
<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, the 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 follows to implement the function. Also, the arguments passed in JavaScript are available in Cordova args.

C#
@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 be 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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Keyhole Software
United States United States
Keyhole is a software development and consulting firm with a tight-knit technical team. We work primarily with Java, .NET, and Mobile 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. Kansas City, St. Louis and Chicago.
This is a Organisation

3 members

Comments and Discussions

 
-- There are no messages in this forum --