About Java, Cell IDs, LAC, MCC, MNC, GSM, CDMA, LTE, WCDMA and all that funny stuff - How to brew your own

1,001 views
Skip to first unread message

Mad Mooney

unread,
Jan 21, 2015, 6:38:01 PM1/21/15
to tas...@googlegroups.com
Welcome to the Android TelephonyManager :-)

If you're reading this, you probably have already tried to
(TelephonyManager) mytelephonyobj = CONTEXT.getSystemService( phone )

And you probably know what phone you have, but let's check anyway:
%myphonetype = mytelephonyobj.getPhoneType()
That gives 1 for GSM, 2 for CDMA, 3 for a SIP phone and 0 is not a phone.

Now, usually, the first step one tries is
(List) allcellinfo = mytelephony.getAllCellInfo()
because you read about that one at http://developer.android.com/reference/android/telephony/TelephonyManager.html
It fails on my SIM-enabled tablet, and also on my phone. Both android 4.2.2. Hm. It should be implemented with API 17, but isn't always, it seems.
I'll try it on an emulator when I find the time. Anyway, the docs also say that if it fails, one should try
getCellLocation()
Funny thing is, this should give you a CellLocation Object, but... there is more that one of those.

For GSM phones (including UMTS and stuff) you'd write
(GsmCellLocation) mycelllocobj = mytelephonyobj.getCellLocation()

For CDMA phones you need to write
(CdmaCellLocation) mycelllocobj = mytelephonyobj.getCellLocation()

both have different methods:
GSM, for example has getCid(), getLac(), getPsc() (which can be different things, stay away)...
%MyCellID = mycelllocobj.getCid() gives a perfectly usable global Tasker variable.
 
CDMA has getBaseStationId(), getBaseStationLatitude(), getBaseStationLongtitude(), getNetworkId(), getSystemId()...
Location by CDMA. Hm. I don't know how far that's implemented. I'm on GSM here. What I found, though, is that even my GSM/UMTS phone has a CdmaCellLocation Object, it's missing a function, though:
convertQuartSecToDecDegrees(int) - this one should convert the values thrown by getBaseStationLatitude() and getBaseStationLongtitude() to degrees (they are in units of 0.25 seconds which is so strange that someone must really have had a gooood reason to do it that way).


A different way to get Cell Information for GSM and UMTS is via
(List) neighborlist = mytelephony.getNeighboringCellInfo()
%neighborlistsize = neighborlist.size()
...where each Object in that List is a (NeighboringCellInfo) Object. Let's grab the first one instead of iterating through the list (example for that further below)...
(NeighboringCellInfo) myneighbor = neighborlist( 0 )
...where this nice neighbor has no milk or eggs for us, but can
%networktype = neighbor.getNetworkType()  - everything from GPRS, EDGE, UMTS, HSPA, HSDPA, HSUPA and whatnot in clear text. Isn't he nice? But he's Old School, no LTE. And he doesn't seem to speak CDMA, which already resulted in protests, but to no avail.
This neighbor is also kinda unreliable, since (at least here) his
%neighborcid = neighbor.getCid() returns -1


Anyway, before I forget:
I get my MCC and MNC (see http://en.wikipedia.org/wiki/Mobile_country_code ) simply from the TelephonyManager:
%MyNetworkOp = mytelephony.getNetworkOperator()
then I grab the left 3 digits for the MCC:
Variable / Variable Section / Name %MyNetworkOp / From 1 / To 3 / Store Result In %MyMCC
and the other 2 or 3 digits by first checking the length:
Variable / Test Variable / Type Length / Data %MyNetworkOp / Store Result In %myoplength
Variable / Variable Section / Name %MyNetworkOp / From 4 / To %myoplength - 3 / Store Result In %MyMNC
Hey, this one does Math without asking! :-)


So, what about LTE? WCDMA?
Once you have one of those, your
(List) allcellinfo = mytelephony.getAllCellInfo()
should work and return a List, which you could check with
%listcount = allcellinfo.size()
I don't have one of those, so I'm flying blind here, but bear with me.
Now, depending on what network radios your phone has, and what kind of towers you have around you, you can
%loopmax = %listcount - 1 (Do Maths!)
For Variable %myloop / Items 0:%loopmax
   (Cellinfo) somecellinfoobj = allcellinfo.get( %myloop )
   ...do some more stuff
End For

or simply grab the first of that list:
(Cellinfo) somecellinfoobj = allcellinfo.get( 0 )
which should give you the first object out of that allcellinfo List, just for testing. This, in turn, isn't just a CellInfo Object:
There are CellInfoGsm, CellInfoCdma, CellInfoLte and CellinfoWcdma.

CellInfo has a method toString() so somebody please check what this returns on LTE or CDMA. There should be a way to find out what kind of CellInfo Object (CDMA, LTE etc) it really is.
Outside tasker, in raw code, you'd check
if (somecellinfoobj instanceof CellInfoLte) {
//cast that baby over ...
} else.....
From there, you grab that object and
for GSM:      (CellIdentityGsm) mygsmidentityobj = somecellinfoobj.getCellIdentity()
and from there you go crayzy with %mylac = mygsmidentityobj.getLac() ....getCid(), getMcc(), getMnc() and all those,
for CDMA:    (CellIdentityCdma) mycdmaidentityobj = somecellinfoobj.getCellIdentity()
with %mybaseid = mycdmaidentityobj.getBasestationId() ....getLatitude(), getLongtitude(), getNetworkId() ....,
for LTE:        (CellIdentityLte) mylteidentityobj = somecellinfoobj.getCellIdentity()
with %myCid = mylteidentityobj.getCi() ....getCid(), getMcc(), getMnc(),
for WCDMA  (CellIdentityWcdma) mycdmaidentityobj = somecellinfoobj.getCellIdentity()
where somebody has to check, because http://developer.android.com/reference/android/telephony/CellIdentityWcdma.html is talking about UMTS.....
D*mn.

Note that per documentation it says here, that for CelIIdentityCdma it's getBasestationId(), while with CdmaCellLocation it's getBaseStationId() - this is sooooo confusing.

And now my head hurts.

Comments welcome!

Mad Mooney

unread,
Jan 21, 2015, 7:01:43 PM1/21/15
to tas...@googlegroups.com
Aaaand I forgot:
Did you know that there are Short and Long Cell IDs for UMTS?
Tasker usually gives us somehing like
GSM:43210.12345678
GSM:43210.12355678
GSM:43210.12365678 (that's an example for 3 antennas on one tower)
for a Cell ID. Perfectly fine.
But if you go on websites that show towers on a map, you sometimes get IDs that you have never seen before.
If that's the case, they are probably using the "Short": Cell ID.
So if you have a "Long" Cell ID, and don't find it on the map, go grab yourself some Bin/Dec/Hex Calculator (or Tasker) and (example)
Convert 12355678 dec to bin, gets you 1011 1100 1000 1000 0101 1110
Now grab the 16 bits on the right side (lower, they say), 1000 1000 0101 1110
Convert those bin to dec, that gets you 34910, which, at first glance, makes it look like a good old 90s GSM basic cell.
Check if you find that one. If so, Baaaad. Close that browser tab. And if it was an app, uninstall and refund. Oh. Probably too late. Sorry.

Pent

unread,
Jan 22, 2015, 2:09:27 AM1/22/15
to tas...@googlegroups.com
Good stuff.

Have a rest now :-)

Pent

Wietse van Buitenen

unread,
Jan 22, 2015, 7:14:43 AM1/22/15
to tas...@googlegroups.com
While I don't have a specific use for it at the moment, this is some interesting stuff :)
Thanks for taking the time to write it down!

anj...@getfastah.com

unread,
Nov 3, 2017, 6:45:43 AM11/3/17
to Tasker
Hi Mad Mooney and rest of the group,

I just joined this group! I'm trying to figure our which ID values to use from Android CellInfo/Telephony manager.

I'm trying to understand and model network variation at the base station level. For LTE, I'm using eNB, which I understand I can calculate from getCI( ) in CellIdentityLte
by converting to hexadecimal, taking the first 6 digits and converting back.

I'd like to know what are the corresponding nodes in GSM and WCDMA. I'm thinking base station id and NodeB respectively.

What method give the appropriate base station id for gsm and NodeB values for WCDMA in the Android Telephony manager?

GSM - do I use getBsic( ) or getCid( ) values? I'm leaning towards getCid as it seems to return the right number of bits, but I'm not confident. 

WCDMA - For NodeB, do I have to use a similar hex conversion as in LTE with getCID( ) values?

Thanks in advance for any suggestions!

Regards,
Anjali
Reply all
Reply to author
Forward
0 new messages