0

I want get LTE signal strength on PhoneStateListener.onSignalStrengthChanged but i don't know what do it. it will be appreciate to help me.

procedure TPhoneStateListener.onSignalStrengthsChanged(signalStrength:JSignalStrength);
begin 
          //how can get LTE signal strength 
end;  

Thanks.

mjn
  • 36,362
  • 28
  • 176
  • 378
saeed yadgari
  • 318
  • 2
  • 8
  • Possible duplicate of https://stackoverflow.com/questions/5545026/how-to-get-lte-signal-strength-in-android – mjn42 Mar 15 '19 at 13:39
  • See also https://stackoverflow.com/questions/32531176/get-rsrp-from-cellsignalstrengthlte-for-android-app-api-17 – mjn42 Mar 15 '19 at 13:42
  • @nolaspeaker This is not the same question. See mjn42's links. – Dave Nottage Mar 15 '19 at 20:25
  • thanks to all. I want it in delphi, not in java. – saeed yadgari Mar 16 '19 at 03:45
  • @nolaspeaker thanks to you for answer in https://stackoverflow.com/questions/55057630, but i want only give LTE signal strength at one of those procedure, in the another hand i want this answer https://stackoverflow.com/questions/5545026/how-to-get-lte-signal-strength-in-android in delphi. – saeed yadgari Mar 21 '19 at 04:56

2 Answers2

2

The PhoneStateListener passes an instance of SignalStrength to the onSignalStrengthsChanged method. However, SignalStrength does not contain a LTE signal strength property.

You may use this code to access the LTE signal strength by reading TelephonyManager.getAllCellInfo():

  private void printLteSignalStrengths() {

      List<CellInfo> cellInfoList = telephonyManager.getAllCellInfo();
      for (CellInfo cellInfo : cellInfoList) {
          if (cellInfo instanceof CellInfoLte) {
              // cast to CellInfoLte and call all the CellInfoLte methods you need
              CellInfoLte ci = (CellInfoLte) cellInfo;
              System.out.println("LTE signal strength: " + ci.getCellSignalStrength().getDbm());
          }
      }
  }

For Delphi, replace curly braces with begin/and end and use the usual wrapper classes.

Note that the code requires the ACCESS_COARSE_LOCATION permission.

mjn
  • 36,362
  • 28
  • 176
  • 378
  • Just one concern, is the onSignalStrengthsChanged really get called when it LTE signal strength get changed? Because I just see the LTE signal strength is not changed for a few calls... – Yeung Apr 12 '19 at 07:45
  • 1
    @Yeung according to the doc, the Callback is invoked when network signal strengths changes. Maybe some other LTE data values changed. To verify this, log all field values and watch which one changes. – mjn Apr 13 '19 at 08:56
1

You will note that you cannot obtain the Cellinfo's signal strength or the like in the Phonestatelistener's onSignalstrengthschanged. It's not available there! Instead you can ask for it as often as you like. You can use a timer or timed loop on a separate thread to achieve that. While I have LTE on my phone, I never got any an android.telephony.CellInfoLte object in the Cells list, so I opted to get the android.telephony.CellInfoWcdma instead. Code for both is shown.

procedure TfrmAppMain.GetCellLevel: Integer;
var
  obj: JObject;
  Cells: JList;
  i: Integer;
  Cell: JCellInfo;
  CellInfoLte: JCellInfoLte;
  CellInfoWcdma: JCellInfoWcdma;
  cname: String;
begin
  Result := -1;
  obj := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.TELEPHONY_SERVICE);
  if obj <> nil then
  begin
    TelephonyManager := TJTelephonyManager.Wrap(obj);
    Cells := TelephonyManager.getAllCellInfo;
    // iter := Cells.iterator;
    i := 0; 
    while (i < cells.size) do
    begin
      obj := Cells.get(i);
      Cell := TJCellInfo.Wrap(obj);
      if Cell.isRegistered then
      begin
        cname := JStringToString(Cell.getClass.getName);
        if cname = 'android.telephony.CellInfoLte' then
        begin
          CellInfoLte := TJCellInfoLte.Wrap(Cell);
          Result := CellInfoLte.getCellSignalStrength.getLevel; // 0..4
        end
        else if cname = 'android.telephony.CellInfoWcdma' then
        begin
          CellInfoWcdma := TJCellInfoWcdma.Wrap(Cell);
          Result := CellInfoWcdma.getCellSignalStrength.getLevel; // 0..4
        end;
      end;
      Inc(i);
    end;
  end;
end;
Freddie Bell
  • 2,186
  • 24
  • 43
  • The first [CellInfo](https://developer.android.com/reference/android/telephony/CellInfo.html) might not be the one which is currently used by the phone. You might check the isRegistered() flag, it is true if this cell is being used or would be used for network signaling – mjn Mar 22 '19 at 07:52