当前位置: 首页 > 文档资料 > PhoneGap 中文文档 >

Device

优质
小牛编辑
131浏览
2023-12-01

Device

The device object describes the device's hardware and software.

Properties

  • device.name
  • device.cordova
  • device.platform
  • device.uuid
  • device.version
  • device.model

Variable Scope

Since device is assigned to the window object, it is implicitly in the global scope.

// These reference the same `device`
var phoneName = window.device.name;
var phoneName = device.name;

Permissions

Android

app/res/xml/config.xml

<plugin name="Device" value="org.apache.cordova.Device" />

app/AndroidManifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

BlackBerry WebWorks

www/plugins.xml

<plugin name="Device" value="org.apache.cordova.device.Device" />

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>

iOS

No permissions are required.

Windows Phone

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

config.xml

<feature name="http://tizen.org/api/systeminfo" required="true"/>

Reference: Application Manifest for Tizen Web Application


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 (OS 5.0 and higher)
  • 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-x.x.x.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 Cordova: '  + device.cordova  + '<br />' +

'Device Platform: ' + device.platform + '<br />' +

'Device UUID: '     + device.uuid     + '<br />' +

'Device Model: '    + device.model    + '<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 returns voles.

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

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

  • Android
  • BlackBerry WebWorks (OS 5.0 and higher)
  • 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-x.x.x.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 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 (OS 5.0 and higher)
  • 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-x.x.x.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 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 (OS 5.0 and higher)
  • 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 cannot 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-x.x.x.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 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 your 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 (OS 5.0 and higher)
  • 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-x.x.x.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 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>