CIS 136 Building Mobile Apps Device,Network, Vibration,Battery,Console https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-network-information/index.html CIS 136 Building Mobile Apps
Device
Device Plug-in org.apache.cordova.device describes the device's hardware and software Global in scope, but not available until the device is ready Device object has 5 properties cordova model platform uuid Version Manufacturer isVirtual serial
Device Object cordova property Gets the version of Cordova running on the device Ex: document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { $(‘#info’).html(device.cordova); }
Device Object model property Gets the the name of the device's model or product set by the device manufacturer and may be different across versions of the same product Might get the production code name Ex: Android: Nexus One returns "Passion" (Nexus One code name) Motorola Droid returns "voles" BlackBerry: Torch 9800 returns "9800" iOS: for the iPad Mini, returns iPad2,5; iPhone 5 is iPhone 5,1. document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { $(‘info’).html(device.model); }
Device Object platform property Gets the operating system name Ex: document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { $(‘info’).html(device.platform); }
Device Object uuid property Gets the Universally Unique Identifier a 128-bit value that is ‘practically unique’ determined by the device manufacturer and are specific to the device's platform or model. Ex: document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { $(‘info’).html(device.uuid); }
Device Object manufacturer property Gets the operating system version Kitkat 4.4.4 Ex: document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { $(‘info’).html(device.version); }
Device Object isVirtual property Determines Ex: document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { $(‘info’).html(device.isVirtual); }
Device Object serial property Gets the serial number Ex: document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { $(‘info’).html(device.serial); }
Device Object version property Gets the operating system version Kitkat 4.4.4 Ex: document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { $(‘info’).html(device.version); }
Network
Network Information Plug-in Navigator.connection provides information about the device's cellular and wifi connection Indicates if the device has an internet connection Connection Object has 1 property -connection.type connection.type has 8 values, available as constants connection.type Connection.UNKNOWN Connection.ETHERNET Connection.WIFI Connection.CELL_2G Connection.CELL_3G Connection.CELL_4G Connection.CELL Connection.NONE
navigator.connection.type determine the device's network connection state, and type of connection Ex: document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { var networkState = navigator.connection.type; $(‘info’).html(networkState); }
Network States Using the type of connection, coupled with the translation of network state constants, can provide a textual description document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { var networkState = navigator.connection.type; var states = {}; states[Connection.UNKNOWN] = 'Unknown connection'; states[Connection.ETHERNET] = 'Ethernet connection'; states[Connection.WIFI] = 'WiFi connection'; states[Connection.CELL_2G] = 'Cell 2G connection'; states[Connection.CELL_3G] = 'Cell 3G connection'; states[Connection.CELL_4G] = 'Cell 4G connection'; states[Connection.CELL] = 'Cell generic connection'; states[Connection.NONE] = 'No network connection'; $(‘info’).html(states[networkState]); }
Network related events offline - fires when an application goes offline, and the device is not connected to the Internet document.addEventListener("offline", yourCallbackFunction, false); online - fires when an application goes online, and the device becomes connected to the Internet document.addEventListener("online", yourCallbackFunction, false);
Vibration
Vibration provides a way to vibrate the device navigator.vibrate object Three different functionalities based on parameters passed to it Standard Vibration: vibrate(duration) duration is specified in milliseconds navigator.vibrate(2000); Pattern Vibration: Sequence of durations (in milliseconds) for which to turn on or off the vibrator To make a device vibrate, pause, then vibrate again, need to call vibrate several times and force the app to pause in between navigator.vibrate([1000,2000,3000,1000,5000]);
Vibration Cancel Vibration: Pass in a parameter of 0, an empty array, or an array with one element of value 0 vibrate(0) vibrate([]) Vibrate([0])
Battery
Battery Status Plug-in org.apache.cordova.battery-status a means for web developers to programmatically determine the battery status of the hosting device and whether the device is plugged in or not without knowing the battery status of a device, you are designing the web application with the assumption there is sufficient battery level for the task at hand battery of a device may exhaust faster than expected if apps are unable to make decisions based on the battery status given knowledge of the battery status, web developers are able to craft web content and applications which are power- efficient, thereby leading to improved user experience
Battery Status Plug-in Battery Status events can be used to defer or scale back work when the device is not plugged in or is low on battery Ex: In an advanced application, like an email app, the app can check the server for new email every few seconds if the device is plugged in, but do so less frequently if the device is not plugged in or is low on battery In an advanced application, like a note keeper, the app could monitor the battery level and save changes before the battery runs out to prevent data loss
Battery Status Events BatteryStatus events are fired when charge percentage changes by at least 1 percent when the device is plugged in or unplugged This event returns an object containing the battery status, which scales from critical to ok Battery Status Object has the following properties: isPlugged (Boolean) whether the device is plugged in level (float) Represents how much of the internal power source remains, scaled from 0 to 100 (null if no juice)
Battery Status Events Critical Low OK null - unable to report the battery's level threshold values are device-specific.
Battery Status Example The battery status handler is passed an object that contains two properties: level: The percentage of battery charge (0-100) isPlugged: A boolean that indicates whether the device is plugged in use an EventListener to attach an event handler after the deviceready event fires window.addEventListener("batterystatus", onBatteryStatus, false); function onBatteryStatus(info) console.log("Level: " + info.level + " isPlugged: " + info.isPlugged); }
Battery Critical The event fires when the percentage of battery charge has reached the critical battery threshold The value is device-specific
Battery Critical Example The batterycritical handler is passed an object that contains two properties: level: The percentage of battery charge (0-100) isPlugged: A boolean that indicates whether the device is plugged in window.addEventListener("batterycritical", onBatteryCritical, false); function onBatteryCritical(info) { alert("Battery Level Critical " + info.level + "%\nRecharge Soon!"); }
Battery Low This event fires when the percentage of battery charge has reached the low battery threshold device-specific value
Battery Low Example The batterylow handler is passed an object that contains two properties: level: The percentage of battery charge (0-100) isPlugged: A boolean that indicates whether the device is plugged in window.addEventListener("batterylow", onBatteryLow, false); function onBatteryLow(info) { alert("Battery Level Low " + info.level + "%"); }
Console
Cordova Console Plugin cordova-plugin-console log() – writes messages to the console document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { console.log(“message to console – Hello there”); }