šŸ•µļøAccessing device location using SIM Card šŸ—ŗļøšŸ“

In this article, weā€™ll see how to access device location using the SIM card in Android without using GPS or location service

Published in
5 min readJul 28, 2020

--

These days, almost many apps use the device locationšŸ“. There might be some use case in your app which is strongly dependent on the userā€™s location. For example, if you have developed a social app šŸ’¬ which shows posts of users and imagine youā€™ve feature which shows trending posts in userā€™s nearby area. For such use case, youā€™ll get userā€™s device location and after some processing, youā€™ll be able to manage this feature for your cool app.

But waitā€¦ šŸ¤”

What if your app userā€™s enabled Mock location? šŸ˜®

If your cool app users have enabled mock locations then your feature will not work as expected. If the user is currently at Mumbai, India and he enabled mock location and showing his location at Beijing, China šŸ˜© then that Indian user will see useless Chinese content in the app. Your cool appā€™s cool feature wonā€™t work as expected šŸ˜¢

Then is there any solution? šŸ¤·ā€ā™€ļø

YesšŸ˜ƒ, for such use case you wonā€™t need userā€™s perfect location. Just area/city level location is enough for such use case. You can get userā€™s cell tower location by extracting the SIM cardšŸ“” details from the userā€™s device.

The famous app TikTok app extracts SIM CardšŸ“¶ details and fetches the userā€™s location for the perfect updates and avoiding spoofing of location.

How did we come up with this Idea? šŸ’”

We at ScaleReal were developing a product which was heavily dependent on the userā€™s location and we thought ā€œWhat if GPS location isnā€™t available due to conditions like device failure or bad weather or if user provides fake locationā€ šŸ¤”. We thoroughly studied these scenarios with all aspects šŸ§ and came with the solution of using SIM card details to extract at least the cell tower location of the user. This solution helped us to pinpoint the user's location using cell tower triangulation and the results were even better when we wrote a custom algorithm on top of these two. In turn a better product! šŸ˜„

In this article, weā€™ll learn to access the deviceā€™s cell tower location by extracting SIM Card details from the device. So letā€™s start implementation.

Note: GPS is not involved in this process. So doesnā€™t matter if itā€™s enabled or disabled!

Letā€™s Start šŸš€

First of all, weā€™ll need to get API for accessing Geolocation information. Iā€™ve seen two APIs for such use casešŸ¤”.

  • Googleā€™s Geolocation API ā€” This API is good to go with but requires a billing account to be enabled for your projectšŸ¤‘.
  • Unwiredlabs OpenCellID ā€” This API is amazing and easy to use and the worldā€™s largest open database of cell towersšŸ˜. (Weā€™re using this API for geolocation)

Just go to above link and Sign Up your account and youā€™ll get API key. See API documentation here for more details about accessing API.

How API Works?šŸ¤”

From your app, you just have to send this data payload to API:

API Spec for Geolocation(Unwiredlabs)

Letā€™s understand these parameters:

  • radio ā€” Network type. For e.g. GSM, LTE, etc
  • mcc ā€” Mobile Country Code used to identify the country which a mobile subscriber belongs to.
  • mnc ā€” Used to uniquely identify a mobile subscribers network the MCC is combined with a Mobile Network Code.
  • lac ā€” Location Area Code
  • cid ā€” Cell ID

When you request API with above details, youā€™ll get a response from API as belowšŸ‘‡.

API Spec for Geolocation(Unwiredlabs)

Thatā€™s the main thing!

I hope now you have API key with you so letā€™s proceed to implementationšŸš€.

Getting Started šŸ’»

  • Open Android Studio
  • Create a new project or you can simply clone or import this project.

Make Request model

Request model will be used to request API with data payload. This will include network details of the device. Hereā€™s you can see how our request model will look like šŸ‘‡

Make Response model

Weā€™ll get a response from API and for that, weā€™ll have to make response model as below.

So now we have to make a service which will communicate with API which will look like as below šŸ‘‡

We have used Retrofit in this app. Iā€™ll not show the whole implementation of it here. For that, you can refer app source code for implementation details. You can that here weā€™ve implemented ViewModel and Activity which is now able to communicate with API.

Remember one thing! šŸ¤Ø

For accessing the deviceā€™s network information youā€™ll need to include below permission in your appā€™s Manifest.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Getting network Details šŸ“¶

Here the main thing is how to get Network details from the device. Here we have created a method getCurrentCellInfo() as below šŸ‘‡ which will help us to extract proper network details.

As you can see in the above code, for extracting network details, we need to check whether network type is GSM, CDMA or LTE.

Weā€™ll need to create a method getCellInfo() for every network type. For example, the below method is created for GSM type šŸ‘‡

The same method can be repeated for other network types.

Okay! By this, weā€™ve completed the main part of the applicationāœØ. As you can see, we havenā€™t used GPS or location service in this app.

Now just send this information along as data payload to the API and youā€™ll get a response which will include location details šŸ”„.

OkayšŸ˜ƒ. Now letā€™s run this appšŸš€. Youā€™ll see like thisā€¦ šŸ‘‡

App Preview (Cell Location Finder)

Yeah!šŸ˜ Our app is working as expected šŸš£. Thatā€™s all.

I hope you liked this article šŸ˜ƒ.

Thank you! šŸ˜ƒ

Sharing is caring!

This article was originally published here at https://blog.shreyaspatil.dev/

--

--

Google Developer Expert for Android, šŸ‘Øā€šŸ’»Engineer @ Paytm ā¤ļø Android & Kotlin | More info: https://shreyaspatil.dev