Get device id in react native?
Instead of expo you can create a project using react-native CLI.
Create a new project using command :
react-native init Myproject
cd Myproject
Run the project using the following command :
react-native run-android
Then you can use react-native-device-info package.
I tried with following versions and it worked for me.
"react": "16.0.0",
"react-native": "0.50.4",
"react-native-device-info": "^0.12.1",
I installed it with command :
npm install --save react-native-device-info
Then I link it with command :
react-native link react-native-device-info
If you are facing any issue while linking the package then you can do the manual link or you can cross check the packages is successfully link or not.
- in android/app/build.gradle:
dependencies { ... compile "com.facebook.react:react-native:+" // From node_modules + compile project(':react-native-device-info') }
- in 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')
- in 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() ); } ...... }
Permissions
Add the appropriate, optional permissions to your AndroidManifest.xml:
...
<uses-permission android:name="android.permission.BLUETOOTH"/> <!-- for Device Name -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <!-- for Phone Number -->
Example
var DeviceInfo = require('react-native-device-info');
// or import DeviceInfo from 'react-native-device-info';
var deviceId = DeviceInfo.getUniqueID();
you can use above deviceId.
If you are using Expo try this.
import Constants from 'expo-constants';
this.clientId = Constants.deviceId;
you cannot use library that use native code with expo see caveats if you want to use native code you need to create your project with native code at getting started doc basically
- install pre request apps (node - Java Development Kit - android studio - xcode - react native cli )
- run command
react-native init MyProjectName
- cd your project folder
cd MyProjectName
- run
react-native run-android
orreact-native run-ios
if you want to convert expo project to native code project you can do ejecting-from-create-react-native-app
- install native code requirement as above
- run
npm run eject
after that you can use third party libraries that use native code and run react-native link