wifi management with pyjnius

1,610 views
Skip to first unread message

hci...@gmail.com

unread,
Apr 30, 2014, 5:43:34 PM4/30/14
to kivy-...@googlegroups.com
I have an Android app that I want to get working as a module in kivy using pyjnius. I've attempted converting the code but since I'm a pyjnius n00b it's not working. Below is the code I'm running in java and the pyjnius code.

Java code minus imports and onCreate:
public boolean checkConnectivity(){
       
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE);
       
boolean conn = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
       
if(conn){
           
return true;
       
} else {
           
return false;
       
}
   
}
   
public void restartWifi(){
   
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

   
WifiManager  wifiManag = (WifiManager) this.getSystemService(WIFI_SERVICE);
   
boolean res1 = wifiManag.setWifiEnabled(false);
    res1
= wifiManag.setWifiEnabled(true);
}
   
public void tryWifi(){
   
if(checkConnectivity()){
       
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
   
} else {
       
Toast.makeText(this, "Restarting", Toast.LENGTH_SHORT).show();
        restartWifi
();
   
}
}

pyjnius code:
from jnius import autoclass, cast

context
= autoclass('android.content.Context')
conn_man
= autoclass('android.net.ConnectivityManager')
wifi_conn
= autoclass('android.net.wifi.WifiConfiguration')
wifi_man
= autoclass('android.net.wifi.WifiManager')
activity
= autoclass('android.app.Activity')

def check_connectivity():
    con_mgr
= cast(conn_man, activity.getSystemService(activity.CONNECTIVITY_SERVICE))
    conn
= con_mgr.getNetworkInfo(conn_man.TYPE_WIFI).isConnectedOrConnecting()
   
if conn:
       
return True
   
else:
       
return False

def restart_wifi():
    wifi
= cast(wifi_man, activity.getSystemService(context.WIFI_SERVICE))
    wifi_manag
= cast(wifi_man, context.getSystemService(WIFI_SERVICE))
    res1
= wifi_manag.setWifiEnabled(False)
    res1
= wifi_manag.setWifiEnabled(True)

def try_wifi():
   
if check_connectivity():
       
print('Connected')
   
else:
       
print('Restarting')
        restart_wifi
()

After compiling the pyjnius code and running it on my nexus 7 I get this error in logcat:
I/python  (14825):  Traceback (most recent call last):
I
/python  (14825):    File "/home/kivy/Desktop/main.py", line 41, in <module>
I
/python  (14825):    File "/home/kivy/android/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/app.py", line 577, in run
I
/python  (14825):    File "/home/kivy/Desktop/main.py", line 36, in build
I
/python  (14825):    File "/home/kivy/Desktop/main.py", line 27, in try_wifi
I
/python  (14825):    File "/home/kivy/Desktop/main.py", line 13, in check_connectivity
I
/python  (14825):    File "jnius_export_class.pxi", line 548, in jnius.jnius.JavaMethod.__call__ (jnius/jnius.c:17107)
I
/python  (14825):    File "jnius_export_class.pxi", line 503, in jnius.jnius.JavaMethod.ensure_method (jnius/jnius.c:16603)
I
/python  (14825):  jnius.jnius.JavaException: Unable to find a None method!
I
/python  (14825): Python for android ended.

Anybody know what I'm doing wrong here? I think it might have to do with how I'm casting or referencing the current activity?


Ben Rousch

unread,
May 1, 2014, 10:41:44 AM5/1/14
to kivy-...@googlegroups.com
The first problem I see is that you are using android.app.Activity. Kivy's python-for-android provides its own Activity. The usual way we use this is:

PythonActivity = autoclass('org.renpy.android.PythonActivity')
activity = PythonActivity.mActivity



--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
 Ben Rousch
   bro...@gmail.com
   http://clusterbleep.net/

hci...@gmail.com

unread,
May 1, 2014, 11:44:12 AM5/1/14
to kivy-...@googlegroups.com
Thanks for the help! I forgot about that using PythonActivity. I changed it out and it now has a different issue that throws runtime errors, a list a million miles long, I don't think you'd be interested. It's still not getting past the first line in the check_connectivity method. Here's what my code looks like now:

from jnius import autoclass, cast
import kivy
from kivy.app import App
from kivy.uix.label import Label


Context = autoclass('android.content.Context')
ConnectivityManager = autoclass('android.net.ConnectivityManager')
WifiConfiguration = autoclass('android.net.wifi.WifiConfiguration')
WifiManager = autoclass('android.net.wifi.WifiManager')
Activity = autoclass('android.app.Activity')

PythonActivity = autoclass('org.renpy.android.PythonActivity')
activity
= PythonActivity.mActivity


def check_connectivity():
   con_mgr
= cast(ConnectivityManager, activity.getSystemService(activity.CONNECTIVITY_SERVICE))
   conn
= con_mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting()

   
if conn:
       
return True
   
else:
       
return False


def restart_wifi():

    wifi
= cast(ConnectivityManager, activity.getSystemService(Context.WIFI_SERVICE))
    wifi_manag
= cast(WifiManager, Context.getSystemService(Context.WIFI_SERVICE))

    res1
= wifi_manag.setWifiEnabled(False)
    res1
= wifi_manag.setWifiEnabled(True)


def try_wifi():
   
if check_connectivity():
       
print('Connected')
   
else:
       
print('Restarting')
        restart_wifi
()
       
class myapp(App):
   
def build(self):
        try_wifi
()
       
return Label(text='Stuff')
       
if __name__=='__main__':
    myapp
().run()


hci...@gmail.com

unread,
May 1, 2014, 12:22:45 PM5/1/14
to kivy-...@googlegroups.com
It might be helpful that the runtime error says ' Fatal signal 11' at the top of it. That code has something to do with non initiated variables. I don't see anything above that seems to not be initialised. Is there?

hci...@gmail.com

unread,
May 1, 2014, 1:45:34 PM5/1/14
to kivy-...@googlegroups.com
Okay, so I fixed it myself just by trying different things out. The main issue I ran into was the fact that Java needs the casting of the ConnectivityManager object, pyjnius seems to not. I took out all the casting, made sure the activity references were to the python activity and took out some unnecessary lines of code and,  voila! I love python, even when you wrap Java code you end up with less lines and better efficiency. Here is the working code:

def check_connectivity():
    con_mgr
= activity.getSystemService(Activity.CONNECTIVITY_SERVICE)

    conn
= con_mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting()
   
if conn:
       
return True
   
else:
       
return False


def restart_wifi():

    wifi_service
= activity.getSystemService(Context.WIFI_SERVICE)
    wifi_service
.setWifiEnabled(False)
    wifi_service
.setWifiEnabled(True)


def try_wifi():
   
if not check_connectivity():
        restart_wifi
()

    

Ben Rousch

unread,
May 1, 2014, 1:47:46 PM5/1/14
to kivy-...@googlegroups.com
Thanks for following up with the fixed version. It often answers others' questions sometimes months down the road.


--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

hci...@gmail.com

unread,
May 1, 2014, 2:47:57 PM5/1/14
to kivy-...@googlegroups.com
Oh don't I know it! We do what we can to help.
Reply all
Reply to author
Forward
0 new messages