Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
react-native-device-info
Advanced tools
The react-native-device-info package provides device information and system details for React Native applications. It allows developers to access a wide range of device-specific information, such as device ID, system version, and more.
Get Device ID
This feature allows you to retrieve the unique device ID of the device running the application.
import DeviceInfo from 'react-native-device-info';
const deviceId = DeviceInfo.getDeviceId();
console.log(deviceId);
Get System Version
This feature allows you to get the operating system version of the device.
import DeviceInfo from 'react-native-device-info';
const systemVersion = DeviceInfo.getSystemVersion();
console.log(systemVersion);
Get Application Version
This feature allows you to retrieve the version of the application currently running on the device.
import DeviceInfo from 'react-native-device-info';
const appVersion = DeviceInfo.getVersion();
console.log(appVersion);
Get Battery Level
This feature allows you to get the current battery level of the device.
import DeviceInfo from 'react-native-device-info';
DeviceInfo.getBatteryLevel().then(batteryLevel => {
console.log(batteryLevel);
});
Get Device Name
This feature allows you to retrieve the name of the device.
import DeviceInfo from 'react-native-device-info';
DeviceInfo.getDeviceName().then(deviceName => {
console.log(deviceName);
});
The react-native-device package offers basic device information like device ID and system version. It is simpler and may not provide as extensive information as react-native-device-info.
The react-native-battery package focuses specifically on battery-related information, such as battery level and charging status. It is more specialized compared to the broader scope of react-native-device-info.
Device Information for React Native.
Using npm:
npm install --save react-native-device-info
or using yarn:
yarn add react-native-device-info
⚠️ If you are on React Native > 0.47, you must use version 0.11.0 of this library or higher
react-native link react-native-device-info
(or using rnpm
for versions of React Native < 0.27)
rnpm link react-native-device-info
Add the following line to your build targets in your Podfile
pod 'RNDeviceInfo', :path => '../node_modules/react-native-device-info'
Then run pod install
In XCode, in the project navigator:
node_modules/react-native-device-info
.xcodeproj
fileIn XCode, in the project navigator, select your project.
libRNDeviceInfo.a
from the deviceinfo project to your project's Build Phases ➜ Link Binary With Libraries.xcodeproj
file you added before in the project navigator and go the Build Settings tab. Make sure All is toggled on (instead of Basic).$(SRCROOT)/../react-native/React
and $(SRCROOT)/../../React
Run your project (Cmd+R)
(Thanks to @brysgo for writing the instructions)
android/build.gradle
:...
ext {
// dependency versions
googlePlayServicesVersion = "<Your play services version>" // default: "+"
compileSdkVersion = "<Your compile SDK version>" // default: 23
buildToolsVersion = "<Your build tools version>" // default: "25.0.2"
targetSdkVersion = "<Your target SDK version>" // default: 22
}
...
android/app/build.gradle
:dependencies {
...
compile "com.facebook.react:react-native:+" // From node_modules
+ compile project(':react-native-device-info')
}
android/settings.gradle
:...
include ':app'
+ include ':react-native-device-info'
+ project(':react-native-device-info').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-device-info/android')
MainApplication.java
:+ import com.learnium.RNDeviceInfo.RNDeviceInfo;
public class MainApplication extends Application implements ReactApplication {
//......
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
+ new RNDeviceInfo(),
new MainReactPackage()
);
}
......
}
MainActivity.java
:+ import com.learnium.RNDeviceInfo.RNDeviceInfo;
public class MainActivity extends ReactActivity {
......
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
+ new RNDeviceInfo(),
new MainReactPackage()
);
}
}
(Thanks to @chirag04 for writing the instructions)
./<app-name>/node_modules/react-native-device-info/windows/RNDeviceInfo
and add RNDeviceInfo.csproj
RNDeviceInfo
you just added and press okMainReactNativeHost.cs
for your app and edit the file like so:+ using RNDeviceInfo;
......
protected override List<IReactPackage> Packages => new List<IReactPackage>
{
new MainReactPackage(),
+ new RNDeviceInfoPackage(),
};
(Thanks to @josephan for writing the instructions)
var DeviceInfo = require('react-native-device-info');
// or import DeviceInfo from 'react-native-device-info';
Gets the API level.
Examples
const apiLevel = DeviceInfo.getAPILevel();
// iOS: ?
// Android: 25
// Windows: ?
Notes
See API Levels
Gets the application name.
Examples
const appName = DeviceInfo.getApplicationName(); // "Learnium Mobile"
Gets the battery level of the device as a float comprised between 0 and 1.
Examples
DeviceInfo.getBatteryLevel().then(batteryLevel => {
// 0.759999
});
Notes
Returns -1 on the iOS Simulator
Gets the device brand.
Examples
const brand = DeviceInfo.getBrand();
// iOS: "Apple"
// Android: "Xiaomi"
// Windows: ?
Gets the application build number.
Examples
const buildNumber = DeviceInfo.getBuildNumber();
// iOS: "89"
// Android: 4
// Windows: ?
Notes
There is a type inconsistency: Android return an integer instead of the documented string.
Gets the application bundle identifier.
Examples
const bundleId = DeviceInfo.getBundleId(); // "com.learnium.mobile"
Gets the carrier name (network operator).
Examples
const carrier = DeviceInfo.getCarrier(); // "SOFTBANK"
Gets the device country based on the locale information.
Examples
const deviceCountry = DeviceInfo.getDeviceCountry(); // "US"
Gets the device ID.
Examples
const deviceId = DeviceInfo.getDeviceId();
// iOS: "iPhone7,2"
// Android: "goldfish"
// Windows: ?
Gets the device locale.
Examples
const deviceLocale = DeviceInfo.getDeviceLocale();
// iOS: "en"
// Android: "en-US"
// Windows: ?
Gets the device name.
Examples
const deviceName = DeviceInfo.getDeviceName();
// iOS: "Becca's iPhone 6"
// Android: ?
// Windows: ?
Android Permissions
Gets the time at which the app was first installed, in milliseconds.
Examples
const firstInstallTime = DeviceInfo.getFirstInstallTime();
// Android: 1517681764528
Gets the device font scale. The font scale is the ratio of the current system font to the "normal" font size, so if normal text is 10pt and the system font is currently 15pt, the font scale would be 1.5 This can be used to determine if accessability settings has been changed for the device; you may want to re-layout certain views if the font scale is significantly larger ( > 2.0 )
Examples
const fontScale = DeviceInfo.getFontScale(); // 1.2
Gets available storage size, in bytes.
Examples
const freeDiskStorage = DeviceInfo.getFreeDiskStorage();
// Android: 17179869184
// iOS: 17179869184
Notes
From developer.android.com:
Return the primary shared/external storage directory.
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.
Gets the device current IP address.
Examples
DeviceInfo.getIPAddress().then(ip => {
// "92.168.32.44"
});
Android Permissions
Notes
Support for iOS was added in 0.22.0
Gets the referrer string upon application installation.
Examples
const referrer = DeviceInfo.getInstallReferrer();
// If the app was installed from https://play.google.com/store/apps/details?id=com.myapp&referrer=my_install_referrer
// the result will be "my_install_referrer"
Gets the application instance ID.
Examples
const instanceId = DeviceInfo.getInstanceID();
// Android: ?
Notes
Gets the time at which the app was last updated, in milliseconds.
Examples
const lastUpdateTime = DeviceInfo.getLastUpdateTime();
// Android: 1517681764992
Gets the network adapter MAC address.
Examples
DeviceInfo.getMACAddress().then(mac => {
// "E5:12:D8:E5:69:97"
});
Android Permissions
Notes
iOS: This method always return "02:00:00:00:00:00" as retrieving the MAC address is disabled since iOS 7
Gets the device manufacturer.
Examples
const manufacturer = DeviceInfo.getManufacturer();
// iOS: "Apple"
// Android: "Google"
// Windows: ?
Returns the maximum amount of memory that the VM will attempt to use, in bytes.
Examples
const maxMemory = DeviceInfo.getMaxMemory(); // 402653183
Gets the device model.
Examples
const model = DeviceInfo.getModel();
// iOS: ?
// Android: ?
// Windows: ?
Gets the device phone number.
Examples
const phoneNumber = DeviceInfo.getPhoneNumber();
// Android: ?
Android Permissions
Notes
This can return
undefined
in certain cases and should not be relied on. SO entry on the subject.
Gets the application human readable version.
Examples
const readableVersion = DeviceInfo.getReadableVersion();
// iOS: 1.0.1
// Android: 1.0.1
// Windows: ?
Gets the device serial number.
Examples
const serialNumber = DeviceInfo.getSerialNumber();
// iOS: undefined
// Android: ?
// Windows: ?
Gets the device OS name.
Examples
const systemName = DeviceInfo.getSystemName();
// iOS: "iOS" on newer iOS devices "iPhone OS" on older devices, including older iPad's.
// Android: "Android"
// Windows: ?
Gets the device OS version.
Examples
const systemVersion = DeviceInfo.getSystemVersion();
// iOS: "11.0"
// Android: "7.1.1"
// Windows: ?
Gets the device default timezone.
Examples
const timezone = DeviceInfo.getTimezone(); // "Africa/Tunis"
Gets full disk storage size, in bytes.
Examples
const storageSize = DeviceInfo.getTotalDiskCapacity();
// Android: 17179869184
// iOS: 17179869184
Gets the device total memory, in bytes.
Examples
const totalMemory = DeviceInfo.getTotalMemory(); // 1995018240
Gets the device unique ID.
Examples
const uniqueId = DeviceInfo.getUniqueID();
// iOS: "FCDBD8EF-62FC-4ECB-B2F5-92C9E79AC7F9"
// Android: "dd96dec43fb81c97"
// Windows: ?
Notes
- iOS: This is
IDFV
so it will change if all apps from the current apps vendor have been previously uninstalled.- android: Prior to Oreo, this id (ANDROID_ID) will always be the same once you set up your phone.
Gets the device User Agent.
Examples
const userAgent = DeviceInfo.getUserAgent();
// iOS: "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143"
// Android: ?
// Windows: ?
Gets the application version.
Examples
const version = DeviceInfo.getVersion();
// iOS: "1.0"
// Android: "1.0
// Windows: ?
Tells if the user preference is set to 24-hour format.
Examples
const is24Hour = DeviceInfo.is24Hour(); // true
Tells if the application is running in an emulator.
Examples
const isEmulator = DeviceInfo.isEmulator(); // false
Tells if a PIN number or a fingerprint was set for the device.
Examples
DeviceInfo.isPinOrFingerprintSet()(isPinOrFingerprintSet => {
if (!isPinOrFingerprintSet) {
// ...
}
});
Notes
- Since the device setting for PIN/Fingerprint can be modified while the app is still open, this is available via callback instead of as a constant.
- iOS: Not supported for iOS < 9
Tells if the device is a tablet.
Examples
const isTablet = DeviceInfo.isTablet(); // true
When installing or using react-native-device-info
, you may encounter the following problems:
react-native-device-info
uses com.google.android.gms:play-services-gcm
to provide [getInstance()][#getinstance].
This can lead to conflicts when building the Android application.
If you're using a different version of com.google.android.gms:play-services-gcm
in your app, you can define the
googlePlayServicesVersion
gradle variable in your build.gradle
file to tell react-native-device-info
what version
it should require.
If you're using a different library that conflicts with com.google.android.gms:play-services-gcm
, you can simply
ignore this dependency in your gradle file:
compile(project(':react-native-device-info')) {
exclude group: 'com.google.android.gms'
}
Seems to be a bug caused by react-native link
. You can manually delete libRNDeviceInfo-tvOS.a
in Xcode -> [Your iOS build target] -> Build Phrases -> Link Binary with Libraries
.
react-native-device-info
contains native code, and needs to be mocked.
Here's how to do it with jest for example:
// in your package.json:
"jest": {
"setupFiles": [
"./testenv.js"
],
// testenv.js:
jest.mock('react-native-device-info', () => {
return {
getModel: jest.fn(),
};
});
See the CHANGELOG.md.
As a courtesy to developers, this library was made compatible in v0.17.0 with react-native-web by providing an empty polyfill in order to avoid breaking builds.
Only getUserAgent() will return a correct value. All other API methods will return an "empty" value of its documented return type: 0
for numbers, ''
for strings, false
for booleans.
0.22.1
FAQs
Get device information using react-native
The npm package react-native-device-info receives a total of 547,993 weekly downloads. As such, react-native-device-info popularity was classified as popular.
We found that react-native-device-info demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.