How to Refresh my Mobile data network in android current version? - android

Kindly give answer,i have trouble to refresh mobile network,I don't want system app,I need only for android Mobile apps,I need to refresh my mobile data network pro grammatically in current version like marshmallow and nougat,I already put permissions for this,i attached following my codes
private static boolean setMobileConnectionEnabled(Context context, boolean enabled) {
try {
// Requires: android.permission.CHANGE_NETWORK_STATE
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
Log.i("if", "" + Build.VERSION.SDK_INT);
// pre-Gingerbread sucks!
final TelephonyManager telMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
final Method getITelephony = telMgr.getClass().getDeclaredMethod("getITelephony");
getITelephony.setAccessible(true);
final Object objITelephony = getITelephony.invoke(telMgr);
final Method toggleDataConnectivity = objITelephony.getClass()
.getDeclaredMethod(enabled ? "enableDataConnectivity" : "disableDataConnectivity");
toggleDataConnectivity.setAccessible(true);
toggleDataConnectivity.invoke(objITelephony);
}
// Requires: android.permission.CHANGE_NETWORK_STATE
else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Log.i("else", "" + Build.VERSION.SDK_INT);
final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
// Gingerbread to KitKat inclusive
final Field serviceField = connMgr.getClass().getDeclaredField("mService");
serviceField.setAccessible(true);
final Object connService = serviceField.get(connMgr);
try {
final Method setMobileDataEnabled = connService.getClass()
.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
Log.i("try", "" + setMobileDataEnabled);
setMobileDataEnabled.setAccessible(true);
setMobileDataEnabled.invoke(connService, Boolean.valueOf(enabled));
} catch (NoSuchMethodException e) {
// Support for CyanogenMod 11+
final Method setMobileDataEnabled = connService.getClass()
.getDeclaredMethod("setMobileDataEnabled", String.class, Boolean.TYPE);
setMobileDataEnabled.setAccessible(true);
Log.i("catch", "" + setMobileDataEnabled);
Log.i("errr", "" + e.getMessage());
try {
setMobileDataEnabled.invoke(connService, context.getPackageName(), Boolean.valueOf(enabled));
} catch (InvocationTargetException e1) {
e1.printStackTrace();
}
}
}
// Requires: android.permission.MODIFY_PHONE_STATE (System only, here for completions sake)
else {
// Lollipop and into the Future!
final TelephonyManager telMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Log.i("telMgr",""+telMgr);
final Method setDataEnabled = telMgr.getClass().getDeclaredMethod("setDataEnabled", Boolean.TYPE);
Log.i("telMgdfgsdgr",""+setDataEnabled);
setDataEnabled.setAccessible(true);
setDataEnabled.invoke(telMgr, Boolean.valueOf(enabled));
}
return true;
} catch (NoSuchFieldException e) {
Log.e("", "setMobileConnectionEnabled", e);
} catch (IllegalAccessException e) {
Log.e("gsdjkghskd", "setMobileConnectionEnabled", e);
} catch (IllegalArgumentException e) {
Log.e("lllll", "setMobileConnectionEnabled", e);
} catch (NoSuchMethodException e) {
Log.e("nooooo", "setMobileConnectionEnabled", e);
} catch (InvocationTargetException e) {
Log.e("innnnnn", "setMobileConnectionEnabled", e);
}
return false;
}
My Manifest file
<permission
android:name="android.permission.READ_PHONE_STATE"
android:maxSdkVersion="24" />
<permission
android:name="android.permission.ACCESS_FINE_LOCATION"
android:maxSdkVersion="24" />
<permission
android:name="android.permission.MODIFY_PHONE_STATE"
android:maxSdkVersion="24" />
<permission
android:name="android.permission.CHANGE_NETWORK_STATE"
android:maxSdkVersion="24" />
Kindly give some suggestions please

I can't comment so I'll put my answer here, but it might disappoint you.
There's currently no way to do that in Android >= 5.
You can do that only on rooted phone or system app. It was possible through reflection but since it was #Hide annotated Google removed it and had every right to do so.
If you're looking for disabling only (or disabling the the possibility to change cellular data state) you can use your device provider's api (like Samsung). Samsung allows to disable the possibility to change the cell data state, but... you cannot force the state back to on, after the possibility to change was turned off.
Google repeatedly ignores tickets from the community to bring back this option to SDK.
Please downvote this answer only if you have a solution how to do that without rooted phone or system app.

Related

Get Bluetooth local mac address in Marshmallow

Pre Marshmallow my app would obtain it's device MAC address via BluetoothAdapter.getDefaultAdapter().getAddress().
Now with Marshmallow Android is returning 02:00:00:00:00:00.
I saw some link(sorry not sure where now) that said you need to add the additional permission
<uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS"/>
to be able to get it. However it isn't working for me.
Is there some additional permission needed to get the mac address?
I am not sure it is pertinent here but the manifest also includes
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
So is there a way to get the local bluetooth mac address?
zmarties is right but you can still get the mac address via reflection or Settings.Secure:
String macAddress = android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");
Access to the mac address has been deliberately removed:
To provide users with greater data protection, starting in this release, Android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi and Bluetooth APIs.
(from Android 6.0 Changes)
You can access Mac address from the file
"/sys/class/net/" + networkInterfaceName+ "/address" ,where networkInterfaceName can be wlan0 or eth1.But Its permission may be read-protected,so it may not work in some devices.
I am also attaching the code part which i got from SO.
public static String getWifiMacAddress() {
try {
String interfaceName = "wlan0";
List<NetworkInterface> interfaces = Collections
.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface intf : interfaces) {
if (!intf.getName().equalsIgnoreCase(interfaceName)) {
continue;
}
byte[] mac = intf.getHardwareAddress();
if (mac == null) {
return "";
}
StringBuilder buf = new StringBuilder();
for (byte aMac : mac) {
buf.append(String.format("%02X:", aMac));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
return buf.toString();
}
} catch (Exception exp) {
exp.printStackTrace();
}
return "";
}
First the following permissions have to be added to Manifest;
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS" />
Then,
public static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS = "bluetooth_address";
String macAddress = Settings.Secure.getString(getContentResolver(), SECURE_SETTINGS_BLUETOOTH_ADDRESS);
After that the application has to be signed with OEM / System key. Tested and verified on Android 8.1.0.
Please use the below code to get the bluetooth mac address. let me know if any issues.
private String getBluetoothMacAddress() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String bluetoothMacAddress = "";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M){
try {
Field mServiceField = bluetoothAdapter.getClass().getDeclaredField("mService");
mServiceField.setAccessible(true);
Object btManagerService = mServiceField.get(bluetoothAdapter);
if (btManagerService != null) {
bluetoothMacAddress = (String) btManagerService.getClass().getMethod("getAddress").invoke(btManagerService);
}
} catch (NoSuchFieldException e) {
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
} else {
bluetoothMacAddress = bluetoothAdapter.getAddress();
}
return bluetoothMacAddress;
}
Getting the MAC address via reflection can look like this:
private static String getBtAddressViaReflection() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Object bluetoothManagerService = new Mirror().on(bluetoothAdapter).get().field("mService");
if (bluetoothManagerService == null) {
Log.w(TAG, "couldn't find bluetoothManagerService");
return null;
}
Object address = new Mirror().on(bluetoothManagerService).invoke().method("getAddress").withoutArgs();
if (address != null && address instanceof String) {
Log.w(TAG, "using reflection to get the BT MAC address: " + address);
return (String) address;
} else {
return null;
}
}
using a reflection library (net.vidageek:mirror) but you'll get the idea.
Since below method return null for android O.
String macAddress = android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");
I found new way to get Bluetooth Mac address, you can try by using below command line.
su strings /data/misc/bluedroid/bt_config.conf | grep Address
NOTE: In my case, i was working with root device so my app has super user permission.
As it turns out, I ended up not getting the MAC address from Android. The bluetooth device ended up providing the Android device MAC address, which was stored and then used when needed. Yeah it seems a little funky but on the project I was on, the bluetooth device software was also being developed and this turned out to be the best way to deal with the situation.
Worked great
private String getBluetoothMacAddress() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String bluetoothMacAddress = "";
try {
Field mServiceField = bluetoothAdapter.getClass().getDeclaredField("mService");
mServiceField.setAccessible(true);
Object btManagerService = mServiceField.get(bluetoothAdapter);
if (btManagerService != null) {
bluetoothMacAddress = (String) btManagerService.getClass().getMethod("getAddress").invoke(btManagerService);
}
} catch (NoSuchFieldException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignore) {
}
return bluetoothMacAddress;
}

Enable/Disable Data mobile programmatically - NO ROOT

I'm trying to enable/disable data mobile in specific moments. I've tried it on MotoG 4.4.2 (Rooted) with it I had no problem to do this, but when I've tried it on Samsung Galaxy Grand Prime 4.4.4 (No Root) it doesn't work, also I've tried it on Samsung Galaxy S3 4.3 (No Root) but it worked.
MotoG Worked -- WHY? -- ROOT Acces
Samsung Galaxy Grand Prime didn't work -- WHY? -- I think it doesn't work because when I try to do it manually I've to accept a dialog that I want to lose data connectivity, and I think this is the problem because it doesn't work.
Samsung Galaxy S3 worked -- WHY? -- I think it worked because the user put "Don't show me again" and user accepted it before.
The code that I'm trying is :
public void GetDataConnectionAPI() {
this.context.getApplicationContext();
TelephonyManager telephonyManager =
(TelephonyManager) this.context.getApplicationContext().
getSystemService(Context.TELEPHONY_SERVICE);
try {
telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);
ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());
dataConnSwitchmethod_OFF =
ITelephonyClass.getDeclaredMethod("disableDataConnectivity");
dataConnSwitchmethod_ON = ITelephonyClass.getDeclaredMethod("enableDataConnectivity");
} catch (Exception e) {
e.printStackTrace();
}
}
And then I enable/disable the data with this method :
public void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
connectivityManagerField.setAccessible(true);
final Object connectivityManager = connectivityManagerField.get(conman);
final Class connectivityManagerClass = Class.forName(connectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(connectivityManager, enabled);
}
When I want to Enable I call this method :
public void EnableMobileData(){
try {
setMobileDataEnabled(context, true);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
On my Manifest I put
<!-- 3g uses-permisions-->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
I wonder if it's any possibility to avoid this dialog or just accept it programmatically..
I've read Bojan Kogoj answer that sais that I've to :
Pre-installed into a system folder on the ROM
Compiled by a manufacturer using their security certificate
But I'm wondering if is any other way to do this.
The MODIFY_PHONE_STATE permission is not guaranteed to work in all cases. By default it is a system-level permission, so it will only work on devices whose manufacturers have enabled it.
Similarly, methods like getITelephony() are device/manufacturer-specific, and there is no way to get the name of the methods for doing so on every device.
Unfortunately there is no guaranteed way to do this.

Reset Android mobile network signal?

I need my app to reset the device's mobile network signal. This would have the same effect as toggling airplane mode where connectivity is temporarily lost, a new IP address is assigned upon reconnection, and the LTE/signal icon in the status bar should disappear and then reappear upon reconnection. I found an app on the Play Store that I tested on my phone running Android 4.4.4 with CyanogenMod and it does exactly this, but I am unsure as to how I can implement this in my own app. I think it's related to the CHANGE_NETWORK_STATE permission. I am seeking documentation or some simple example code that will reset the network connection.
Note that I am not specifically trying to toggle airplane mode, but rather to reset the mobile data in the way that the app linked above does, as I have tested that it indeed works without even requiring root privileges.
Lollipop support requires a new System level privledge android.permission.MODIFY_PHONE_STATE to work.
private static boolean setMobileConnectionEnabled(Context context, boolean enabled)
{
try{
// Requires: android.permission.CHANGE_NETWORK_STATE
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD){
// pre-Gingerbread sucks!
final TelephonyManager telMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
final Method getITelephony = telMgr.getClass().getDeclaredMethod("getITelephony");
getITelephony.setAccessible(true);
final Object objITelephony = getITelephony.invoke(telMgr);
final Method toggleDataConnectivity = objITelephony.getClass()
.getDeclaredMethod(enabled ? "enableDataConnectivity" : "disableDataConnectivity");
toggleDataConnectivity.setAccessible(true);
toggleDataConnectivity.invoke(objITelephony);
}
// Requires: android.permission.CHANGE_NETWORK_STATE
else if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
// Gingerbread to KitKat inclusive
final Field serviceField = connMgr.getClass().getDeclaredField("mService");
serviceField.setAccessible(true);
final Object connService = serviceField.get(connMgr);
try{
final Method setMobileDataEnabled = connService.getClass()
.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabled.setAccessible(true);
setMobileDataEnabled.invoke(connService, Boolean.valueOf(enabled));
}
catch(NoSuchMethodException e){
// Support for CyanogenMod 11+
final Method setMobileDataEnabled = connService.getClass()
.getDeclaredMethod("setMobileDataEnabled", String.class, Boolean.TYPE);
setMobileDataEnabled.setAccessible(true);
setMobileDataEnabled.invoke(connService, context.getPackageName(), Boolean.valueOf(enabled));
}
}
// Requires: android.permission.MODIFY_PHONE_STATE (System only, here for completions sake)
else{
// Lollipop and into the Future!
final TelephonyManager telMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
final Method setDataEnabled = telMgr.getClass().getDeclaredMethod("setDataEnabled", Boolean.TYPE);
setDataEnabled.setAccessible(true);
setDataEnabled.invoke(telMgr, Boolean.valueOf(enabled));
}
return true;
}
catch(NoSuchFieldException e){
Log.e(TAG, "setMobileConnectionEnabled", e);
}
catch(IllegalAccessException e){
Log.e(TAG, "setMobileConnectionEnabled", e);
}
catch(IllegalArgumentException e){
Log.e(TAG, "setMobileConnectionEnabled", e);
}
catch(NoSuchMethodException e){
Log.e(TAG, "setMobileConnectionEnabled", e);
}
catch(InvocationTargetException e){
Log.e(TAG, "setMobileConnectionEnabled", e);
}
return false;
}
Permission required.
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
Starting with 4.4.2, one can no longer acquire permissions to toggle airplane mode or mobile data. However, using reflection, one can work around that limitation.
Here's how to toggle airplane mode:
Toggle airplane mode in Android
Here's a method to toggle mobile data pre 4.4.2 and post (using reflection):
Toggle mobile data programmatically on Android 4.4.2
Note, I found all that in less than 3 minutes by searching on my favorite search engine for "android toggle airplane mode" and "android toggle mobile data".
Note 2: You can use reflection for airplane mode toggling too, but you may need to do some digging to find the necessary ASOP API or see if someone else (like CyanogenMod) has already done it.

Enabling USB tethering programmatically - there is an app that did it for 2.3

I've read many questions here on SO that ask how to enable USB tethering programmatically.
The answer is always the same, ordinary applications can't do it, only system apps.
Yet for 2.3 you could download an app in the market that would do it for you.
https://play.google.com/store/apps/details?id=org.tdtran.autousbtethering
On ICS (Android 4.0.3) it no longer works.
How did they do it for 2.3? Is it possible also for 4.0?
using the following code you can enable USB tethering. i didt test in 4.0.
public void switchOnTethering() {
Object obj = getSystemService(Context.CONNECTIVITY_SERVICE);
for (Method m : obj.getClass().getDeclaredMethods()) {
if (m.getName().equals("tether")) {
try {
m.invoke(obj, "usb0");
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
ICS and above: To execute the reflection method, the application would require the WRITE_SECURE_SETTINGS permission.
This is not available unless the phone is rooted.
since this is one of the most popular pages in the Google results for this topic I'd like to contribute my code which is checking the available interfaces. It does work on a Gingerbread phone I have, but not my Galaxy S3.
// DETECT INTERFACE NAME
Log.i("UsbTethering","Detecting tetherable usb interface.");
String[] available = null;
ConnectivityManager connMgr = (ConnectivityManager)connectivityServiceObject;
Method[] wmMethods = connMgr.getClass().getDeclaredMethods();
for(Method getMethod: wmMethods)
{
if(getMethod.getName().equals("getTetherableUsbRegexs"))
{
try
{
available = (String[]) getMethod.invoke(connMgr);
break;
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
// DETECT INTERFACE NAME
if(available.length > 0)
{
for(String interfaceName : available)
{
Log.i("UsbTethering", "Detected " + String.valueOf(available.length) + " tetherable usb interfaces.");
Log.i("UsbTethering", "Trying to " + desiredString + " UsbTethering on interface " + interfaceName + "...");
Integer returnCode = (Integer)method.invoke(connectivityServiceObject, interfaceName);
if(returnCode == 0)
{
Log.i("UsbTethering", "UsbTethering " + desiredString + "d.");
return true;
}
else
{
Log.w("UsbTethering", "Failed to " + desiredString + "Usb Tethering. ReturnCode of method " + method.getName() + ": " + String.valueOf(returnCode));
}
}
}
On Samsumg Galaxy Tab 2 10.1 the interface isn't called "usb0" but "rndis0". Maybe that's the same for Galaxy SII
The port "rndis0" is enabled dynamically not availble in the the Tetherable interfaces list.
It gets added when the user selects the USB Tethering option in the menu.
The function ConnectivityManager::setUsbTethering(bool) is called when the option is selected. And this function call is allowed only for System applications.
I know its an old thread but i hope this could help some other people in the future ,
That code worked for me in Android 4.4(with root privilege)
code:
the trick is to use reflection , the method which changes the usb tethring is called "setUsbTethering"
I wont write the entire class but here is what you need:
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Method usbTethering = connectivityManager.getClass().getMethod('setUsbTethering')
int returnCode = (Integer)usbTethering.invoke(connectivityManager, true);
0 = success
you can print the entire class methods using the following code
private static void printClassMethod(#NonNull Class aClazz) {
Method[] wmMethods = aClazz.getDeclaredMethods();
for (Method method : wmMethods) {
Log.i('anytag', method.getName());
}
}
then call printClassMethod(ConnectivityManager.class)
More Over, you can go onto the class itself and check the methods and arguments needed.
to get things work:
you must set your app as System app,
Declare in manifest : android:sharedUserId="android.uid.system"
add writing secure permission<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"
Sign the apk using google certificate key(apk-with-system-privileges) and push it to /system/app
Mind, that usbTethering is turned on while USB is attached, so a nicer solution will be to register to both USB_STATE and USB_ATTACH and enable/disable it correspondingly in onReceive Method. (USB tethering is turned off automatically when USB is de-attached)

How to detect WiFi tethering state

I want to know how to detect state of WiFi tethering. I've seen an article: Android 2.3 wifi hotspot API But it doesn't work! It returns always WIFI_AP_STATE_DISABLED = 1. It doesn't depend on real state of WiFi tethering.
Using reflection:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Method[] wmMethods = wifi.getClass().getDeclaredMethods();
for (Method method: wmMethods) {
if (method.getName().equals("isWifiApEnabled")) {
try {
boolean isWifiAPenabled = method.invoke(wifi);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
As you can see here
In addition to the reflexion, to get the Wifi tethering status update, you can listen to this broadcast Action :
IntentFilter filter = new IntentFilter("android.net.wifi.WIFI_AP_STATE_CHANGED");
To get all tethering option update :
IntentFilter filter = new IntentFilter("android.net.conn.TETHER_STATE_CHANGED");
Those actions are hidden inside the Android source code
First, you need to get WifiManager:
Context context = ...
final WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
Then:
public static boolean isSharingWiFi(final WifiManager manager)
{
try
{
final Method method = manager.getClass().getDeclaredMethod("isWifiApEnabled");
method.setAccessible(true); //in the case of visibility change in future APIs
return (Boolean) method.invoke(manager);
}
catch (final Throwable ignored)
{
}
return false;
}
Also you need to request a permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Here is the Xamarin C# version if anyone is looking:
static Method isWifiApEnabledMethod;
public static bool IsWifiApEnabled ()
{
var wifiManager = WifiManager.FromContext (Application.Context);
if (isWifiApEnabledMethod == null)
{
try
{
isWifiApEnabledMethod = wifiManager.Class.GetDeclaredMethod ("isWifiApEnabled");
isWifiApEnabledMethod.Accessible = true; //in the case of visibility change in future APIs
}
catch (NoSuchMethodException e)
{
Debug.WriteLine ("Can't get method by reflection" + e);
}
catch (System.Exception ex)
{
Debug.WriteLine ("Can't get method by reflection" + ex);
}
}
if (isWifiApEnabledMethod != null)
{
try
{
return (bool)isWifiApEnabledMethod.Invoke (wifiManager);
}
catch (System.Exception ex)
{
Debug.WriteLine ("Can't invoke by reflection" + ex);
}
}
return false;
}
(without using reflection since they say google is restricting it)
I'm writting this answer 10 years later. also I don't know if this can be considered a good aproach or not but I first get the Wlan network interface IPs
and if there is no address I assume that it tethering isn't enabled. if there is an address, I check using the connectivity manger whether WI-FI is connected to a network or not. if there is an IP for the Wlan network interface but it isn't connected to a network, I assume tethering is enabled.
you probably would need to add this line to your manifest file
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
code to get the ip of an inteface (this only gets the IPv4, you can modify it to get the IPv6 or both if you want)
// method used to retrieve Wlan ip addresses IPv4 of the device.
public static String IpAddresses() throws NoAddressFoundException, SocketException {
Enumeration<NetworkInterface> Nics = NetworkInterface.getNetworkInterfaces();
while (Nics.hasMoreElements()) {
NetworkInterface NIC = Nics.nextElement();
if (NIC.isUp() && !NIC.isLoopback() && NIC.getName().contains("wlan")) {
Enumeration<InetAddress> Addresses = NIC.getInetAddresses();
while (Addresses.hasMoreElements()) {
InetAddress WlanAddress = Addresses.nextElement();
if (WlanAddress instanceof Inet4Address)
return WlanAddress.getHostAddress();
}
}
}
throw new NoAddressFoundException("No suitable wifi address found");
}
then if there is an address i check if wifi is connected by this method:
//method to check if the device is connected to a Wi-Fi network; it doesn't matter if network has access to internet
public static boolean isWifiConnected(Context context) {
ConnectivityManager ConMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo WifiInfo = ConMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return WifiInfo.isConnected();
}
NOTE: the "NoAddressFoundException" is a custom exception in my app if anyone is wondering. it won't exist in your case.
Reflection is a poor way to achieve this.
We can inspect the DhcpInfo to determine if the device is allocating addresses (mobile hotspot) or is being allocated by another DHCP server.
Here is a kotlin function that will determine if a device is a mobile hotspot, it has not been widely tested so YMMV.
fun isMobileHotspot(manager: WifiManager): Boolean {
val info = manager.dhcpInfo
return (
info.ipAddress == 0
&& info.netmask == 0
&& info.gateway == 0
&& info.serverAddress == 16885952) // 192.168.1.1
}

Categories

Resources