how to get device Id in react native android?

YAM picture YAM · Jun 13, 2016 · Viewed 14.2k times · Source

I am trying to get device id from android device. There are several links and packages available to get device id but very few are usable in case of Android. Below are links i had tried already with no results:

Link1: https://github.com/rebeccahughes/react-native-device-info I had tried this link with no success it may run fine on ios but on Android it is showing me error while running mentioned command on github link "react-native link react-native-device-info" Error: Link unrecognised

Link2: https://github.com/lazywei/react-native-device-uuid It has solution only for IOS

I am stuck here. I am getting no idea how i will resolve this issue? Please help?

Thanks in advance

Answer

rahul.sapkal23 picture rahul.sapkal23 · Nov 28, 2017

You not specified react native version number.

I used same 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.

  1. in android/app/build.gradle:
 dependencies {
        ...
        compile "com.facebook.react:react-native:+"  // From node_modules
    +   compile project(':react-native-device-info')
    }
  1. 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')

  1. 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.