Using PhoneGap Plugin to Expand BlackBerry Feature Support
Azoft Blog Using PhoneGap Plugin to Expand BlackBerry Feature Support

Using PhoneGap Plugin to Expand BlackBerry Feature Support

By Ildar Galimbikov on June 18, 2013

Using PhoneGap to develop a mobile app allows you to expedite delivery time of the project, since you are creating the app for several mobile platforms simultaneously. The drawback of using PhoneGap is that not all features of mobile operating systems are supported. This limitation can be partially solved by PhoneGap plugins that provide support for specific features of the devices not supported by the core framework. So today we are going to shed some light on how to write plugins for BlackBerry devices. 

PhoneGap in a nutshell

PhoneGap in a nutshell
Image from html5hu.wordpress.com

PhoneGap is a free framework that allows creating applications for various mobile platforms  — iOS, Android, BlackBerry, WebOS, Symbian, Windows Phone 7, and Bada  — without changing much code.

The major tools are HTML, JavaScript, and CSS. You are writing a mobile app like an ordinary site or web service. PhoneGap is expanding browser's API, granting permissions to use camera, compass, contacts, files, GEOlocation, media, the Internet and local networks, notifications and data storage.

Why use PhoneGap?

  • You write code once for all platforms
  • You don't need to learn Objective-C, Java or other languages, as you can make it by HTML, JavaScript, and CSS alone.
  • PhoneGap apps work similarly on various platforms.

Why you should think twice before using PhoneGap?

  • PhoneGap provides just partial support for mobile OS features. You can check the list of supported featured for all mobile platforms on the official web site.
  • New OS features might not appear right after the release, if at all.
  • App looks alike on different platforms as they do not use native OS components.

Developing a PhoneGap plugin

A PhoneGap plugin connects the WebView which serves as a container for the application with platform native components. PhoneGap plugins provide common JavaScript APIs shared across different platforms together with their platform-specific implementations.

The plugin APIs have the same underlying principle as interfaces in Java. A JavaScript file declares the API methods and the implementation of these methods are different across different platforms.

To create a plugin for a BlackBerry smartphone:

  • Create a JavaScript file and declare all the variables and functions that you will use in the PhoneGap plugin.
  • Create a Java class derived from org.apache.cordova.api.Plugin and implement a method with the following signature: public PluginResult execute(String action, JSONArray args, String callbackId).

Sample 1. JavaScript code snippet.

AzcPush.prototype.registerDevice = function (url,port,appId) {
      cordova.exec(function(winParam) {}, 
					 function(error) {},
					 "AzcPush", 
					 "registerDevice", 
					 [ url,port,appId]);  
  };

The snippet above shows a function called registerDevice with three arguments: url, port and appId. In the function body we call a function named cordova.exec.

Let's take a look at cordova.exec parameters:

  • function(winParam) {} — the callback to run on successful termination of exec;
  • function(error) {} — the callback to run on erroneous termination of exec;
  • "AzcPush" — the name of class which cordova is addressing;
  • "registerDevice" — method in the AzcPush class which is called by cordova;
  • [url, port, appId] — arguments passed down to registerDevice.

Plugin description in plugins.xml:

<plugin name="azcPush" value="org.apache.cordova.plugins.azcpush.AzcPush">

Sample 2. Java code snippet.

public class AzcPush extends Plugin {
	private final static String ACTION_REGISTER_DEVICE = "registerDevice";
	private final static String TAG = "AzcPush: ";
  	private String callbackId = null;

	public PluginResult execute(String action, JSONArray args, String callbackId) {
		PluginResult result;
		if (ACTION_REGISTER_DEVICE.equals(action)) {
			this.callbackId = callbackId;
      		result = registerDevice(args);
     	} else {
      	result = new PluginResult(PluginResult.Status.INVALID_ACTION, TAG
          + "Invalid action: " + action);
    	}
    	return result;
  	}
	private PluginResult registerDevice(JSONArray args) {
		return new PluginResult(PluginResult.Status.OK, "");
  	}
}

Let's see what happens when the Java method is called from JavaScript. When PhoneGap tries to call window.plugins.AzcPush.registerDevice("http://bb.com","2050","appId"), registerDevice is executed from the JavaScript file which in turn has the code to call cordova.exec with the required parameters. cordova then looks for a plugin called AzcPush in the org.apache.cordova.plugins.azcpush.AzcPush package. Finally, the execute method of AzcPush is being called.

Challenges of BlackBerry plugin development

  • Compiling and signing the project is being done from the command line. These steps take time to get used to.
  • Compile-time error checking is only performed for Java files. HTML and JavaScript errors will occur during the application execution.
  • There is no debugger.

PhoneGap plugins certainly have their own quirks, but they also have benefits. If your application uses PhoneGap and the framework lacks some feature, a plugin can help to get the required functionality in a good time frame.

VN:F [1.9.22_1171]
Rating: 5.0/5 (2 votes cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)
Using PhoneGap Plugin to Expand BlackBerry Feature Support, 5.0 out of 5 based on 2 ratings



Request a Free Quote
 
 
 

Please enter the result and submit the form

Content created by Ildar Galimbikov