Android cell id

I would like to get cid from my mobile phone, I am currently using this code for this:

GsmCellLocation gsmLocation = (GsmCellLocation)telephonyManager.getCellLocation();
                 int Cid = gsmLocation.getCid();
                 Log.e("#############","current cid is: "+Cid);
                 int lac = gsmLocation.getLac();
                 Log.e("#############","current lac is: "+lac);

this returns something like 301 or 6061.

I was looking through some code examples and found this:

/**
 * Seems that cid and lac shall be in hex. Cid should be padded with zero's
 * to 8 numbers if UMTS (3G) cell, otherwise to 4 numbers. Mcc padded to 3
 * numbers. Mnc padded to 2 numbers.
 */
try {
 // Update the current location
 updateLocation(getPaddedHex(cid, cellPadding), getPaddedHex(lac, 4),
   getPaddedInt(mnc, 2), getPaddedInt(mcc, 3));
 strResult = "Position updated!";
} catch (IOException e) {
 strResult = "Error!\n" + e.getMessage();
}

// Show an info Toast with the results of the updateLocation
// call.
Toast t = Toast.makeText(getApplicationContext(), strResult,
  Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
t.show();}});
}

/**
 * Convert an int to an hex String and pad with 0 up to minLen.
 */
String getPaddedHex(int nr, int minLen) {
  String str = Integer.toHexString(nr);
  if (str != null) {
   while (str.length() < minLen) {
    str = "0" + str;
   }
  }
  return str;
 }

 /**
  * Convert an int to String and pad with 0 up to minLen.
  */
 String getPaddedInt(int nr, int minLen) {
  String str = Integer.toString(nr);
  if (str != null) {
   while (str.length() < minLen) {
    str = "0" + str;
   }
  }
  return str;
 }

Is the number from GsmCellLocation correct or do I need to change the results as shown in the example? what is the difference? I know from the documentation that cid can be -1 (unknown) or 0xffff the maximum value, 0xffff for 2g or 3g networks?

+3
source share
1 answer

This code does not really change the numbers. It displays them differently as hexadecimal or with lots of zeros on the left.

+2
source

Source: https://habr.com/ru/post/1768438/


All Articles