Saturday, 5 December 2020

How to do Orientation, Network, and Battery status plugin in PhoneGap?

 In our previous section, we learned about the device info plugin. In this section, we will learn about the remaining plugins, i.e., device orientation plugin, Network Information plugin, and Battery status plugin. The complete description of each plugin is given below:

Device orientation plugin

For device orientation, we will use our previous example, i.e., Device info example. We will use the following steps to work with device orientation plugin in our PhoneGap application:

1) Create index2.html file

We will create a new file index2.html with the same code as preset in the index.html. We will make changes later in index2.html file not in index.html.


Orientation, Network, and Battery status plugin

2) Create a field to show the orientation on the UI

We will add a heading in our application using <h1></h1> tag. After that we will use a field for showing the orientation information on the user interface using <div></div> tag.

  1. <body>  
  2.         <h1> Hardware </h1>  
  3.         <div id="result"></div>  
  4.         <h1> Orientation </h1>  
  5.         <div id="orientation"></div>  
  6.         <script type="text/javascript" src="cordova.js"></script>  
  7. </body>  

3) Make changes in the window.onload function

Now, we will make some changes to the window.onload function. We will create a watch Id because the compass will keep working until we stop it. So eventually, we want to clear that watch Id. We will access it using the navigator.compass.watch heading (). This function takes three callback functions, i.e., success, error, and frequency. We will access the watchId in the following way:

  1. var watchId = navigator.compass.watchHeading(success, error, {frequency: 100});  

4) Create success and error callback function

We will make the success and error callback functions. The success and error functions take heading and error as arguments, respectively. In the error callback function, we will simply use the alert function, and in the success function, we will set the orientation data in the orientation div in the following way:

  1. function success(heading)  
  2. {  
  3.           document.getElementById('orientation').innerHTML = heading.magneticHeading;  
  4. }  
  5.               
  6. function error(error)  
  7. {  
  8.           alert("Compass Error: " + error.code);  
  9. }  

Note: We can or cannot see the orientation information. We can see it only when the manufacturer of the device allows it.

Network information plugin

Now, we will take a look at the network information plugin. We will add some lines of code in our index2.html. These are the following steps that we will perform to work with the Network Information plugin. By using this plugin, we can find out exactly what type of network we are connected to.

1) Create a field for showing the network information on the UI

We will add a heading in our application using <h1></h1> tag. After that we will use a field for showing the orientation information on the user interface using <div></div> tag.

  1. <body>  
  2.         <h1> Hardware </h1>  
  3.         <div id = "result" ></div>  
  4.         <script type="text/javascript" src="cordova.js"></script>  
  5.         <h1> Network Information </h1>  
  6.         <div id = "networkResult" ></div>  
  7.         <h1> Orientation </h1>  
  8.         <div id = "orientation" ></div>  
  9. </body>  

2) Adding network information in the window.onload function

We will create a variable and store the data returned by the navigator.connection.type and pass that variable into an alert function in the following way:

  1. //Network Information  
  2. var networkType = navigator.connection.type;  
  3. alert(networkType);   


Orientation, Network, and Battery status plugin

It really looks good but there is a bunch of other types which are required to know. The connection can be unknown, Ethernet, Wi-Fi, 2G, 3G and 4G etc. We can access different types of connection using enumeration. We will create an enumeration conType. We will access the element using id in which we show the result on the UI in the following way:

  1. var conType = {};  
  2. conType[ Connection.UNKOWN ] = "Connection Unknown";  
  3. conType[ Connection.ETHERNET ] = "Ethernet";  
  4. conType[ Connection.WIFI ] = "wifi";  
  5. conType[ Connection.CELL_2G ] = "Cell";  
  6. conType[ Connection.CELL_3G ] = "Cell";  
  7. conType[ Connection.CELL_4G ] = "Cell";  
  8. conType[ Connection.CELL ] = "Cell";  
  9. conType[ Connection.NONE ] = "None";  
  10. document.getElementById('networkResult').innerHTML = conType[networkType];   

Now, we will run our application on the PhoneGap Developer App:

Orientation, Network, and Battery status plugin

Battery status plugin

Battery status is another piece of the hardware that PhoneGap can interact with. It is required to look at battery status if we are going to perform a critical operation. We want to make sure that it is not going to run out of batteries during the operation. There is a couple of different ways to get the battery status. We will use the following steps to get the battery status in our application:

1) Create batterystatus event listener

Now, we will attach an event listener called batterystatus with our window. This event listener will call the onBatteryStatusChange function. This function will take the battery status as an argument. The batteryStatus event listener will be written in the following way:

  1. //Battery Status  
  2. window.addEventListener('batterystatus', onBatteryStatusChange, false);  

2) Create onBatteryStatusChange function

Now, we will create the onBatteryStatusChange callback function. This callback function contains an alert function which tells us about the level of the status and whether the status is plugged in or not. The onBatteryStatusChange function is designed in the following way:

  1. function onBatteryStatusChange(status)  
  2. {  
  3.                alert("Battery Level: " + status.level + " Plugged in: " + status.isPlugged);  
  4. }   

We can also use batterylow and batterycritical event listener rather than using batterystatus. They both work in the same way as battery status work. The batterylow and batterycritical event listeners are designed in the following way:

  1. window.addEventListener('batterylow', onBatteryStatusLow, false);  
  2. window.addEventListener('batterycritical', onBatteryCritical, false);  

Complete Code

  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="utf-8">  
  5.         <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">  
  6.         <title>Hardware</title>  
  7.         <script>  
  8.             window.onload=function()  
  9.             {  
  10.                 var out="";  
  11.                 out += "Cordova Version: " + device.cordova;  
  12.                 out += "<br/>Device Model: " + device.model;  
  13.                 out += "<br/>Device Platform: " + device.platform;  
  14.                 out += "<br/>UUID: " + device.uuid;  
  15.                 out += "<br/>Version: " + device.version;  
  16.                 out += "<br/>Manufacturer: " + device.manufacturer;  
  17.                 out += "<br/>Serial: " + device.serial;  
  18.                 document.getElementById('result').innerHTML = out;  
  19.                   
  20.                 //Network Information  
  21.                 var networkType = navigator.connection.type;  
  22.                 var conType = {};  
  23.                 conType[Connection.UNKOWN] = "Connection Unknown";  
  24.                 conType[Connection.ETHERNET] = "Ethernet";  
  25.                 conType[Connection.WIFI] = "wifi";  
  26.                 conType[Connection.CELL_2G] = "Cell";  
  27.                 conType[Connection.CELL_3G] = "Cell";  
  28.                 conType[Connection.CELL_4G] = "Cell";  
  29.                 conType[Connection.CELL] = "Cell";  
  30.                 conType[Connection.NONE] = "None";  
  31.                 document.getElementById('networkResult').innerHTML = conType[networkType];  
  32.               
  33.                 var watchId = navigator.compass.watchHeading(success, error, {frequency: 100});  
  34.                   
  35.                 //Battery Status  
  36.                 window.addEventListener('batterystatus', onBatteryStatusChange, false);  
  37.                   
  38. //                window.addEventListener('batterylow', onBatteryStatusLow, false);  
  39. //                  
  40. //                window.addEventListener('batterycritical', onBatteryCritical, false);  
  41. //                  
  42.                 function onBatteryStatusChange(status)  
  43.                 {  
  44.                     alert("Battery Level: " + status.level + " Plugged in: " + status.isPlugged);  
  45.                 }  
  46.                   
  47.                   
  48.             }  
  49.               
  50.             function success(heading)  
  51.             {  
  52.                 document.getElementById('orientation').innerHTML = heading.magneticHeading;  
  53.             }  
  54.               
  55.             function error(error)  
  56.             {  
  57.                 alert("Compass Error: " + error.code);  
  58.             }  
  59.           
  60.               
  61.               
  62.         </script>  
  63.     </head>  
  64.     <body>  
  65.         <h1>Hardware</h1>  
  66.         <div id="result"></div>  
  67.         <script type="text/javascript" src="cordova.js"></script>  
  68.         <h1>Network Information</h1>  
  69.         <div id="networkResult"></div>  
  70.         <h1>Orientation</h1>  
  71.         <div id="orientation"></div>  
  72.     </body>  
  73. </html>  

Output

Orientation, Network, and Battery status plugin


No comments:

Post a Comment