I want to get the phone number with **react-native-device-info **library, What happens is if I try with the android studio emulator I capture the number, but when I do it with a physical phone it doesn't capture me.
The two permissions that I ask for on the phone are:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_SMS" />
import api from '_services/api';
import {PermissionsAndroid, Platform} from 'react-native';
import DeviceInfo from 'react-native-device-info';
const UseSendPhoneNumber = () => {
const sendPhoneNumber = async token => {
if (!token || Platform.OS !== 'android') {
return;
}
const hasPhoneStatePermission = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE,
);
const hasReadSMSPermission = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_SMS,
);
if (
hasPhoneStatePermission === PermissionsAndroid.RESULTS.GRANTED &&
hasReadSMSPermission === PermissionsAndroid.RESULTS.GRANTED
) {
const phoneNumber = await DeviceInfo.getPhoneNumber();
if (phoneNumber) {
await api.sendPhoneNumber(phoneNumber);
}
}
};
return {
sendPhoneNumber,
};
};
export default UseSendPhoneNumber;
I tried with emulators, and if it captures the data, but with physical devices it does nothing for me.
CodePudding user response:
I also want this functionality once so i used below npm package :
It will works 100% but not with Phone having Minimum API Level of 22.But if want this same functionality without using npm package then wait it i find out i will surely add but if i not then wait for someone else to response.