One of the advantages of arriving late to the Android party is that it is potentially easier to find answers about things now than it was a few years ago. One of the disadvantages is that some feature or other that was available in earlier releases of Android may have been removed after users started to rely on it, making a lot of online information obsolete. This is the case with USB mass storage support. However, finding this out led to an exploration of other features.
Just for interest's sake, I wanted to know if I could get an old laptop to talk to my Android phone. Since it is running an old version of Debian, it doesn't have the packaged Android tools that I can use on my main machine, and I didn't want to face the Android build system just to compile adb for it. Happily, I discovered that Google has a pure Python version of adb; unhappily, I discovered that it relies on a version of libusb that is much newer than the one on the laptop. I later built this version along with the Python bindings for it, plus a newer Python interpreter to actually run the bindings, but it failed to work.
Giving up on adb for a while, I thought that I could at least plug in the phone and use it as a USB mass storage device, but there didn't seem to be an option on the phone to enable this. Searching for information yielded some guides and videos describing how to find the right option in the phone's menus but the phones in those were all running very old versions of Android. I briefly looked at a newer video that began with the disclaimer that you need to have rooted your phone. At that point I also gave up on USB mass storage but not before I'd noticed the option in the phone's menu about tethering.
My phone's Settings screen starts with the Wireless & Networks section which contains four entries for Wi-Fi, Bluetooth, Data usage and Call settings. Following these is the More... entry which is used to open a screen showing some more specialised settings. On ancient Android phones this is where you find the setting for USB mass storage. On my phone the interesting entry is Tethering & portable hotspot, which may not be available on all phones of that vintage.
The USB tethering option is only enabled if the phone is connected to a computer via a USB cable. Enabling USB tethering causes the phone to assign itself an IP address and declare itself as a USB network adapter. I'm not actually sure if it's possible to find the IP address in the phone's settings screens, but it is a typical local network address like 192.168.42.129 like the ones you might use on a local area network. I wrote a simple application to run on the phone to show the addresses that it has defined.
With the phone acting as a network device and providing an address, we can connect to it by defining the connection to it. I found a guide to doing this that suggests setting up DHCP for the USB interface, but we can also set it up manually in the shell. However, we need to know which interface to use on the computer; typically it will be something like usb0 but we can check the system logs to find out:
$ dmesg | tail | grep RNDIS [20581.364236] rndis_host 1-2:1.0: usb0: register 'rndis_host' at usb-0000:00:12.2-2, RNDIS device, ee:d1:1f:ef:43:f9
So it looks like usb0 is correct in this case. With this information, and assuming that the phone chose an address on the 192.168.42.x subnet, we bring up the interface and assign an address to it:
$ sudo ifconfig usb0 up 192.168.42.63
It is interesting to see which ports are exposed on the phone, which has an address of 192.168.42.129 in the following example:
$ nmap -A -T4 192.168.42.129 Starting Nmap 6.00 ( http://nmap.org ) at 2017-08-15 17:08 CEST Nmap scan report for 192.168.42.129 Host is up (0.0062s latency). Not shown: 999 closed ports PORT STATE SERVICE VERSION 53/tcp open domain dnsmasq 2.51 | dns-nsid: |_ bind.version: dnsmasq-2.51 Service detection performed. Please report any incorrect results at http://nmap.org/submit/ . Nmap done: 1 IP address (1 host up) scanned in 10.65 seconds
Since the tethering option is supposed to be used to allow hosts connecting to the phone to use its networking capabilities, the ports it exposes are limited to those needed to perform this task. However, the network connection between the computer and the phone can be used to provide the phone with limited access to the computer's resources.
Perhaps the easiest way to check this is to use the phone's browser to view pages served by a webserver running on the computer. Using the addresses from the examples above, it should be possible to navigate to the computer's address (192.168.42.63) and fetch an index page.
This limited form of reverse tethering is possibly only useful for situations where you want to test applications that access online services, using the computer as a test or proxy server, though it seems to prevent adb from communicating with the phone. For more extensive reverse tethering it looks like you either need to have root permissions on the phone or can use an application that will tunnel a connection via some kind of Virtual Private Network facility, like this one seems to do, though I haven't tried it.
I'm not sure I'll use tethering or reverse tethering much but it's good to see that support is there. It would be better if I could directly modify the network interfaces and routing on the phone itself, but without root permissions that's not something I can do. One hand gives while the other takes away. As with USB mass storage, maybe tethering support isn't something users should rely on. It seems like something you have to ask permission for, not a feature to expect.
Categories: Python, Android
Copyright © 2017 David Boddie
Published: 2017-08-15 18:51:35 UTC
Last updated: 2017-08-15 18:51:35 UTC