Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get device Id in react native android?

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

like image 439
YAM Avatar asked Jun 13 '16 05:06

YAM


People also ask

What is device ID in react-native?

React Native Device ID The device ID uniquely identifies the devices and is determined automatically by the SDK. In the React Native SDK, we use the platform-specific logic for iOS or Android as covered in detail above.


2 Answers

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.

like image 101
rahul.sapkal23 Avatar answered Nov 10 '22 07:11

rahul.sapkal23


You can install this package : https://github.com/rebeccahughes/react-native-device-info

And then use it

import DeviceInfo from 'react-native-device-info';

const uniqueId = DeviceInfo.getUniqueID();
like image 41
Mahdi Bashirpour Avatar answered Nov 10 '22 06:11

Mahdi Bashirpour