Monday, February 27, 2017

Using Cordova Device API in gwt phonegap

 
Cordova Device API provides access to the device hardware and software information like what is the OS the device is running, its OS version and Serial id etc..

Configuration


This is a global object and is accessible any time after the deviceready event is triggered.

For using this API, you need to add the Cordova Device Plugin to the project.
cordova plugin add cordova-plugin-device
After your app is launched, include the below code to use any Apache Cordova API’s in the project.
final PhoneGap phoneGap = GWT.create(PhoneGap.class);
The Device API can be used only after the deviceready event is fired. So once your app is launched check for PhoneGapAvailableEvent. This event is fired once the Cordova library is successfully loaded and is ready for use.
    phoneGap.addHandler(new PhoneGapAvailableHandler() {
              @Override
              public void onPhoneGapAvailable(PhoneGapAvailableEvent event) {
                   // start your application code here.              }
            });
The Device API can be accessed through the Device object. The Device Object can be accessed as below

Device device = phoneGap.getDevice();

Usage


platform


Description


This property returns the operating system the device is running.
iOS devices return iOS while the Android devices return Android.

Code

String platform = device.getPlatform();

Supported Platforms


  • Android
  • iOS
  • Windows

version


Description


This property returns the device Operating system version number.

Code

String version = device.getVersion();

Supported Platforms


  • Android
  • iOS
  • Windows

cordova


Description


This property provides information of the platform specific cordova version running on the device. The cordova version and platform specific cordova  versions may be different. The cordova version displayed when you use cordova –v is the Cordova tooling and CLI version installed on your development machine.The cordova version may be 6.1, but the android platform cordova version may be 5.1 and ios platform cordova version may be 4.1. This platform specific Cordova version is the version of the cordova.js file in each platform. To get the platform specific version information from Cordova CLI, use the below command.
cordova platform list

image_thumb1


Code

String cordovaVersion = device.getPhoneGapVersion();

Supported Platforms


  • Android
  • iOS
  • Windows

model


Description


This property provides the information about the device model or product. This value is set by the manufacturer and may vary across the versions of the same product and these may not be well known names.
Ex: iPhone 6 is iPhone7,2.
On Android, this returns the product name instead of the device model.
The list of android devices product names mapping to common names is available at the below location.
https://support.google.com/googleplay/answer/1727131
And a similar list for iOS devices is available at the below url.
http://www.everyi.com/by-identifier/ipod-iphone-ipad-specs-by-model-identifier.html

Code

String model = device.getModel();

Supported Platforms


  • Android
  • iOS
  • Windows

manufacturer


Description


This property returns the name of the manufacturer of the device.
Ex: For iPhone, it would be Apple and for Samsung Galaxy Note, it would be samsung etc.

Code

String manufacturer = device.getManufacturer();

Supported Platforms


  • Android
  • iOS
  • Windows

uuid


Description


This property returns the device UUID(Universally Unique Identifier). The process of generating this UUID is different for different OS and is determined by the manufacturer.

On Android, uuid is a 64 bit integer created on first boot which is returned as string.

On iOS,uuid is created when a app from a vendor is installed. It will be same for all the apps from the same vendor. But if all the apps from the same vendor are reinstalled, then uuid will be recreated and will be different from the earlier one.

Code

String uuid = device.getUuid()

Supported Platforms


  • Android
  • iOS
  • Windows

serial


Description


This property returns the hardware serial number of the device, if available. Works only on physical devices since the emulators doesn’t have serial number.

Code

String serial = device.getSerial()

Supported Platforms


  • Android only.

isVirtual


Description


This property indicates whether the app is running on an emulator or an actual physical device.


Code

boolean isVirtual = device.isVirtual();

Supported Platforms


  • Android
  • iOS
  • Windows

Demo


The below example demonstrates the usage of the Device functions described above.

FlowPanel container = new FlowPanel();
Form widgetList = new Form();
widgetList.setHeader(new Label("Device"));
Device device = phoneGap.getDevice();
widgetList.add(new FormEntry("Platform", createTextBox(device
        .getPlatform())));
widgetList.add(new FormEntry("OS Version", createTextBox(device
        .getVersion())));
widgetList.add(new FormEntry("Cordova Version",
        createTextBox(device.getPhoneGapVersion())));
widgetList.add(new FormEntry("Model", createTextBox(device
        .getModel())));
widgetList.add(new FormEntry("Manufacturer", createTextBox(device
        .getManufacturer())));
widgetList.add(new FormEntry("Name",
        createTextBox(device.getName())));
widgetList.add(new FormEntry("Serial", createTextBox(device
        .getSerial())));
widgetList.add(new FormEntry("UUID",
        createTextBox(device.getUuid())));
widgetList.add(new FormEntry("Is Virtual", createTextBox(String
        .valueOf(device.isVirtual()))));
container.add(widgetList);
RootPanel.get("DeviceDemo").add(container);

The result of the program when it is run on different devices is as below.

Android emulator


g1_thumb1

On the Android emulator, since the manufacturer and the serial are displayed as unknown since this is a virtual device. As you can observe the device is indicated as a virtual device. Since the name of the device property is depreciated, the name value is not displayed.

Android Genymotion emulator


g2_thumb1

On a Genymotion Android emulator, the manufacturer is indicated as ‘Genymotion’ and there is no serial number as this is a virtual device and the serial number is derived from the hardware.
And similar to the other Android emulator, the device is indicated as a virtual device.
The model of the device selected in the Genymotion configuration is indicated as the model.
And the name of the device is depreciated and so no information is displayed against that.

Samsung device


Screenshot_2016-04-28-19-13-33_thumb

When the same program is run on an actual Samsung device, you see slightly different results.
The virtual status is returned as false, indicating that the device is an actual physical device.
The serial number is available since this is a real physical device.
The manufacturer name is displayed as ‘samsung’, since this is a samsung device.
The model of this device is displayed and to understand its common name, a reference to https://support.google.com/googleplay/answer/1727131 indicates that the market name of the device is ‘Galaxy Note3’.
Similar to emulator, the name is not displayed since it is depreciated.

iPhone


When the program is run on the iPhone

image_thumb4

The app is running on iPhone 6 device, so the isVirtual device flag is indicated as false.
And since name is not depreciated and serial is not supported on iOS, those values are set to blank and unknown respectively.
If we look up http://www.everyi.com/by-identifier/ipod-iphone-ipad-specs-by-model-identifier.html for iPhone7,2, we find out it is one of the iPhone 6 models.


iPad


When the app is run on iPad,


image_thumb3

The app is running on iPad device, so the isVirtual device flag is indicated as false.
And since name is not depreciated and serial is not supported on iOS, those values are set to blank and unknown respectively.
If we look up http://www.everyi.com/by-identifier/ipod-iphone-ipad-specs-by-model-identifier.html for iPad3,4, we find out it is iPad 4th Gen wifi model.


iPhone Simulator


When the program is run on the iPhone simulator:

image_thumb6

Since this is running on an emulator, the virtual flag is indicated as false and the name and serial are not supported on iOS and since this is a emulator, the model is indicated differently.

         The code displaying the usage of this API is available on github.








































2 comments:

  1. Really an amazing post..! By reading your blog post I gathered more information. I really appreciate your news. Thanks a lot for posting individual information and made me more knowledgeable person. I hope it will be very helpful for all. I don't have words to describe this blog. Keep on blogging!!
    Engineering Colleges in Chennai, Mechanical Engineering Colleges in Chennai

    ReplyDelete
  2. Thanks for information admin and get more topic to be add your site for me For more Details: Yadav Matrimony,Christian Matrimony,Matrimony,Trichy Matrimony.

    ReplyDelete