7

I have a simple problem with my phone: When I get disconnected from a wifi hotspot, my phone doesn't automatically reconnect because it always thinks there is no Internet. This is because of my firewall. I'm guessing that Android phones try to ping a server to check if there is an Internet connection.

So what I'm currently trying to do is to write a simple script that constantly scans for Wifi networks until it finds one that I have been connected to in the past.
It would probably be a really simple script, but I just can't find a method to actually connect to an access point.

import threading
import androidhelper
droid = androidhelper.Android()
knownAPs = ["home-wifi","workAP"]

def autoConnect():
    threading.Timer(5.0, autoConnect).start()
    droid.wifiStartScan()
    nearbyAPs = droid.wifiGetScanResults().result
    for i, curAP in enumerate(nearbyAPs):
        if curAP['ssid'] in knownAPs:
            # ?connect to a wifi?
autoConnect()

Am I missing something in the docs? Any idea how I could accomplish this?

Edit:
Allegedly it is possible to use the Kivy library in QPython. I couldn't find an example of how it could be accomplished though. But maybe the problem could be solved like that?

Forivin
  • 14,780
  • 27
  • 106
  • 199
  • 1
    Maybe you're looking for `wifiReconnect`? This, and some other wifi related functions are described at the bottom of [document you mentioned](http://kylelk.github.io/html-examples/androidhelper.html) – running.t Nov 29 '16 at 12:28
  • ... just think about it. My phone doesn't automatically connect to any wifi hotspots anymore. I doubt that reconnect could automatically connect me to any wifi network I've been connected to in the past. – Forivin Nov 29 '16 at 13:08

1 Answers1

0

As of today, there is no way to specify which network to connect to in SL4A (which is used by androidhelper).

Moreover, to quote from SL4A's Github Page:

SL4A is no longer under active development. However, some forks of this project may be.


As a replacement, you can use plyer (I'm not sure if QPython provides this though). There is a really nice looking example of using plyer to control WiFi. I'm pretty sure you can adapt it for your needs.

If QPython does not support plyer (I hope it does), I recommend looking at python-for-android built by the folks at kivy who also maintain plyer. There's proper apps built with it [1].

This feels like a link-only-answer and an endorsement for kivy... Huh.

Community
  • 1
  • 1
pradyunsg
  • 18,287
  • 11
  • 43
  • 96
  • I have read that QPython can use Kivy's library. I couldn't find any examples though. Any idea how this could be done? – Forivin Dec 07 '16 at 11:41
  • Try `import plyer` on QPython. If that works, the linked example would be a great starting point. – pradyunsg Dec 07 '16 at 13:37
  • How do I even get plyer? I tried `pip install plyer` (which seems to work, at least I got no errors) and then I used `from plyer import wifi` in my script. Result: `ImportError: cannot import wifi` – Forivin Dec 08 '16 at 10:52
  • According to the Readme on Github, plyer doesn't support the wifi API for Android. – Forivin Dec 08 '16 at 10:59