How to use wifi as 3G/2G on Android? - android

There are some code on Android:
if(netType == NetType.3G ||netType == NetType.2G)
{
//do something
}
So I need use 3G/2G to test this code. But for security reason . I can only reach this if statement when I using the specific WIFI.
So is there any way to use WIFI and let code looks it as 3G/2G?

You can use the android emulator. With the emulator you can simulate various network connections.

Related

is it possible to recognize wifi network inside app with xamarin

I'm just wondering if it's possible for an app to recognize what network you are connected to. I'm making and app where you have to be connected to a specific network before the app lets you use it's functions, but I'm wondering if that is even possible, I am using xamarin but I can program with androidand a little bit ofswift, so I also want to know if it's possible forxamarinto do this, if it's possible with android studio andxcode. I am usingxamarin.forms` by the way.
This used to be disabled for security reasons but after iOS 4.0 Apple enabled it.
Although this question is for Xcode, the answer can be applied in Xamarin.
This is a sample application built with the feature in question, although it is for Xamarin.Mac
Use function CNCopyCurrentNetworkInfo.
This is possible but you will have to do native implementations to access the specific platform apis. For Android you will need to use WifiManager (https://developer.android.com/reference/android/net/wifi/WifiManager) and for iOS you could possibly use NEHotspotConfiguration (https://developer.apple.com/documentation/networkextension/nehotspotconfiguration).
I have used WifiManager for Android to connect to a specific Wifi network programmatically.
You can use "CrossConnectivity" plugin in Xamarin by adding the package "xam.plugin.connectivity". Below is the code to check connectivity. From the connectionType property you can detect to which network the device is connected and perform operations accordingly. `
CrossConnectivity.Current.ConnectivityTypeChanged += (sender, e) =>
{
var wifi = Plugin.Connectivity.Abstractions.ConnectionType.WiFi;
var cellular = Plugin.Connectivity.Abstractions.ConnectionType.Cellular;
var connectionTypes = CrossConnectivity.Current.ConnectionTypes;
if (connectionTypes.Contains(cellular))
{
//Do operations with cellular
}
else if (connectionTypes.Contains(wifi))
{
//Do operations with wifi
}
};`

How to find the status of VPN connection through framework APIs or any other efficient method?

So far:
I found the following solutions
Using Broadcastreceiver
The broadcast receiver is deprecated from ICS
Using Ping or Traceroute
It's definitely going to take lots of time and its not efficient
Polling for DNS server changes
It's definitely going to take lots of time and its not efficient
Using ip-address
Even though it won't take much time depending on the network connection it may vary
My conclusion:
So far all the solutions I found are either not efficient or not so reliable.
My questions:
If VPN is connected in android device then android OS must be aware of it.
Are there any public android framework APIs to read it because finding it locally is most efficient and reliable solution ?
Are there any other efficient and reliable ways to achieve it (like integrating a C or C plus plus library with NDK)?
Note:
I couldn't find any custom broadcast senders/AIDL from OpenVPN for Android as well
You can try to check for tun0 INTERFACE, It is being start afther the establish command.
try {
for( NetworkInterface intf : Collections.list(NetworkInterface.getNetworkInterfaces())) {
// Pass over dormant interfaces
if(!intf.isUp() || intf.getInterfaceAddresses().size() == 0)
continue;
if ("tun0".equals(intf.getName())){
// The VPN is up
break;
}
}
}
this also might work:
(Collections.list(NetworkInterface.getByName("tun0")
Have you used - VpnService
As per documentation -
Prepare to establish a VPN connection. This method returns 'null' if the VPN application
is 'already prepared'.
From here-
http://developer.android.com/reference/android/net/VpnService.html#prepare(android.content.Context)
Intent intent = VpnService.prepare(getApplicationContext());
if (intent == null) {
// this means there is already a prepared VPN connection
}
I know the answer is ridiculously late, but I was just trying to figure this out and stumbled upon the command:
ifconfig tun0
This will return an IP address and other info if the VPN is connected and if it isn't.
tun0: No such device

Android JellyBean and P2P support

I am analysing Android JellyBean 4.3 source code.I could find the varialbe p2p_supported in HAL layer for Wi-Fi Direct support. In the below code snippet from wifi_ath.c
int wifi_start_supplicant(int p2p_supported)
{
if (p2p_supported)
{
strcpy(supplicant_name, P2P_SUPPLICANT_NAME);
strcpy(supplicant_prop_name, P2P_PROP_NAME); // for P2P support
.......................
}
else {
strcpy(supplicant_name, SUPPLICANT_NAME);
strcpy(supplicant_prop_name, SUPP_PROP_NAME); //for station support
}
The values of the macros are:
P2P_SUPPLICANT_NAME = p2p_supplicant ,P2P_PROP_NAME= init.svc.p2p_supplicant
SUPPLICANT_NAME=wpa_supplicant ,SUPP_PROP_NAME=init.svc.wpa_supplicant
Even while connecting in station mode the if part is getting executed and I could not make the WiFi up. Where in the code exactly p2p_supported variable is enabled and disabled so that both the P2P and Wi-Fi works smoothly?
From Jelly Bean(4.1) you only need to turn WiFi on to use WiFi Direct functionality, though using both together depends upon whether your chip supports it.(For that see this SO question)

Enable Wifi Programatically or via Activity?

Is it considered best practice or more acceptable or, for any other reason, preferable, to initialise WiFI on an Android device via a programmatic approach:
WifiManager oWiFIMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
oWiFIMgr .setWifiEnabled(true);
vs. launching the WiFI settings activity?
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
I guess it depends on the purpose of your app.
If you are going to create a home screen widget like wifi toggle or something similar, the user would be pleased if just touching the widget turns On the wifi, but if its some app that just require the wifi access to do certain task, it would be better to open the wifi page allowing the user to take his own choice.
What really matters is you should design your app in such a way that once the purpose of wifi is done, it should be turned off again.
PS: No matter which choice you make in your app design the permissions of that app is going to be displayed during the installation.
So just keep in mind for the user friendliness of your app and its performance.
In Android Q (Android 10) you can't enable/disable wifi programmatically anymore. So you don't have a choice, you need to use Settings Panel to toggle wifi connectivity:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val panelIntent = Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY)
startActivityForResult(panelIntent, 0)
}
Always let user take those kind of decisions, enable WiFi, GPS .. stuff like that .. new Google maps app does that .. i think it's best ..

Enable 3G and WiFi Simultaneously

Does anyone know if its possible (programatically) to enable both 3G and WiFi to be used simultaneously, both receiving and sending packets?
I have seen various other questions on here, but with the Tethering ability inside Android now, I was wondering if this is a possibility? This has to be on a standard/stock device, and no modifications via root to the OS.
Thanks
Adam
I don't believe it is, as soon as wifi is turned on, 3g will automatically disconnect. As far as I know there is no way round it.
The only way you can achieve this is by using a APN name with the HttpConnection. This is possible in Java ME, Please visit this answer, however i have never tried it.
You need to check for the active connection, if not active first one the switch to the next connection.
public void shutup(){
SuDroid cmd = new SuDroid();
cmd.sh.runWaitFor("svc wifi enable");
cmd.sh.runWaitFor("svc data enable");
cmd.sh.runWaitFor("svc data prefer");
}
Using SuDroido

Categories

Resources