Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expo get unique device id without ejecting

Tags:

This library allows you to get unique device id / Mac address of Android devices, which doesn't change after reinstallation.

Expo.Constants.deviceId changes after every reinstallation (even if the app version number is the same).

Is there a way to get an unique id for Android that doesn't change after reinstallation (at least for if it's the same version), without ejecting?

like image 862
Avery235 Avatar asked Oct 21 '17 13:10

Avery235


People also ask

How do I find my unique device ID in Expo?

Just use getuniqueid() method from react-native-device-info. Works on iOS and android to uniquely identify a device .

How do I get a unique device ID in react-native?

To get the device's Unique ID and we can use the react-native-device-info library. The unique device ID is used to identify a device uniquely.

How do I run expo app on physical device?

Download Expo Go from the Apple App Store or from the Google Play Store. On your iPhone or iPad, open the default Apple "Camera" app and scan the QR code you see in the terminal. On your Android device, press "Scan QR Code" on the "Home" tab of the Expo Go app and scan the QR code you see in the terminal.


2 Answers

For Expo IOS theres currently very limited options, Since Apple forbids getting private device info. We will need to create our own unique identifier below.

My solution is a combination of uuid and Expo SecureStore works for IOS and Android.

import * as SecureStore from 'expo-secure-store';
import 'react-native-get-random-values';
import { v4 as uuidv4 } from 'uuid';


let uuid = uuidv4();
await SecureStore.setItemAsync('secure_deviceid', JSON.stringify(uuid));
let fetchUUID = await SecureStore.getItemAsync('secure_deviceid');
console.log(fetchUUID)

This solution will work even if app gets reinstalled, or if user switches devices and copy's all data to new device. (Expo.Constants.deviceId is deprecated and will be removed in SDK 44)

like image 193
Morris S Avatar answered Sep 19 '22 04:09

Morris S


Use Application.androidId from expo-application. Id will not change after reinstall or update. The value may change if a factory reset is performed on the device or if an APK signing key changes. https://docs.expo.dev/versions/latest/sdk/application/#applicationandroidid

Example:

import * as Application from 'expo-application';
import { Platform } from 'expo-modules-core';
import * as SecureStore from 'expo-secure-store';
import Constants from 'expo-constants';

const getDeviceId = async () => {
  if (Platform.OS === 'android') {
    return Application.androidId;
  } else {
    let deviceId = await SecureStore.getItemAsync('deviceId');

    if (!deviceId) {
      deviceId = Constants.deviceId; //or generate uuid
      await SecureStore.setItemAsync('deviceId', deviceId);
    }

    return deviceId;
  }
}
like image 29
Dmitro_ Avatar answered Sep 22 '22 04:09

Dmitro_