6

Sto cercando di ottenere i seguenti dati: stazioneAndroid: ottenere CellID e RSS per stazione base e neigboring Cells

  • Base: CellID e RSS (riconoscimento quale è la stazione base)
  • Per tutte le stazioni vicine: CellID e RSS

Ci sono varie API e sembra che dovrei usare diverse API telephonyManager e PhoneStateListener. Sono un bitbit confuso, perché penso che questo dovrebbe essere disponibile in un'unica interfaccia. Inoltre, penso che dovrebbe essere possibile eseguire il polling del CellID della Base Station corrente invece di dover ascoltare State Changes per determinare int, dal momento che le celle adiacenti sono anch'esse interrogate dal telephonyManager.

Puoi dirmi come posso ottenere i dati specificati sopra?

risposta

6

Nuova modalità skool: API 17 molto più bella e pulita, ma ancora troppo nuova.

List<CellInfo> cellInfos = (List<CellInfo>) this.telephonyManager.getAllCellInfo(); 

for(CellInfo cellInfo : cellInfos) 
{ 
    CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo; 

    CellIdentityGsm cellIdentity = cellInfoGsm.getCellIdentity(); 
    CellSignalStrengthGsm cellSignalStrengthGsm = cellInfoGsm.getCellSignalStrength(); 

    Log.d("cell", "registered: "+cellInfoGsm.isRegistered()); 
    Log.d("cell", cellIdentity.toString());   
    Log.d("cell", cellSignalStrengthGsm.toString()); 
} 

La vecchia API non fornisce una soluzione molto soddisfacente. Ma qui è il modo vecchia scuola:

this.telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
this.phoneStateListener = setupPhoneStateListener(); 
this.telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION); 
this.telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE); 
this.telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS); 

// This part is used to listen for properties of the neighboring cells 
List<NeighboringCellInfo> neighboringCellInfos = this.telephonyManager.getNeighboringCellInfo(); 
for(NeighboringCellInfo neighboringCellInfo : neighboringCellInfos) 
{ 
    neighboringCellInfo.getCid(); 
    neighboringCellInfo.getLac(); 
    neighboringCellInfo.getPsc(); 
    neighboringCellInfo.getNetworkType(); 
    neighboringCellInfo.getRssi(); 

    Log.d("cellp",neighboringCellInfo.toString()); 
} 

public PhoneStateListener setupPhoneStateListener() 
{ 
    return new PhoneStateListener() { 

     /** Callback invoked when device cell location changes. */ 
     @SuppressLint("NewApi") 
     public void onCellLocationChanged(CellLocation location) 
     { 
      GsmCellLocation gsmCellLocation = (GsmCellLocation) location; 
      gsmCellLocation.getCid(); 
      gsmCellLocation.getLac(); 
      gsmCellLocation.getPsc(); 

      Log.d("cellp", "registered: "+gsmCellLocation.toString()); 
     } 

     /** invoked when data connection state changes (only way to get the network type) */ 
     public void onDataConnectionStateChanged(int state, int networkType) 
     { 
      Log.d("cellp", "registered: "+networkType); 
     } 

     /** Callback invoked when network signal strengths changes. */ 
     public void onSignalStrengthsChanged(SignalStrength signalStrength) 
     { 
      Log.d("cellp", "registered: "+signalStrength.getGsmSignalStrength()); 
     } 

    }; 
} 
  • Non dimenticare di impostare tutte le autorizzazioni necessarie Il problema di questa soluzione è che non ho ricevuto alcuna vicina informazioni cella, eventhough ho configurato :

    usa-autorizzazione android: name = "android.permission.ACCESS_COARSE_UPDATES"

(notare che io sto usando un telefono cellulare Samsung e si tratta di un problema noto che i telefoni Samsung don 't supportano l'elenco delle celle adiacenti)

+1

Ottengo un 'android.telephony.CellInfoWcdma non può essere lanciato su android.telephony.CellInfoGsm' su' CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo; ' – msysmilu

+0

Il codice potrebbe non essere 100% lavorando, l'API era troppo nuova per essere eseguita sul mio telefono nel momento in cui ho scritto il codice. Si prega di inviare una nuova domanda e/o proporre una modifica se si dispone di una soluzione. – ndrizza

+0

è legale prendere questi dati? – heximal