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.
}
}
}
Related
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 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.
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 would like to know if the mobile network is enabled or disabled.
My application is designed to help the user when he receives a phone call, and to do this I need Internet access. Thus, I would like to display an information box when the user access the application for the first time if Wi-Fi has a sleep policy and Mobile network is disabled. (I need Internet within milliseconds after the phone start ringing).
I found Settings.System.WIFI_SLEEP_POLICY, but I can't find any information on how to check if mobile network is disabled (when Wi-Fi is on and working).
Any help would be appreciated!
Edit:
The problem is that I want to know if mobile network is turned of by the user (while the phone could have WiFi access at the time).
I finally found a solution. Apparently not all phones have this option:
Home > Menu > Settings > Wireless & networks > Mobile network (checkbox)
However, for those who do, this method will work:
/**
* #return null if unconfirmed
*/
public Boolean isMobileDataEnabled(){
Object connectivityService = getSystemService(CONNECTIVITY_SERVICE);
ConnectivityManager cm = (ConnectivityManager) connectivityService;
try {
Class<?> c = Class.forName(cm.getClass().getName());
Method m = c.getDeclaredMethod("getMobileDataEnabled");
m.setAccessible(true);
return (Boolean)m.invoke(cm);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
apparently there is an alternative, more clean solution then the Reflection approach:
final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo = cm.getActiveNetworkInfo();
int type = networkInfo.getType();
String typeName = networkInfo.getTypeName();
boolean connected = networkInfo.isConnected()
networkInfo.getType() will return '0' when connected to mobile network
or '1' when connected trough WIFI. networkInfo.getTypeName() will return
the strings "mobile" or "WIFI". and networkInfo.isConnected() will tell you whether or not you have an active connection.
UPDATE FOR ANDROID 5.0+ (API 21+)
Calling getMobileDataEnabled via the reflection leads to NoSuchMethodException on Android 5.0+ on some devices. So in addition to accepted answer you may wanna do second check if NoSuchMethodException is thrown.
...
catch(NoSuchMethodException e)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
isDataEnabled = Settings.Global.getInt(context.getContentResolver(), "mobile_data", 0) == 1;
}
}
you can use below code this is working for all API versions:
ConnectivityManager cm =
(ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
if(isConnected)
{
if(activeNetwork.getType()==ConnectivityManager.TYPE_MOBILE)
return true;
else
return false;
}
else
return false;
PackageManager pm = context.getPackageManager();
boolean hasTelephony = pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
i want know the current internet connection on device??
in android i found the two property to know network type like..
ConnectivityManager.TYPE_WIFI
ConnectivityManager.TYPE_MOBILE
but how to know 3G network??
pls help me out
Thanks in Advance!
now i can know 3 type of network as follow..........
ConnectivityManager connec = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo info = connec.getActiveNetworkInfo();
int netSubType = info.getSubtype();e
if (wifi.isConnected())
{
wifi is connected
}
else if (mobile.isConnected())
{
if(netSubType == TelephonyManager.NETWORK_TYPE_UMTS)
{
3G is connected
}
else
{
GPRS is connected
}
}
try to get subType() with this snippet:
NetworkInfo info = mConnectivity.getActiveNetworkInfo();
int netSubType = info.getSubtype();
then if netSubType is TelephonyManager.NETWORK_TYPE_UMTS, then its a 3G network
Updated: What's 'info' here
As far as I have used 3G is coming under ConnectivityManager.TYPE_MOBILE only.
If you are using emulator then you can press F8 to connect and disconnect 3G.
It also disconnects GPRS. for both ConnectivityManager.TYPE_MOBILE is used.