android getLine1Number() returns empty string [duplicate] - android

This question already has answers here:
MSISDN : Is it a SIM Card Data? Why all The Provided Function (from Blackberry and Android) to fetch MSISDN not reliable?
(3 answers)
Closed 5 years ago.
I want to get the mobile number of users, which will be defaultly taken as username, when I used the following code to get the mobile number, it returns an empty string
TelephonyManager tMgr = (TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number();
String mobNo = mPhoneNumber.replace("+", "");
One reason I found is My phone number is unknown under Settings->About Phone->Status->SIM Status. How to rectify this issue without giving any burden to the users.

There are at least some SIM Cards/Operators out there who don't provide numbers - I have seen it and you have seen it. There is probably nothing you can do about it.

See getLine1Number() documentation. It says if its unavailable, it returns null. So #sstn is partially right. You can't do much about it. As far as I can remember this method gets number which is stored in the SIM. Some providers restrict the access and other don't provide it.
However that being said if you are really really pushing to get phone number from network automatically, you can try one more thing. Most of the network providers provide their own API. The API might require the user to connect to internet through the network data and not wi-fi or any other method. Although different providers in different countries can differ on that. You can potentially recognize the network provider using getNetworkOperator(), then use provider specific API to get the number. However not sure if its free. For eg: In Canada, Telus has its API but it requires you to partner with them. Similarly with Rogers etc.
If you don't want to go through that pain, the last option is to ask the user to enter it. If you are worried about its validity then you can always do one time code SMS verification.
Hope it helps.

Related

Get Android Device IMEI not Using READ_PHONE_STATE

I need to get an Android device's IMEI for authentication purposes. I'm aware that this is bad practice; we are currently working on a replacement system. For now, this is what I need (if it's even possible). All I can find are old questions that require READ_PHONE_STATE in the manifest, which I believe to be causing the issue. The code being called is:
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
and, later to get the IMEI:
telephonyManager.getDeviceId();
Is there any way to run this code without the above permission? Is there any other way to get the IMEI? Worst case scenario, I can put out a release with a SQLite update that hard-codes the value of each user and gets it form the db every time they need to make a request. This is feasible because there are only about a dozen users of the app, all internal employees. Is this is a better route to take? Thanks!
There is only one way to get IMEI from Android 10. You need privileged permissions, which is impossible to get, if your app is installed through the Google Play.
There are different ways how to get unique ids, take a look at best practices in links below.
Please see:
https://developer.android.com/training/articles/user-data-ids
https://developer.android.com/about/versions/10/privacy/changes#non-resettable-device-ids

Is it advised to verify a phone number locally..?

I see that most of the applications (SnapChat, WhhatsApp) do the phone number verification through a OTP/verification code.
I was just wondering why not avoid this if public String getLine1Number () gives you a valid phone number and only do verfication code based auth if the above method returns null or empty string.
I don't know how reliable is this way of doing things. Any advice is much appreciated.
Thanks
Somebody could implement a fake client and can then send whatever he likes. The server should never trust information it gets sent from the client.
No it isn't advised to do so. Phone numbers are not available in SIM for all operators, for eg in INDIA Tata teleservices you can not retrieve this number. However in some countries the operators have stored phone numbers on SIM, and we can get those.
However if present it can be used to prepopulate on the Edittext and let user change it and only later SMS should be sent, if user accepts.
Most mobile operators does not store MSISDN(phone number) in the SIM.
Even if getLine1Number() returns a String, the String might not be reliable. (due to porting/MSISDN change etc.)
Programmatically obtain the phone number of the Android phone
It will be best to ask the user for the MSISDN for verification SMS.
Alternatively, you can consider to use other identifier.
Is there a unique Android device ID?
Yes, and in fact not all android devices + SIM card combinations will pour the phone number in that method. Plus, what happens if someone is using a Dual SIM card, or attempting to use their personal phone number on their second phone? You should rely on the user input when it comes to the phone number they want to be identified with.

Get current mobile phone number of a user in my android application [duplicate]

This question already has answers here:
MSISDN : Is it a SIM Card Data? Why all The Provided Function (from Blackberry and Android) to fetch MSISDN not reliable?
(3 answers)
Closed 5 years ago.
Without asking the users to input their mobile numbers, I want to get the current mobile phone number associated with that mobile, so that I can send particular messages about deals and offers available at a particular location or country, by getting the mobile phone number when my app is installed at the first time. Is it possible in android? Will there be any problems associated with it, if I'm able to collect mobile phone numbers of users? I would like to more deepely about it. So comments and answers to my question are extremely welcomed. Since I don't know where to start with, any tutorials or related links are also welcomed..
TelephonyManager mManager =(TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = mManager.getLine1Number();
// check if mPhoneNumber is null then there is no number saved in the phone.
and set in manifest
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
and further reading here
hope it helps :)
Spurdow's answer is correct but works only if the number is available in the system. Not all the countries and/or wireless carriers support that. I tested reading phone number, and it gave my T-mobile phone number in USA correctly, whereas returned no number in a friend`s phone in Turkey. So plan your code accordingly(i.e., check a possible null result, ask the phone number to user etc.). For more information:
https://developer.android.com/reference/android/telephony/TelephonyManager.html#getLine1Number%28%29

Mobile app access [duplicate]

This question already has an answer here:
Get MSISDN from the SIM using Android
(1 answer)
Closed 9 years ago.
Can I access the mobile number of a device after installing an app? I can ask from the user for it but need to ensure that it is the correct number that he is inputting. Is there a way? Just need a way to ensure that the app is downloaded on the same mobile number device.
Some SIM providers save the number on the SIM. You can directly extract from SIM without user interaction. Use TelephonyManager as follows:
TelephonyManager teleManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String getSimNumber = teleManager.getLine1Number();
Although, it turns out that there might be a case when SIM is present and still you cannot read the number. Some SIM cards doesn't allow to do so while others just don't have the number in the SIM(since this field is not mandatory). Quoting TelephonyManager.getLine1Number() failing?, Android issue about TelephoneManager and this blog, you'll get to know that the presence of number within the SIM isn’t required for operation and is actually assigned directly by the network itself.
Hope this helps.

Verify a user identity via Phone Number or other unique ID (iOS/Android)

Following scenario:
I want to create an wifi hotspot on a public place (e.g. train station). Therefore i want to write an mobile application (iOS and/or Android) which works as a portal page. As I would be providing internet access via my wifi hotspot I need to make sure that people who want to log in verify their identity properly (responsibility). First I thought i could do something like a facebook login but I guess that would not be enough as people can create fake accounts.
Then I got the idea that I could maybe access their telephone number via their smartphones.
I googled alot and came to the conclusion that it is pretty tough on both platforms.
The iOS method seems to be deprecated and apps wont make it to the app store with that version. Android can read the phone number from the sim card, but not all providers store the number on the sim card.
Question
Is there any possibility to get the phone number? Or is there any other way to uniquely identify a person in a wifi network?
Of course I dont want to do any of that without asking for users permission etc...
Greetings and thanks in advance
Peter
Choosing the MAC address is a good choice, it is not the safest, but it is the more easy to do.
The IMEI is not always provided, and the SERIAL is not uniq.
But actually, I don't know how to do this with iOS.
What is about the MAC address of the phone? This should be unique enough and you can get is easily from android and from iOS.
Android: http://developer.android.com/reference/android/net/wifi/WifiInfo.html#getMacAddress%28%29
iOS: How can I programmatically get the MAC address of an iphone
Using IMEI number
Android Does provide functions/method to access device IMEI number
First Set <uses-permission android:name="android.permission.READ_PHONE_STATE" />
in android manifest
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// get IMEI
String imei = tm.getDeviceId();
Hope it helps
For NON CDMA or GSM devices like Tablet
Use
import android.provider.Settings.Secure;
private String android_id = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);
This would generate a unique code for the device..how ever it would change on when user reset the device ie factory reset it .. and it works for device having os > 2.2(froyo)
This really depends on who you are trying to identify. That may seem like an odd response, but usually, we are trying to identify two, not one, sets of credential elements.
First, we want ot identify the device itself -- is this the deivce we expect to see and can we trust that this device has not been tampered with. Second, we usually want to know if the user of this device at the moment is one we expect to be using this, now validated, device. (If I take your phone, without step #2, I just became you.)
This is why we have things like 802.1x. It lets me validate the user of a network, not just the device. For your case, consider something like:
For device validation, the combination of hte MAC address and the IMEI. TUrn those into a unique hash with some data from the user. For example.
ID = SHA1(IMEI+MAC(Wifi)+user password)
Now that we're pretty sure the device and user are correct, if you need to, you can go one step further and use that hash a key to encrypt a user validation step in your app. Even if the user is correct, if they're on the wrong device, their key won't decrypt.

Categories

Resources