Jun 7, 2011

Get neighboring cell information, getNeighboringCellInfo().

TelephonyManager.getNeighboringCellInfo() return a List of neighboring cell information(NeighboringCellInfo), including Received Signal Strength and Cell ID location.

Get neighboring cell information, getNeighboringCellInfo().

package com.AndroidTelephonyManager;

import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidTelephonyManager extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textGsmCellLocation = (TextView)findViewById(R.id.gsmcelllocation);
TextView textMCC = (TextView)findViewById(R.id.mcc);
TextView textMNC = (TextView)findViewById(R.id.mnc);
TextView textCID = (TextView)findViewById(R.id.cid);


//retrieve a reference to an instance of TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();

String networkOperator = telephonyManager.getNetworkOperator();
String mcc = networkOperator.substring(0, 3);
String mnc = networkOperator.substring(3);
textMCC.setText("mcc: " + mcc);
textMNC.setText("mnc: " + mnc);

int cid = cellLocation.getCid();
int lac = cellLocation.getLac();
textGsmCellLocation.setText(cellLocation.toString());
textCID.setText("gsm cell id: " + String.valueOf(cid));

TextView Neighboring = (TextView)findViewById(R.id.neighboring);
List<NeighboringCellInfo> NeighboringList = telephonyManager.getNeighboringCellInfo();

String stringNeighboring = "Neighboring List- Lac : Cid : RSSI\n";
for(int i=0; i < NeighboringList.size(); i++){

String dBm;
int rssi = NeighboringList.get(i).getRssi();
if(rssi == NeighboringCellInfo.UNKNOWN_RSSI){
dBm = "Unknown RSSI";
}else{
dBm = String.valueOf(-113 + 2 * rssi) + " dBm";
}

stringNeighboring = stringNeighboring
+ String.valueOf(NeighboringList.get(i).getLac()) +" : "
+ String.valueOf(NeighboringList.get(i).getCid()) +" : "
+ dBm +"\n";
}

Neighboring.setText(stringNeighboring);
}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/gsmcelllocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/mcc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/mnc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/cid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/neighboring"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

18 comments:

  1. how ?
    it returns null on 2G and 3G :(

    ReplyDelete
  2. Venus,

    Which function return null? getCellLocation()? according to Android document, it return null if current location is not available.

    Requires Permission: ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION.

    ReplyDelete
  3. tnx for the reply
    i really appreciate

    ReplyDelete
  4. does it need ACCESS_COARSE_UPDATES permission ?

    ReplyDelete
  5. the value of the local variable lac is not used:
    int lac = cellLocation.getLac();
    can u give me a full version of manifest ? please
    it is really important to me.
    On emulator , is there any way to test this code ?

    ReplyDelete
  6. when i am using this code on api 1.6
    there is error , that says :
    The method getLac() is undefined for the type NeighboringCellInfo.
    String.valueOf(NeighboringList.get(i).getLac())

    ReplyDelete
  7. when i am using this code on api 2.1
    the value of the local variable lac is not used:
    int lac = cellLocation.getLac();
    can u give me a full version of manifest ? please
    it is really important to me.
    On emulator , is there any way to test this code ?

    ReplyDelete
  8. this code is not run on the emulator dude :) because emulator is not receive from telephonymanager library :)
    telephonymanager library run only phone :)

    ReplyDelete
  9. excuse me, would you tell me which phone(brand/model) you used when you test your program? thanks :)

    by the way, will the mobile devices affect the test result?

    ReplyDelete
    Replies
    1. hello 欠債貓,

      I use Nexus One. I THINK the function affect by service provider more than device.

      Please note that Permission of ACCESS_COARSE_UPDATES is needed.

      Delete
    2. Thank for your reply, we finally did it!

      As you say, the permission ACCESS_COARSE_UPDATES is the point.

      Thank for your help again. :)

      Delete
  10. Well, I tried this program in my phone HTC desire S but NeighboringList.size() always returning 0. Please help me to find out.

    ReplyDelete
    Replies
    1. hello Pijush,

      getNeighboringCellInfo() return null means info unavailable.

      Permission of ACCESS_COARSE_UPDATES is needed for this feature.

      Delete
  11. Hi, I'm a serious noob with this. I'm currently looking for possibilities to receive neighboring cell info, so it would be pretty AWESOME to get it through an Android app.

    - What is all the data able of being accessed by this command?
    - will data be received on neighboring cells for both 2G and 3G technologies?
    - will this command/app work on a Samsung Galaxy SII?

    ReplyDelete
  12. I'm using Samsung Galaxy SII. And NeighboringList returns null. I wrote ACCESS_COARSE_UPDATES to manifest.xml file. Will I write another permissions to manifest.xml file?

    Thanks.

    ReplyDelete
  13. When I run this code it returned me "Source not found." I think the problem in this line "GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();", and I dont know why.....

    ReplyDelete

Infolinks In Text Ads