以编程方式打开/关闭WiFi热点 [英] Turn on/off WiFi hotspot programmatically
问题描述
在创建C#脚本时需要帮助,该脚本可将android WiFi设置为热点模式.这是我设法创建的代码.
public bool setAPEnabled(bool enabled)
{
using (AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity"))
{
try
{
if(isWifiEnabled()==true){
setWifiEnabled(false);
}
using (var wifiManager = activity.Call<AndroidJavaObject>("getSystemService", "wifi"))
{
return wifiManager.Call<bool>("setWifiApEnabled",null, enabled);
}
}
catch (Exception e)
{
}
}
return false;
}
一切正常-但是我在设置SSID和密码时遇到问题.在查看文档之后,我知道我必须替换我的设置对象具有null值,但是我完全不知道如何在Unity中实现.
这些方法仅适用于android 5.0及更低版本!
简单方式:
尝试首先实例化 WifiConfiguration
:/p>
AndroidJavaObject wifiConfiguration = new AndroidJavaClass("android.net.wifi.WifiConfiguration");
现在您可以调用此对象中的方法并设置/获取字段:
// to set SSID
wifiConfiguration.Set("SSID", meSSID); // string
wifiConfiguration.Set("preSharedKey", mePassword); // string
设置所有必填字段后,只需调用setWifiApEnabled
方法:
wifiManager.Call<bool>("setWifiApEnabled", wifiConfiguration, enabled);
也许您需要设置比这两个字段更多的字段,但要确认您应该检查
WifiConfiguration: 我试图找出为什么上面的代码可能无法正常工作,但对我来说却可以正常工作(在某些虚拟机和Samsung Galaxy S5 Neo上进行了测试). (我几乎在午夜时分发现)可能是一种密码短语. 也称为WPA-PSK(预共享密钥)模式,它是为家庭和小型办公室网络设计的,不需要身份验证服务器.[9]每个无线网络设备都使用256位密钥对网络流量进行加密.该密钥可以输入64位十六进制数字的字符串,也可以输入8至63个可打印ASCII字符的密码.[10]如果使用ASCII字符,则通过将PBKDF2密钥派生函数应用于密码短语(使用SSID作为SID和HMAC-SHA1的4096次迭代)来计算256位密钥.[11] WPA和WPA2均可使用WPA个人模式.) 我的建议是使用与上面链接的文章相同的密码,以确保密码有效. 还要注意的另一件事是 SSID 部分,该部分简短而完整地描述了 Everything works well - but I have a problem with setting the SSID and password. After reviewing the documentation I know that I have to replace my null value with the settings object, but I completely don't know how to do it in Unity. Theses methods works only for android 5.0 and less ! The EASY way : Try instantiating the Now you can call methods and set/get fields within this object : After settings all of the required fields just call your Maybe you will have to set more fields than these two but to confirm that you should check the source and ensure what The HARD way : Step 6 does not work for android 5.0+ ! Using reflection with So from the beginning :
WifiConfiguration : I was trying to find out why the above code might not work but for me it was working okay ( tested on some virtual machines and Samsung Galaxy S5 Neo ). What may be the case ( which I found out at almost midnight ) is a passphrase. Also referred to as WPA-PSK (pre-shared key) mode, this is designed for home and small office networks and doesn't require an authentication server.[9] Each wireless network device encrypts the network traffic using a 256 bit key. This key may be entered either as a string of 64 hexadecimal digits, or as a passphrase of 8 to 63 printable ASCII characters.[10] If ASCII characters are used, the 256 bit key is calculated by applying the PBKDF2 key derivation function to the passphrase, using the SSID as the salt and 4096 iterations of HMAC-SHA1.[11] WPA-Personal mode is available with both WPA and WPA2.) My suggestion would be to use the same passphrase as in the article linked above to make sure it's valid. Also another thing to note is the SSID part which has a short but good description here on wikipedia. A common, albeit incorrect assumption, is that an SSID is a string of human-readable characters (such as ASCII), terminated by a NUL character (as in a C-string). SSIDs must be treated and handled as what they are, a sequence of 0–32 octets, some of which may not be human-readable From what I've checked you do not need to null-terminate your string within Java or C# because it will be handled by native code but still you should not exceed 31 characters ( 32 will be the null character ). I checked this with : 这篇关于以编程方式打开/关闭WiFi热点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
根据此Wikipedia文章中有关 WPA-PSK 的部分>
WifiConfiguration
first :AndroidJavaObject wifiConfiguration = new AndroidJavaClass("android.net.wifi.WifiConfiguration");
// to set SSID
wifiConfiguration.Set("SSID", meSSID); // string
wifiConfiguration.Set("preSharedKey", mePassword); // string
setWifiApEnabled
method :wifiManager.Call<bool>("setWifiApEnabled", wifiConfiguration, enabled);
setWifiApEnabled
method does internaly.
( using reflection code )AndroidJavaObject
can be a bit tricky because you have to remember to dispose every object. // android code for that should look like :
// wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
// but in Unity C# you have to split this into few chunks:
// 1. Get calling class :
using ( AndroidJavaObject classObj = wifiManager.Call<AndroidJavaObject>("getClass") )
{
// classObj should contains your class object
// 2. call get WifiConfiguration class details :
using ( AndroidJavaObject wifiConfiguration = new AndroidJavaObject("setWifiApEnabled") )
{
// 3. Fill that object :
wifiConfiguration.Set("SSID", meSSID); // string
wifiConfiguration.Set("preSharedKey", mePassword); // string
// 4. Get WifiConfiguration class definition
using (AndroidJavaObject wifiCfgClass = wifiConfiguration.Call<AndroidJavaObject>("getClass") )
{
// 5. Get boolean definition
using ( AndroidJavaObject booleanObj = new AndroidJavaObject("java.lang.Boolean") )
{
using ( AndroidJavaObject booleanClass = booleanObj.Call<AndroidJavaObject>("getClass") )
// 6. Get method definition
using ( AndroidJavaObject methodObj = classObj.Call<AndroidJavaObject>("getMethod", "setWifiApEnabled", wifiCfgClass , booleanClass))
{
// 7. Call that method :)
methodObj.Call("invoke", wifiManager, wifiConfiguration, enabled);
}
}
}
}
}
According to this wikipedia article in the section about WPA-PSK
SSID:MeHotSpot
WPA-PSK:5260305714217573