saeed yadgari
saeed yadgari

Reputation: 328

how can get LTE signal strength at onsignalStrengthsCanged in delphi firemonkey

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.

Upvotes: 0

Views: 813

Answers (2)

Freddie Bell
Freddie Bell

Reputation: 2287

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;

Upvotes: 1

mjn
mjn

Reputation: 36664

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.

Upvotes: 2

Related Questions