I'm making an app for Android that scans Wi-Fi networks, I already discovered how to use pyjnius to scan Wi-Fi networks in Android but I still don't know exactly how I can connect to a Wi-Fi network with pyjnius, I have seen how this could be done in kotlin and I have tried to do the same with pyjnius but it does not work for me, this is my code
def conect(self):
Contexto = autoclass('android.content.Context')
ConnectivityManager = autoclass('android.net.ConnectivityManager')
WifiConfiguration = autoclass('android.net.wifi.WifiConfiguration')
WifiManager = autoclass('android.net.wifi.WifiManager')
Actividad = autoclass('android.app.Activity')
PythonActivity = autoclass('org.renpy.android.PythonActivity')
activity = PythonActivity.mActivity
service = activity.getSystemService(Contexto.WIFI_SERVICE)
#String = jnius.autoclass("java.lang.String")
WifiConfiguration.SSID ="TURBONETT_295786"
WifiConfiguration.preSharedKey = "KMgApsqz"
p = service.addNetwork(WifiConfiguration)
#service.getConfiguredNetworks()
service.disconnect()
service.enableNetwork(p, True)
#service.startScan()
service.reconnect()
When i ran the function i get this error
jnius.jnius.JavaException: JVM exception occurred: Illegal reason value: 6619241
If someone can tell me that I am doing wrong I would really appreciate it, thank you.
I have managed to make it work, this is a small example of how to connect to a Wifi network with kivy and Pyjnius, I hope it helps someone, You're welcome.
import kivy
from jnius import autoclass
from kivymd.app import MDApp
from kivy.lang.builder import Builder
import kivymd
from kivymd.uix.button import MDRoundFlatButton
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.toast import toast
String = autoclass('java.lang.String')
WifiConfigure = autoclass('android.net.wifi.WifiConfiguration')
PythonActivity = autoclass('org.kivy.android.PythonActivity')
activity = PythonActivity.mActivity
service = activity.getSystemService("wifi")
WifiManager = autoclass('android.net.wifi.WifiManager')
#The WifiManager methods are static which means that you do not have to instantiate the class
WifiConfig = WifiConfigure()
#Instant the class since its methods are public and not static
Builder.load_string('''
<Principal>:
MDRoundFlatButton:
text: 'Conectar'
pos_hint: {"center_x": .5, "center_y": .64}
on_press:
root.Conectar()
MDCard:
id: dos
elevation: 0
padding: '0dp'
size_hint: .95, .07
pos_hint: {"center_x": .5, "center_y": .85}
border_radius: 20
radius: [10]
TextInput:
id: t1
text: ''
hint_text: 'Nombre de la Red'
pos_hint: {"center_x": .55, "center_y": .84}
size_hint: .7, .05
text_color: 1, 1, 1, 1
multiline: False
background_color: 0,0,0,0
foreground_color: 1,1,1,1
MDCard:
id: dos
elevation: 0
padding: '0dp'
size_hint: .95, .07
pos_hint: {"center_x": .5, "center_y": .75}
border_radius: 20
radius: [10]
TextInput:
id: t2
text: ''
hint_text: 'ContraseƱa'
pos_hint: {"center_x": .55, "center_y": .74}
size_hint: .7, .05
text_color: 1, 1, 1, 1
multiline: False
background_color: 0,0,0,0
foreground_color: 1,1,1,1
''')
class Principal(Screen):
def __init__(self, **kwargs):
super(Principal, self).__init__(**kwargs)
def Conectar(self):
toast("Conectando...")
Connectname = String(self.ids.t1.text)
connectkey = String(self.ids.t2.text)
WifiConfig.SSID = "\""+Connectname.toString()+"\""
WifiConfig.preSharedKey ="\""+ connectkey.toString()+"\""
added = WifiManager.addNetwork(WifiConfig)
WifiManager.enableNetwork(added, True)
class Inicia (MDApp):
def build(self):
self.theme_cls.theme_style = 'Dark'
hijo = ScreenManager()
princ = Principal(name="principal")
hijo.add_widget(princ)
return hijo
Inicia().run()
Made in Pydroid3