Search code examples
androidbluetoothusbtethering

Checking Tethering USB/Bluetooth is active


Problem

I'm trying to notify the user when he enables or disables the tethering functionality for Android devices higher than Android KitKat.

Despite accessing the WIFI Tethering hidden API via reflection, I was unable to find a similar way to tell me if Tethering via USB/Bluetooth is enable or not.

For USB Tethering

So far I changed my receiver manifest declaration to something like this:

<receiver android:name=".features.tethering.TetheringReceiver">
    <intent-filter>
        <action android:name="android.net.wifi.WIFI_AP_STATE_CHANGED"/>
        <action android:name="android.intent.action.ums_connected" />
        <action android:name="android.net.conn.TETHER_STATE_CHANGED" />
    </intent-filter>
</receiver>

With this I can receive indeed some actions when the USB Tether is disabled/enabled, however it does not send the current state, meaning I receive this action but not if is active or not, I only receive 3 arrays (ConnectivityManager.EXTRA_AVAILABLE_TETHER, ConnectivityManager.EXTRA_ACTIVE_TETHER and ConnectivityManager.EXTRA_ERRORED_TETHER).

I was checking the source code of ConnectivityManager and I found a method called tether, althouth I need WRITE_SETTINGS permissions for this. Is there any other way to check the usb tethering state without write settings permission?

For Bluetooth Tethering

For the bluetooth is a little different, so far I was unable to receive any broadcast or find a way to get the current state of the tethering bluetooth.

I changed the received to something like this, but I was unable to receive anything:

 <receiver android:name=".features.tethering.TetheringReceiver">
    <intent-filter>
        <action android:name="android.net.wifi.WIFI_AP_STATE_CHANGED" />
        <action android:name="android.intent.action.ums_connected" />
        <action android:name="android.net.conn.TETHER_STATE_CHANGED" />
        <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
        <action android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" />
        <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
        <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
    </intent-filter>
</receiver>

Additionally, I have the following permission declared:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Summary

I'm looking for a way to know the current state of USB and Bluetooth Tethering without any Dangerous permissions. Is there any way to do this?

I did also apply this solution on here but without success.


Solution

  • when tethering is enabled there is an additional network interface in /sys/class/net folder of the android (rndis0 in case of usb tethering or ap0 in case of wlan hotspot on my device).

    you can list all network interfaces using NetworkInterface class: https://developer.android.com/references/java/net/NetworkInterface.html

    the getHardwareAddress() function requires NetPermission

    if rndis0 or ap0 is in the list tethering is enabled

    maybe see https://airtower.wordpress.com/2010/07/29/getting-network-interface-information-in-java/

    an alternative is to use the shell command ls /sys/class/net that also lists all interfaces. the problem is that my android is rooted and i do not know if it is possible to access the information in the /sys/class/net folder without root normally you can execute shell commands in android and get the informations from them like in http://www.stackoverflow.com/questions/20932102/execute-shell-command-from-android

    apps have more rights on android, so you can appear as your app to read protected folders without root, see http://denniskubes.com/2012/09/25/read-android-data-folder-without-root