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.htmlIt 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 CelI
IdentityCdma it's getBase
stationId(), while with
CdmaCellLocation it's getBase
StationId() - this is sooooo confusing.
And now my head hurts.
Comments welcome!