can we switch on/off the wi-fi of the device in a test cases in robotium?
because i am testing an issue which needs wifi to be on in initial phase then turning off the wi-fi and continue with testing.
Yes you can do it, see the example:
public void testNoNetworkConnection() throws Exception {
setWifiEnabled(false);
// do stuff solo.something
setWifiEnabled(true);
}
private void setWifiEnabled(boolean state) {
WifiManager wifiManager = (WifiManager)solo.getCurrentActivity().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(state);
}
Remember to add permission in your Manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
EDIT: With the new Robotium 5.3.1 you can use setWiFiData(Boolean turnedOn) to turn wifi on or off (See documentation)
Enjoy
You can use ExtSolo provided by Testdroid. Library can be found on https://github.com/bitbar/robotium-extensions and api for needed method: turn wifi. If you don't want to use extra library you can use code as below:
protected void turnWifi(boolean enabled) {
try {
WifiManager wifiManager = (WifiManager) getInstrumentation()
.getTargetContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(enabled);
} catch (Exception ignored) {
// don't interrupt test execution, if there
// is no permission for that action
}
}
You need permission in your application to change state of Wifi
Since, you will test the application where you know the behavior(when wifi is on and when wifi is off) of application , you can mock the data accordingly. Mocking the data will make the application to behave as it is expected in that particular situation.
There is not an easy way to do this, that is different to me saying that it is not possible though but might be enough to disuade you from doing this.
If your application has the correct permissions then you can from your robotium script in fact turn on or off the wifi just as any application can. But remember the permission needs to be in your applications manifest not your test apk manifest.
If your application doesn't have the correct permission (and you are unwilling to change it) there is another way but is quite a lot of work (working on getting the solution I use open sourced). Make an application that does have the permission as a service, bind to that service in your tests and ask that service to turn off the wifi.
Related
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
}
};`
We are developing a locked down "kiosk-style" Android app on a stock Samsung tablet, which is mounted in customer vehicles. We would like to be able to allow customers to edit their wifi settings, without giving them access to the rest of the Settings app (e.g. Launcher, accounts, etc)
We have been able to launch the Wifi Settings activity, but it allows the user to go into other areas.
I'm not sure whether it's possible to create a custom interface for connecting to wifi, but even if it were possible, this seems fragile and a lot of work for something quite simple.
Is there any way to solve this well?
I would create a device policy controller app that is provisioned on the device as a device owner using Android Enterprise (Android for Work) APIs.
https://developers.google.com/android/work/dpc/build-dpc
As a device owner, you can set your app in lock task mode which is generally used for kiosks.
https://developer.android.com/work/cosu.html
Then, you can set user restrictions:
addUserRestriction api
user restrictions list
The user restrictions don't cover everything in the settings app, but the coverage is pretty good.
Then I would provision it using NFC or QR code reader from the Google Setup Wizard welcome screen.
https://github.com/googlesamples/android-NfcProvisioning
You might want to also look at existing open source EMM/MDM implementations that already exist such as WSO2.
Other references:
How to enable task locking in Android 5.0 production devices
How to make sure there is only one app
I was also working on Kiosk Type applications and we have to give options for Change wifi and Display Settings So we have used these commands on Button click for Wifi And Display
Settings
btnWifiSetting.setOnClickListener {
startActivityForResult( Intent(android.provider.Settings.ACTION_WIFI_SETTINGS), 0);
}
And For Display Setting
btnDisplay.setOnClickListener {
startActivityForResult(Intent(android.provider.Settings.ACTION_DISPLAY_SETTINGS),0)
}
And you can also check the full list of Available Commands here
https://ourcodeworld.com/articles/read/318/how-to-open-android-settings-programmatically-with-java
try LineAgeOS
https://lineageos.org/
Your requirement needs to access OS System level, this way you have access and customize the WIFI settings before releasing the phone itself
can you try this way.
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
Tools_WiFi.setImageResource(R.drawable.tool_wifi_off);
} else {
wifiManager.setWifiEnabled(true);
Tools_WiFi.setImageResource(R.drawable.tool_wifi_on);
}
You can try this:
startActivityForResult(new Intent(Settings.ACTION_WIFI_SETTINGS), 0);
Hope it helps you.
I'm building a bluetooth app for android. I have a UI button, and when I press it, I want to engage bluetooth discovery mode for 30 seconds. The code I have right now does this perfectly, however it generates a popup to ask if I want to allow bluetooth discovery. This popup kinda ruins the flow of my application, so is there a way to bypass it?
No you can't bypass it. The dynamic permissions were introduced with Android 6. This allows the user to specify the permissions he want to grant to an App more precisely.
For an Android user it doesn't ruin the flow. It is normal for him. This is how Android works (and iOS, too btw).
See: https://developer.android.com/guide/topics/permissions/requesting.html
The BLUETOOTH_ADMINis considered as a normal permission. But for a scan, you also need ACCESS_COARSE_LOCATION and/or ACCESS_FINE_LOCATION. These are classified as dangerous permissions and that's why you need to ask the user once.
also see: BluetoothLeScanner.startScan()
There are several blog posts on how to handle permissions. James Montemagno released a plugin for that.
Blog Post
Permission Plugin
Yes. You can check if it's enabled and enable it programmatically,
BluetoothManager btm = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bta = btm.getAdapter();
if (!bta.isEnabled()) {
boolean ret = bta.enable();
if (!ret) {
// enable failed!
}
}
You need to have the BLUETOOTH_ADMIN permission,
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
So I've created the majority of my application but I am having an issue with power saving applications interfering with it. I use the AlarmManager to run a piece of code that send information to a server every x minutes (minimum 1h), the main issue I am having is that power managers are disabling with WiFi because the device is sleeping.
What's the most effective way to ensure WiFi is available at wakeup? Is it to simply enable WiFi and reconnect it?
Maybe an other way to your solution is to listen for the connection_changed intents. That way you know there is a connection to the internet and you can upload.
You can also enable WiFi but you will need permissions for that: (I guess these)
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(enabled);
Haven't tried it but this should do the trick.
On android 2.3 the permission <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> is not allowed anymore for non-system applications. Is there an alternative to enable/disable data connection, besides that one where you modify APN name? I found Data Enabler Widget on Android Market that does that, but I can't seem to understand how. Can anyone help me?
Thanks! - Alex Ady
I don't know how to change data connection (3g, 2g, etc), but you can enable/disable wifi connection through this:
WifiManager wifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(boolean enabled);
I found a solution to my problem, so I'm closing this question. The alternative is to simply display the mobile settings activity if a level 10 API or higher is detected, or continue with direct enable from code otherwise. I keep the android.permission.MODIFY_PHONE_STATE, but only use it the API is under level 10.