Signal Strength on android saved as a variable - android

I'm trying to creat an app that would save the current signal strength. So far I've seen many examples that are all extending PhoneStateListener, but all of them use a Toast to display this information, like in this example:
http://www.firstdroid.com/2010/05/12/get-provider-gsm-signal-strength/
I was wondering a couple of things:
a) Do I always have to use the PhoneStateListener AND override the onSignalStrengthsChanged(SignalStrength signalStrength) ?
b) How can I access the value signalStrength.getGsmSignalStrength() from outside the PhoneStateListener class?
Thanks in advance

U can try with it. I cant say its the final solution.If u want to access certain data from other activity, then try it with shared preference.Shared preference value is visible from other activities

I might be late to answer your question, but if you are still looking for the answer, here it is:
a) yes you have to use the PhoneStateListener and override the onSignalStrengthsChanged as, in my knowledge that is the only way to get current cells' signal strength for GSM. The listener is only called in big signal strength changes, so you yourself cannot control the listener. the listener will automatically update or make a toast when it is called. So, it is better to declare the listener and ask it to listen at onCreate().
b) For accessing the the RSSI value from outside the Listener is not really difficult, just store the value in a variable and make a method like getRSSI(), which will return you the value when its called. The example is given below:
public class GsmRSSI extends Activity{
MyPhoneStateListener MyListener;
TelephonyManager Tel;
ArrayList<String> signalStrength = new ArrayList<String>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyListener = new MyPhoneStateListener();
Tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
timer.schedule(new TimerTask() {
public void run() {
String rssi = MyListener.getStrength();
if(!rssi.equals(""))
signalStrength.add(rssi);
}
}, 0, 5000);//it will add the rssi value after every 5000ms
}
private class MyPhoneStateListener extends PhoneStateListener {
String gsmStrength = "";
#Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
gsmStrength = String.valueOf(signalStrength.getGsmSignalStrength()* 2 - 113);
}
public String getStrength() {
return gsmStrength;
}
}
}
This should do the work for you. But at the begining you might not get any rssi value for a little while as the listener is only called when there is a significant change in the rssi.

Related

Telephony manager in Application class

When I try to use telephony manager to retrieve the phone number from an Activity class, I am able to do it successfully. But I will be using the phone number in multiple places of the app, therefore I shifted the phone number to be a static field in my application class.
public class FourApplication extends Application {
static String phonenumber ;
#Override
public void onCreate() {
super.onCreate();
ParseObject.registerSubclass(Post.class);
// Add your initialization code here
Parse.initialize(this, "**********", "*********");
ParseACL defaultACL = new ParseACL();
// If you would like all objects to be private by default, remove this
// line.
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
phonenumber = getPhoneNumber();
}
public String getPhoneNumber()
{
TelephonyManager tMgr =(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number(); ;
Log.i("mPhoneNumber : ", mPhoneNumber);
return mPhoneNumber;
}
}
What is the mistake I am making here? I read through a few Context related questions and threads, Not able to figure out what's going wrong in my code as I am noob here.
Edit : My question is, When I move the telephony manager part to the application class, it doesn't return a phone number. Why is that?
#55597
Please use the following piece of code.You got your problem
TelephonyManager tMgr =(TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
You are not passing the correct context of Activity for TelephonyManager, So that its return null.

Two Listeners in Service (android)

Firstly,sorry my English.And i am sorry about a large text.I just tried to describe in details.
A bit of my story:
I am new in Android.I have some programming background(some Python,
some C++).I read a book about Java, at First.Then I read a book
about Android(just a few pages =) )
So,the goal of my program:
Run in background(here i used Service) PhoneCallListener. When there is an incoming call, i use SensorEventListener, and do some magical things.
How I realized it:
...
public class mbackground extends Service{
public int onStartCommand(Intent intent, int flags, int startId){
PhoneStateListener phoneStateListener = new PhoneStateListener();
TelephonyManager telephonyManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);
then i try to use here "public class phoneCallListener extends PhoneStateListener{"
And some methods of PhoneStateListener
public void onCallStateChanged(int state, String incomingNumber){ do some things }
Here i have a problem: for some reason, the compiler does not like public in definition class phoneCallListener...And then,how i can use Sensor Listener in one of the methods of PhoneStateListener?
I tried to find information in Google. Honestly.
I do not know how to use Listeners correctly in that situation.I found some similar questions, but there Listeners used without class extends and some abstract methods.
Thanks for reading!
In onStartCommand
PhoneStateListener phoneStateListener = new PhoneStateListener();
should be
phoneCallListener phoneStateListener = new phoneCallListener();

Android - Signal Strength in Level, DBM, and ASU

I am currently writing an application for a client who wants to gather data regarding the signal strength at set intervals.
Currently I am using this code:
private static class MyPhoneStateListener extends PhoneStateListener
{
#Override
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
super.onSignalStrengthsChanged(signalStrength);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
InfoStore.setSignal(String.valueOf(signalStrength.getGsmSignalStrength()));
}
};
This works fine, however the client wants the signal strength in both level (I guess how many bars?), DBM, and ASU.
Anyone have any clue how to read the signal strengths using those different forms?
As mentioned by Charles Ma and Kevin Krumwiede the relevant Android methods are hidden (probably for good reason), however it is still possible to get the values by reflection. Thus one solution to original question:
private class MyPhoneStateListener extends PhoneStateListener
{
public static final int INVALID = Integer.MAX_VALUE;
public int signalStrengthDbm = INVALID;
public int signalStrengthAsuLevel = INVALID;
#Override
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
signalStrengthDbm = getSignalStrengthByName(signalStrength, "getDbm");
signalStrengthAsuLevel = getSignalStrengthByName(signalStrength, "getAsuLevel");
}
private int getSignalStrengthByName(SignalStrength signalStrength, String methodName)
{
try
{
Class classFromName = Class.forName(SignalStrength.class.getName());
java.lang.reflect.Method method = classFromName.getDeclaredMethod(methodName);
Object object = method.invoke(signalStrength);
return (int)object;
}
catch (Exception ex)
{
return INVALID;
}
}
}
In android 4.x the SignalStrength class has getAsuLevel, getDbm, as well as getLevel (bars) methods.
If you need this to work for older android versions, have a look at the source code and you can copy the implementations of those methods over.
http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/4.1.2_r1/android/telephony/SignalStrength.java/
The only thing that you can't get is the Lte measurements in older android versions, but you can probably use java reflection to see if the getLte* methods exist and call it.
Calculate dBm by
int SignalStrength_ASU = signalStrength.getGsmSignalStrength();
int SignalStrength_dBm = (2 * SignalStrength_ASU) - 113; // -> dBm

Stop Listener after first update

I want to know the signal strength of my current cell tower on Android. After some research I found, that I should use a PhoneStateListener which listen for an update to get the value (strange way to do that IMHO).
So I want to get the signal as soon as I get it and stop the listener after. This is the code I use :
// object containing the information about the cell
MyGSMCell myGSMCell = new MyGSMCell(cid,lac);
// object listening for the signal strength
new GetGsmSignalStrength(myGSMCell, context);
...
public class GetGsmSignalStrength {
private MyGSMCell callback;
private TelephonyManager telMan;
private MyPhoneStateListener myListener;
public GetGsmSignalStrength(MyGSMCell callback, Context context) {
this.callback = callback;
this.myListener = new MyPhoneStateListener();
this.telMan = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
this.telMan.listen(this.myListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
private class MyPhoneStateListener extends PhoneStateListener
{
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
// get the strength
super.onSignalStrengthsChanged(signalStrength);
// return it to the MyGSMCell object to set the value
callback.setStrength(signalStrength.getGsmSignalStrength());
}
}
}
I would like to stop the listener as soon as I send a setStrength(value)
Any idea ?
Thank you
From the docs:
To unregister a listener, pass the listener object and set the events
argument to LISTEN_NONE (0).
So, add this to your listener:
telMan.listen(myListener, PhoneStateListener.LISTEN_NONE);

How to get valid Bit Error Rate or Rx Qual

I am trying to get bit Error rate by using getGthe following coding but i am always get -1 only. Do you anybody have idea to get valid Bit Error Rate. please help to get correct value.
The code are following.
public class GetGsmSignalStrengthActivity extends Activity {
TelephonyManager Tel;
MyPhoneStateListener MyListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Tel = ( TelephonyManager )getSystemService(Context.TELEPHONY_SERVICE);
Tel.listen(MyListener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
private class MyPhoneStateListener extends PhoneStateListener
{
#Override
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
super.onSignalStrengthsChanged(signalStrength);
TextView dummy1 = (TextView)findViewById(R.id.textView5);
String x,m ="";
x=String.valueOf(signalStrength.getGsmBitErrorRate());
dummy4.setText("Bit Error : " +x);
}
}
}
Read this bug report comment #4. You see that it is optional and therefore possible. It will return -1 if it isn't provided by the modem manufacturer.
Quote:
The problem is that AT+CSQ in TS 27.007 section 8.5 is listed as optional. Therefore the modem manufacturer has the choice whether or not to provide both the signal and bit error rate using standard interfaces. So when you see -1 that means the modem people haven't implemented it.

Categories

Resources