8

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

YAM
  • 474
  • 2
  • 7
  • 14

3 Answers3

9

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.

rahul.sapkal23
  • 707
  • 9
  • 15
  • I noticed after linking, MainApplication.java wasn't updated. So I updated it manually - Still NO luck I get --> **`TypeError: undefined is not an object (evaluating '_reactNativeDeviceInfo.DeviceInfo.getVersion')`** ` "react": "16.0.0", "react-native": "0.50.3", "react-native-device-info": "^0.14.0", ` – Azmeer Feb 02 '18 at 13:01
  • `"react": "16.0.0", "react-native": "0.50.3", "react-native-device-info": "^0.14.0" ` – Azmeer Feb 02 '18 at 13:26
3

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();
Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
0

Actually Push notification device id (combined with user device and your package name ) generated and send to device by google GCM server.you have to understand the GCM concept first then try..

GOOGLE GCM DOC

then try some react-native plugins like and link

sharma sk
  • 651
  • 4
  • 14
  • Do you have a sample code . i am stuck at it and since i am a beginner in this , facing lot of issues. – YAM Jun 13 '16 at 05:59
  • @YAM .. sorry i didn't try this before with react native.. i only use with cordova push plugin.. i don't know about react native. first you have to get server key and Sender Id from google developer console do you get that. – sharma sk Jun 13 '16 at 06:09
  • this plugin seems like very easy try this..[link](https://www.npmjs.com/package/react-native-gcm-push-notification) – sharma sk Jun 13 '16 at 06:12