I am developing android application using pjsua2.I am able to register,make calls and perform everything when there is internet connection but once the internet connection is lost and again connected to internet the connection is not established with the server. when I checked the logs it displayed Sip is not registered. even after internet connection is re-established.
Please some help to find what the mistake mght be ?
Thanks
You can use this piece of code
as described in this link
IpChangeParam changeParam = new IpChangeParam();
endpoint.handleIpChange(changeParam);
I found the same behavior when running in connection issues. I've ended registering to the android.net.conn.CONNECTIVITY_CHANGE to detect when internet status is changed and then using a method to detect the new status.Bellow is my implementation for the method:
public enum InternetStatus { WIFI, MOBILE, ROAMING, NO_INTERNET, UNKNOWN };
/**
* Get the internet status of the phone. The possible values are :
* +Not connected
* +Connected through WiFi
* +Connected to Mobile Carrier
* +Connected on Roaming
* #param context Context under which the app is running
* #return Returns the internet status as an enum value
*/
public static InternetStatus getInternetStatus(Context context)
{
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) { // connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
//Toast.makeText(context, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
return InternetStatus.WIFI;
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
// connected to the mobile provider's data plan
if(activeNetwork.isRoaming()) {
//Toast.makeText(context, activeNetwork.getTypeName() + " Roaming", Toast.LENGTH_SHORT).show(); // roaming
return InternetStatus.ROAMING;
}
else {
//Toast.makeText(context, activeNetwork.getTypeName() + " NOT Roaming", Toast.LENGTH_SHORT).show();
return InternetStatus.MOBILE;
}
}
} else {
// not connected to the internet
//Toast.makeText(context," NO Internet", Toast.LENGTH_SHORT).show();
return InternetStatus.NO_INTERNET;
}
return InternetStatus.UNKNOWN;
}
Once you get the internet connection you should access you SIP Account class and call
sipAccount.setRegistration(true);
This will trigger the library to send the registration message once again and everything should work properly.
Hope it helps.
Related
hello am developing a hotel app and I want my app to work only within the hotel.
To do this I am checking if the user is currently connected to the hotel's specific wifi before initiating the home activity.
By now I am able to check if the connection is wifi and its cool the only part am missing is how to check if the connected wifi is the one provided by the hotel.
Any idea here please?
most questions that am finding about this have not yet been answered and some answeres date back to 2013 which may not be very appropriate for me.
try this.
private void checkWifiConnection() {
ConnectivityManager connMgr = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netwifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (netwifi.getState() == NetworkInfo.State.CONNECTED) {
WifiInfo info = wifi.getConnectionInfo();
String ssid = info.getSSID();
if (ssid.equals("your SSID here")) {
Toast.makeText(this, "Connected to :" + ssid, Toast.LENGTH_SHORT).show();
} else {
//If not then do nothing.
}
}
}
While doing OTP verification via Twiiter Digits, if the user is on VOLTE only connection and WIFI is connected on phone then DIGITS does not send OTP SMS. How can this be solved?
The possible solution could be to check whether the user is on a Packet only (PS) carrier and if the WIFI is connected then show a Alert to the User to turn OFF WIFI.
You can use the following code for this:-
private void check_wifi_for_volte(){
TelephonyManager manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String carrierName = manager.getNetworkOperatorName();
Log.d(TAG,"carrierName:"+carrierName);
boolean isWifiActive =false;
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) { // connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
isWifiActive=true;
//Toast.makeText(this, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
// connected to the mobile provider's data plan
//Toast.makeText(this, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
}
} else {
// not connected to the internet
// can we show some toast not connected to internet
}
boolean isPSUser = carrierName.toLowerCase().contains(<PS-CARRIER>);
if(isPSUser && isWifiActive){
Toast.makeText(this, "If you are using PS Network. Turn OFF WIFI to get OTP SMS.", Toast.LENGTH_LONG).show();
}
}
I would like to know if mobile flag in setting is checked or not.
With this threads I can check if 3g is connected or not but i can not see if checkbox for enable it is enabled or not in setting
How to check if wifi/3g is enabled?
Google Android - how to figure out if 3g and 2g is turned on
Android: How to check whether 3G is enabled or not?
How can i check this?
This function is return true if internet connection is available else return false.
public boolean getConnection()
{
ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
boolean is3g = manager.getNetworkInfo(
ConnectivityManager.TYPE_MOBILE)
.isConnectedOrConnecting();
boolean isWifi = manager.getNetworkInfo(
ConnectivityManager.TYPE_WIFI)
.isConnectedOrConnecting();
Log.v("",is3g + " ConnectivityManager Test " + isWifi);
if (!is3g && !isWifi) {
Toast.makeText(getApplicationContext(),"Your internet connction is OFF",Toast.LENGTH_LONG).show();
return false;
} else
{
Toast.makeText(login.this, "Connected",Toast.LENGTH_LONG).show();
return true;
}
}
If this is not satisfy above answer then you can use try-catch. If you does not receive data that means your connection has problem.
I am creating an application to check the status of the mobile network.
I use NetworkInfo.State.CONNECTED to check the connectivity. It works fine on my emulator.
But when the user makes a call the Network info state is SUSPENDED(But the user is connected to the network) What does the suspended code indicate?
Also when I run the application on a device, the network state is given as DISCONNECTED. But the user is connected to the network and can recevice/give calls. The NetworkInfo.getReason() is given as datadisconnected in this case.
Can someone please help met o check the mobile connectivity to the network.
Thanks in advance.
public class NetWorkCheck{
ConnectivityManager connectivityManager;
NetworkInfo wifiInfo, mobileInfo, lanInfo;
public Boolean checkNow(Context con){
try{
connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(wifiInfo.isConnected() || mobileInfo.isConnected())
{
return true;
}
}
catch(Exception e){
System.out.println("CheckConnectivity Exception: " + e.getMessage());
}
return false;
}
}
I have a code to determine if there is a network connection or not :
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected())
{
// There is an internet connection
}
But if there is a network connection and no internet this is useless. I have to ping a website and wait for a response or timeout to determine the internet connection:
URL sourceUrl;
try {
sourceUrl = new URL("http://www.google.com");
URLConnection Connection = sourceUrl.openConnection();
Connection.setConnectTimeout(500);
Connection.connect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// no Internet
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// no Internet
}
But it is a slow detection. I should learn a good and fast way to detect it.
Thanks in advance.
Try following method to detect different type of connection:
private boolean haveNetworkConnection(Context context)
{
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) Your_Activity_Name.this.getSystemService(Context.CONNECTIVITY_SERVICE);
// or if function is out side of your Activity then you need context of your Activity
// and code will be as following
// (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo)
{
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
{
if (ni.isConnected())
{
haveConnectedWifi = true;
System.out.println("WIFI CONNECTION AVAILABLE");
} else
{
System.out.println("WIFI CONNECTION NOT AVAILABLE");
}
}
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
{
if (ni.isConnected())
{
haveConnectedMobile = true;
System.out.println("MOBILE INTERNET CONNECTION AVAILABLE");
} else
{
System.out.println("MOBILE INTERNET CONNECTION NOT AVAILABLE");
}
}
}
return haveConnectedWifi || haveConnectedMobile;
}
The problem with all such schemes is that 'the internet' does not exist as an entity. There is a reason why failed connection attempts are reported as 'unreachable' or 'cannot connect to server at blahblah'. Examples:
1) You have no signal. Are you connected to the internet? Will PING succeed? Can you connect to your target server?
2) You have a signal, but your provider data allowance has been exceeded. Are you connected to the internet? Will PING succeed? Can you connect to your target server?
3) Your provider connection is fine, but their backbone router is down. Are you connected to the internet? Will PING succeed? Can you connect to your target server?
4) Your provider connection is fine, their backbone router is up but the fibre connection to country X where the server is has been interrupted by some drunken Captain and his ship's anchor. Are you connected to the internet? Will PING succeed? Can you connect to your target server?
5) All the links to the target country are up but Fred, with his ditch-digging JCB, has cut the power cable to the server farm. One of Fred's other jobs is to service the backup generator:( Are you connected to the internet? Will PING succeed? Can you connect to your target server?
6) All the hardware is up, but the server code was written by Fred before he was demoted to ditch-digger for incompetence and has now crashed, again. Are you connected to the internet? Will PING succeed? Can you connect to your target server?
7) Fred has had a day off, but his replacement, competent server admin has blocked off ICMP ping in the routers to prevent ping DOS attacks. Are you connected to the internet? Will PING succeed? Can you connect to your target server?
So, the ony way to be sure is to attempt to connect to the target server and see what happens.
You can surely detect some negative cases more quickly - surely if there is no signal, you cannot get a connection:) Past that, you should just try to connect. Tell the user what is going on, use a timeout and supply the user with a 'Cancel' button. That's about the best you can do.
How about this?
Make sure you have an active WiFi connection, now Use WifiManager.getConnectionInfo() which returns dynamic information about the current Wi-Fi connection, WifiInfo , you can get WifiInfo.getLinkSpeed(), which gives you the current link speed and check that against some minimum value.