在 android 上使用 pyjnius 连接到 wifi 网络
Connect to a wifi network with pyjnius on android
我正在为 Android 制作一个扫描 Wi-Fi 网络的应用程序,我已经在 Android 中发现了如何使用 pyjnius 扫描 Wi-Fi 网络,但我仍然不知道我如何使用 pyjnius 连接到 Wi-Fi 网络,我已经看到如何在 kotlin 中完成此操作,并且我尝试使用 pyjnius 进行相同操作,但它对我不起作用,这是我的代码
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()
当我 运行 函数时出现此错误
jnius.jnius.JavaException: JVM exception occurred: Illegal reason value: 6619241
如果有人能告诉我我做错了什么,我将不胜感激,谢谢。
我已经设法让它工作了,这是一个如何使用 kivy 和 Pyjnius 连接到 Wifi 网络的小例子,我希望它能帮助别人,不客气。
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()
在 Pydroid3 中制作
我正在为 Android 制作一个扫描 Wi-Fi 网络的应用程序,我已经在 Android 中发现了如何使用 pyjnius 扫描 Wi-Fi 网络,但我仍然不知道我如何使用 pyjnius 连接到 Wi-Fi 网络,我已经看到如何在 kotlin 中完成此操作,并且我尝试使用 pyjnius 进行相同操作,但它对我不起作用,这是我的代码
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()
当我 运行 函数时出现此错误
jnius.jnius.JavaException: JVM exception occurred: Illegal reason value: 6619241
如果有人能告诉我我做错了什么,我将不胜感激,谢谢。
我已经设法让它工作了,这是一个如何使用 kivy 和 Pyjnius 连接到 Wifi 网络的小例子,我希望它能帮助别人,不客气。
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()
在 Pydroid3 中制作