Device
The
device
object describes the device's hardware and software.
Properties
Variable Scope
Since device
is assigned to the window
object, it is implicitly in the global scope.
// These reference the same `device`
var phoneModel = window.device.model;
var phoneModel = device.model;
Accessing the Feature
As of version 3.0, Cordova implements device-level APIs as plugins.
Use the CLI's plugin
command, described in The Command-Line
Interface, to add or remove this feature for a project:
$ cordova plugin add org.apache.cordova.device
$ cordova plugin ls
[ 'org.apache.cordova.device' ]
$ cordova plugin rm org.apache.cordova.device
These commands apply to all targeted platforms, but modify the platform-specific configuration settings described below:
Amazon Fire OS
(in app/res/xml/config.xml) <feature name="Device"> <param name="android-package" value="org.apache.cordova.device.Device" /> </feature>
Android
(in app/res/xml/config.xml) <feature name="Device"> <param name="android-package" value="org.apache.cordova.device.Device" /> </feature>
BlackBerry WebWorks
(in www/plugins.xml) <feature name="Device"> <param name="blackberry-package" value="org.apache.cordova.device.Device" /> </feature> (in www/config.xml) <feature id="blackberry.app" required="true" version="1.0.0.0" /> <rim:permissions> <rim:permit>read_device_identifying_information</rim:permit> </rim:permissions>
Windows Phone (in
Properties/WPAppManifest.xml
)<Capabilities> <Capability Name="ID_CAP_WEBBROWSERCOMPONENT" /> <Capability Name="ID_CAP_IDENTITY_DEVICE" /> <Capability Name="ID_CAP_IDENTITY_USER" /> </Capabilities>
Reference: Application Manifest for Windows Phone
Tizen (in
config.xml
)<feature name="http://tizen.org/api/systeminfo" required="true"/>
Some platforms may support this feature without requiring any special configuration. See Platform Support for an overview.
device.model
Get the device's model name.
var string = device.model;
Description
The device.model
returns the name of the device's model or
product. The value is set by the device manufacturer and may be
different across versions of the same product.
Supported Platforms
- Android
- BlackBerry WebWorks 5.0+
- iOS
- Tizen
- Windows Phone 7 and 8
- Windows 8
Quick Example
// 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. See http://theiphonewiki.com/wiki/index.php?title=Models
//
var model = device.model;
Full Example
<!DOCTYPE html>
<html>
<head>
<title>Device Properties Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
var element = document.getElementById('deviceProperties');
element.innerHTML = 'Device Model: ' + device.model + '<br />' +
'Device Cordova: ' + device.cordova + '<br />' +
'Device Platform: ' + device.platform + '<br />' +
'Device UUID: ' + device.uuid + '<br />' +
'Device Version: ' + device.version + '<br />';
}
</script>
</head>
<body>
<p id="deviceProperties">Loading device properties...</p>
</body>
</html>
Android Quirks
- Gets the product name instead of the model name, which is often the production code name. For example, the Nexus One returns
Passion
, and Motorola Droid returnsvoles
.
Tizen Quirks
- Returns the device model assigned by the vendor, for example,
TIZEN
Windows Phone 7 and 8 Quirks
- Returns the device model specified by the manufacturer. For example, the Samsung Focus returns
SGH-i917
.
device.cordova
Get the version of Cordova running on the device.
var string = device.cordova;
Description
device.cordova
returns the version of Cordova running on the device.
Supported Platforms
- Amazon Fire OS
- Android
- BlackBerry WebWorks 5.0+
- iOS
- Tizen
- Windows Phone 7 and 8
- Windows 8
Quick Example
var name = device.cordova;
Full Example
<!DOCTYPE html>
<html>
<head>
<title>Device Properties Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
var element = document.getElementById('deviceProperties');
element.innerHTML = 'Device Model: ' + device.model + '<br />' +
'Device Cordova: ' + device.cordova + '<br />' +
'Device Platform: ' + device.platform + '<br />' +
'Device UUID: ' + device.uuid + '<br />' +
'Device Version: ' + device.version + '<br />';
}
</script>
</head>
<body>
<p id="deviceProperties">Loading device properties...</p>
</body>
</html>
device.platform
Get the device's operating system name.
var string = device.platform;
Supported Platforms
- Android
- BlackBerry WebWorks 5.0+
- iOS
- Tizen
- Windows Phone 7 and 8
- Windows 8
Quick Example
// Depending on the device, a few examples are:
// - "Android"
// - "BlackBerry"
// - "iOS"
// - "WinCE"
// - "Tizen"
var devicePlatform = device.platform;
Full Example
<!DOCTYPE html>
<html>
<head>
<title>Device Properties Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
var element = document.getElementById('deviceProperties');
element.innerHTML = 'Device Model: ' + device.model + '<br />' +
'Device Cordova: ' + device.cordova + '<br />' +
'Device Platform: ' + device.platform + '<br />' +
'Device UUID: ' + device.uuid + '<br />' +
'Device Version: ' + device.version + '<br />';
}
</script>
</head>
<body>
<p id="deviceProperties">Loading device properties...</p>
</body>
</html>
BlackBerry Quirks
Devices may return the device platform version number instead of the
platform name. For example, the Storm2 9550 returns a value such as
2.13.0.95
.
Windows Phone 7 Quirks
Windows Phone 7 devices report the platform as WinCE
.
Windows Phone 8 Quirks
Windows Phone 8 devices report the platform as Win32NT
.
device.uuid
Get the device's Universally Unique Identifier (UUID).
var string = device.uuid;
Description
The details of how a UUID is generated are determined by the device manufacturer and are specific to the device's platform or model.
Supported Platforms
- Android
- BlackBerry WebWorks 5.0+
- iOS
- Tizen
- Windows Phone 7 and 8
- Windows 8
Quick Example
// Android: Returns a random 64-bit integer (as a string, again!)
// The integer is generated on the device's first boot
//
// BlackBerry: Returns the PIN number of the device
// This is a nine-digit unique integer (as a string, though!)
//
// iPhone: (Paraphrased from the UIDevice Class documentation)
// Returns a string of hash values created from multiple hardware identifies.
// It is guaranteed to be unique for every device and can't be tied
// to the user account.
// Windows Phone 7 : Returns a hash of device+current user,
// if the user is not defined, a guid is generated and will persist until the app is uninstalled
// Tizen: returns the device IMEI (International Mobile Equipment Identity or IMEI is a number
// unique to every GSM and UMTS mobile phone.
var deviceID = device.uuid;
Full Example
<!DOCTYPE html>
<html>
<head>
<title>Device Properties Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
var element = document.getElementById('deviceProperties');
element.innerHTML = 'Device Model: ' + device.model + '<br />' +
'Device Cordova: ' + device.cordova + '<br />' +
'Device Platform: ' + device.platform + '<br />' +
'Device UUID: ' + device.uuid + '<br />' +
'Device Version: ' + device.version + '<br />';
}
</script>
</head>
<body>
<p id="deviceProperties">Loading device properties...</p>
</body>
</html>
iOS Quirk
The uuid
on iOS is not unique to a device, but varies for each
application, for each installation. It changes if you delete and
re-install the app, and possibly also when you upgrade iOS, or even
upgrade the app per version (apparent in iOS 5.1). The uuid
is not
a reliable value.
Windows Phone 7 and 8 Quirks
The uuid
for Windows Phone 7 requires the permission
ID_CAP_IDENTITY_DEVICE
. Microsoft will likely deprecate this
property soon. If the capability is not available, the application
generates a persistent guid that is maintained for the duration of the
application's installation on the device.
device.version
Get the operating system version.
var string = device.version;
Supported Platforms
- Android 2.1+
- BlackBerry WebWorks 5.0+
- iOS
- Tizen
- Windows Phone 7 and 8
- Windows 8
Quick Example
// Android: Froyo OS would return "2.2"
// Eclair OS would return "2.1", "2.0.1", or "2.0"
// Version can also return update level "2.1-update1"
//
// BlackBerry: Torch 9800 using OS 6.0 would return "6.0.0.600"
//
// iPhone: iOS 3.2 returns "3.2"
//
// Windows Phone 7: returns current OS version number, ex. on Mango returns 7.10.7720
// Tizen: returns "TIZEN_20120425_2"
var deviceVersion = device.version;
Full Example
<!DOCTYPE html>
<html>
<head>
<title>Device Properties Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
var element = document.getElementById('deviceProperties');
element.innerHTML = 'Device Model: ' + device.model + '<br />' +
'Device Cordova: ' + device.cordova + '<br />' +
'Device Platform: ' + device.platform + '<br />' +
'Device UUID: ' + device.uuid + '<br />' +
'Device Version: ' + device.version + '<br />';
}
</script>
</head>
<body>
<p id="deviceProperties">Loading device properties...</p>
</body>
</html>
device.name
WARNING: device.name
is deprecated as of version 2.3.0. Use device.model
instead.
Get the device's model name.
var string = device.name;
Description
device.name
returns the name of the device's model or product. This
value is set by the device manufacturer and may be different across
versions of the same product.
Supported Platforms
- Android
- BlackBerry WebWorks 5.0+
- iOS
- Tizen
- Windows Phone 7 and 8
- Windows 8
Quick Example
// Android: Nexus One returns "Passion" (Nexus One code name)
// Motorola Droid returns "voles"
// BlackBerry: Torch 9800 returns "9800"
// iOS: All devices returns either "iPhone", "iPod Touch", "iPhone Simulator", "iPad", "iPad Simulator"
//
var name = device.name;
Full Example
<!DOCTYPE html>
<html>
<head>
<title>Device Properties Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
var element = document.getElementById('deviceProperties');
element.innerHTML = 'Device Name: ' + device.name + '<br />' +
'Device Model: ' + device.model + '<br />' +
'Device Cordova: ' + device.cordova + '<br />' +
'Device Platform: ' + device.platform + '<br />' +
'Device UUID: ' + device.uuid + '<br />' +
'Device Version: ' + device.version + '<br />';
}
</script>
</head>
<body>
<p id="deviceProperties">Loading device properties...</p>
</body>
</html>
Android Quirks
- Gets the product name instead of the model name, which is often the production code name. For example, the Nexus One returns
Passion
, and Motorola Droid returnsvoles
.
Windows Phone 7 and 8 Quirks
- Returns the device model specified by the manufacturer. For example, the Samsung Focus returns
SGH-i917
.
Tizen Quirks
- Returns the device model assigned by the vendor, for example,
TIZEN