'Get SIM number on android phone with React Native
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
