Class: Device


CloudControl Pro 9 Docs / device / Device

Class: Device

device.Device

Device class provides methods to get device status, information, adjust device brightness, etc. It is a singleton, you can get the instance by const { device } = require('device').

Hierarchy

  • EventEmitter

    Device

Table of contents

Accessors

Methods

Accessors

androidId

get androidId(): string

Android ID.

See

https://developer.android.com/reference/android/provider/Settings.Secure#ANDROID_ID

Returns

string


battery

get battery(): number

The current battery level of the device, as a float between 0.0 and 1.0.

Example

"nodejs";
const { device } = require('device');
console.log(device.battery);

Returns

number


batteryPluggedTypes

get batteryPluggedTypes(): PluggedType[]

The array of the types of power source which the device is plugged in to, for example ["usb"] means connected to USB power, ["ac"] means connected to AC power. If the device is not connected to power, it returns an empty array.

Example

"nodejs";
const { device } = require('device');
console.log(device.batteryPluggedTypes);

Returns

PluggedType[]


bootloader

get bootloader(): string

The system bootloader version number.

See

https://developer.android.com/reference/android/os/Build.html#BOOTLOADER

Returns

string


brightness

get brightness(): number

The brightness mode of the device.

"nodejs";

const { device } = require("device");
console.log(device.brightness);
// Set the brightness to 255
device.brightness = 255;

See

https://developer.android.com/reference/android/provider/Settings.System#SCREEN_BRIGHTNESS

Returns

number

set brightness(value): void

Parameters

Name Type
value number

Returns

void


brightnessMode

get brightnessMode(): BrightnessMode

The brightness mode of the device.

"nodejs";

const { device, BrightnessMode } = require("device");
console.log(device.brightnessMode);
// Set the brightness to automatic brightness
device.brightnessMode = BrightnessMode.AUTOMATIC;

See

https://developer.android.com/reference/android/provider/Settings.System#SCREEN_BRIGHTNESS_MODE

Returns

BrightnessMode

set brightnessMode(value): void

Parameters

Name Type
value BrightnessMode

Returns

void


buildId

get buildId(): string

Either a changelist number, or a label like "M4-rc20".

See

https://developer.android.com/reference/android/os/Build.html#ID

Returns

string


cpuApis

get cpuApis(): string[]

An array of supported CPU architectures, such as ['arm64-v8a', 'armeabi-v7a'].

Returns

string[]


display

get display(): string

A build ID string meant for displaying to the user

See

https://developer.android.com/reference/android/os/Build.html#DISPLAY

Returns

string


displayMetrics

get displayMetrics(): DisplayMetrics

The display related information, such as width, height, pixel density, etc.

See

DisplayMetrics

Returns

DisplayMetrics


externalStorageDirectory

get externalStorageDirectory(): string

Return the primary shared/external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState().

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

On devices with multiple users (as described by UserManager), each user has their own isolated shared storage. Applications only have access to the shared storage for the user they're running as.

In devices with multiple shared/external storage directories, this directory represents the primary storage that the user will interact with. Access to secondary storage is available through Context.getExternalFilesDirs(String), Context.getExternalCacheDirs(), and Context.getExternalMediaDirs().

Applications should not directly use this top-level directory, in order to avoid polluting the user's root namespace. Any files that are private to the application should be placed in a directory returned by Context.getExternalFilesDir, which the system will take care of deleting if the application is uninstalled. Other shared files should be placed in one of the directories returned by getExternalStoragePublicDirectory.

Writing to this path requires the Manifest.permission.WRITE_EXTERNAL_STORAGE permission, and starting in Build.VERSION_CODES.KITKAT, read access requires the Manifest.permission.READ_EXTERNAL_STORAGE permission, which is automatically granted if you hold the write permission.

Starting in Build.VERSION_CODES.KITKAT, if your application only needs to store internal data, consider using Context.getExternalFilesDir(String), Context.getExternalCacheDir(), or Context.getExternalMediaDirs(), which require no permissions to read or write.

This path may change between platform versions, so applications should only persist relative paths.

See

https://developer.android.com/reference/android/os/Environment#getExternalStorageDirectory()

Returns

string


fingerprint

get fingerprint(): string

A string that uniquely identifies this build. Do not attempt to parse this value.

See

https://developer.android.com/reference/android/os/Build.html#FINGERPRINT

Returns

string


hardware

get hardware(): string

The name of the hardware (from the kernel command line or /proc).

See

https://developer.android.com/reference/android/os/Build.html#HARDWARE

Returns

string


imei

get imei(): null | string

Returns the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones. Return null if device ID is not available.

Starting with API level 29, applications are not allowed to access IMEI.

See

https://developer.android.com/reference/android/telephony/TelephonyManager#getDeviceId()

Returns

null | string


macAddress

get macAddress(): null | string

Returns

null | string


memoryInfo

get memoryInfo(): MemoryInfo

Return general information about the memory state of the system, including total memory, available memory, etc. This can be used to help decide how to manage your own memory.

Memory units are bytes.

Example

"nodejs";
const { device } = require('device');
console.log(`total memory: ${device.memoryInfo.totalMem / 1024 / 1024} MB`);
console.log(device.memoryInfo);

See

https://developer.android.com/reference/android/app/ActivityManager#getMemoryInfo(android.app.ActivityManager.MemoryInfo)

Returns

MemoryInfo


product

get product(): Product

Device product information, including product name, device name, device board name, product brand, product model.

Example

"nodejs";
const { device } = require('device');
console.log(device.product); 
// {
//  name: 'mars',
//  device: 'mars',
//  board: 'mars',
//  brand: 'Xiaomi',
//  model: 'M2102K1AC'
// }

See

https://developer.android.com/reference/android/os/Build.html#PRODUCT

Returns

Product


screenHeight

get screenHeight(): number

The height of the screen in pixels.

Returns

number


screenWidth

get screenWidth(): number

The width of the screen in pixels.

Returns

number


serial

get serial(): string

Gets the hardware serial number, if available.

Starting with API level 29, persistent device identifiers are guarded behind additional restrictions, and apps are recommended to use resettable identifiers (see Best practices for unique identifiers). This method can be invoked if one of the following requirements is met:

  • If the calling app has been granted the READ_PRIVILEGED_PHONE_STATE permission; this is a privileged permission that can only be granted to apps preloaded on the device.
  • If the calling app has carrier privileges (see TelephonyManager.hasCarrierPrivileges()) on any active subscription.
  • If the calling app is the default SMS role holder (see RoleManager.isRoleHeld(String)).
  • If the calling app is the device owner of a fully-managed device, a profile owner of an organization-owned device, or their delegates (see DevicePolicyManager.getEnrollmentSpecificId()).

If the calling app does not meet one of these requirements then this method will behave as follows:

  • If the calling app's target SDK is API level 28 or lower and the app has the READ_PHONE_STATE permission then Build#UNKNOWN is returned.
  • If the calling app's target SDK is API level 28 or lower and the app does not have the READ_PHONE_STATE permission, or if the calling app is targeting API level 29 or higher, then a SecurityException is thrown.

See

https://developer.android.com/reference/android/os/Build.html#getSerial()

Returns

string

Methods

cancelVibration

cancelVibration(): void

Turn the vibrator off.

See

https://developer.android.com/reference/android/os/Vibrator.html#cancel()

Returns

void


getVolume

getVolume(type): number

Gets the volume for a particular stream type. For example, using device.getVolume("music") to get the current music volume.

Parameters

Name Type Description
type StreamType The stream type whose volume should be returned. One of StreamType

Returns

number


getVolumeRange

getVolumeRange(type): StreamVolumeRange

Gets the volume range for a particular stream type. e.g. {min: 0, max: 15}.

Parameters

Name Type Description
type StreamType The stream type whose volume range should be returned. One of StreamType

Returns

StreamVolumeRange


setVolume

setVolume(type, volume, flags?): void

Sets the volume for a particular stream type. For example, device.setVolume("music", 10) to set the music volume to 10.

Parameters

Name Type Default value Description
type StreamType undefined The stream type whose volume should be set. One of StreamType
volume number undefined The volume to set. This value must be in the range 0 to maxVolume inclusive.
flags string[] [] Optional flags. For example, ['SHOW_UI', 'PLAY_SOUND', 'VIBRATE'] indicates that showing ui, playing sound and vibrating when adjusts volume. Flags can be any of the values in documentation of AudioManageropen in new window.

Returns

void


vibrate

vibrate(millis): void

Vibrate constantly for the specified period of time. The app should be in the foreground for the vibration to happen.

See

https://developer.android.com/reference/android/os/Vibrator.html#vibrate(long)

Parameters

Name Type Description
millis number Vibration time, in milliseconds

Returns

void