admin管理员组文章数量:1603247
我的使用场景:新买的盒子无屏如何通过手机让盒子连上wifi
1、首先依赖AndServer,添加权限,开启服务
github地址-https://github/yanzhenjie/AndServer
implementation 'com.yanzhenjie:andserver:1.0.2'//该版本不是最新版本
<!--wifi 网络权限 -->
<uses-permission android:name="android.permission.CHANGE_NETWORKE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
//创建并开启服务
private void createWebServer(){
AssetManager mAssetManager = getAssets();
//如果xx.html在assets下填""即可,如果在assets子目录下填子目录的文件名
WebSite wesite = new AssetsWebsite(mAssetManager, path);
AndServer andServer = new AndServer.Build()
.port(8080) // 默认是8080,Android平台允许的端口号都可以。
.timeout(10 * 1000) // 默认10 * 1000毫秒。
.website(wesite)
.registerHandler("wifi", new RequestWifiHandler())
.registerHandler("login", new RequestLoginHandler())
.listener(mListener)
.build();
Server mServer = andServer.createServer();
mServer.start();
}
//服务状态监听
private Server.Listener mListener = new Server.Listener() {
@Override
public void onStarted() {
// 服务器启动成功.
LogUtils.d("服务器启动成功");
}
@Override
public void onStopped() {
// 服务器停止了,一般是开发者调用server.stop()才会停止。
LogUtils.d("服务器停止了");
}
@Override
public void onError(Exception e) {
// 服务器启动发生错误,一般是端口被占用。
LogUtils.d("服务器启动发生错误");
}
};
2、创建并开启热点
private void createWifiHot(String name, String password) {
WifiManager mWifiManager = (WifiManager) LemonConfig.getApplicationContext().getSystemService(WIFI_SERVICE);
try {
//wifi和热点不能同时打开,所以先判断wifi是否打开,打开则关闭
if (mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(false);
}
//java反射机制得到Method
Method method = mWifiManager.getClass().getMethod("setWifiApEnabled",WifiConfiguration.class, boolean.class);
//配置热点信息
WifiConfiguration config = new WifiConfiguration();
config.SSID = name;
config.preSharedKey = password;
config.hiddenSSID = true;
config.status = WifiConfiguration.Status.ENABLED;
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
/*****如果连接热点需要密码,则解开注释,将config.allowedKeyManagement.set(0);注释掉****/
// int indexOfWPA2_PSK = 4;
从WifiConfiguration.KeyMgmt数组中查找WPA2_PSK的值
// for (int i = 0; i < WifiConfiguration.KeyMgmt.strings.length; i++) {
// if (WifiConfiguration.KeyMgmt.strings[i].equals("WPA2_PSK")) {
// indexOfWPA2_PSK = i;
// break;
// }
// }
config.allowedKeyManagement.set(0);
//通过java反射调用WifiManager的setWifiApEnabled方法
method.invoke(mWifiManager, config, true);
} catch (Exception e) {
e.printStackTrace();
}
}
3、手机连上热点,这样开启服务的设备与连上热点的手机就形成了一个局域网,通过http://ip:port/xxx.html就可以访问我们写好的html页面
4、html要提交给本地服务器通过第一步创建服务注册名来进行访问
5、在RequestWifiHandler类中接受html页面提交过来的信息,得到wifi名称和wifi密码进行连接
public class RequestWifiHandler implements RequestHandler {
private WifiManager mWifiManager;
@Override
public void handle(HttpRequest req, HttpResponse res, HttpContext con) {
Map<String, String> params = null;
try {
params = HttpRequestParser.parse(req);
} catch (IOException e) {
e.printStackTrace();
}
// 在这里得到html页面提交过来的参数,字段与html的name属性值对应上
String ssid = params.get("username");
String pwd = params.get("password");
LogUtils.d( "wifi!");
openWifi(ssid,pwd,res);
}
//关闭热点,打开wifi
private void openWifi(String userName, String password,HttpResponse res) {
mWifiManager = (WifiManager) LemonConfig.getApplicationContext().getSystemService(WIFI_SERVICE);
closeWifiHot();
// 调用函数打开/关闭WiFi,status为boolean变量true/false
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(true);
}
WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
// 指定对应的SSID
config.SSID = "\"" + userName + "\"";
config.preSharedKey = "\"" + password + "\"";
config.hiddenSSID = true;
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.status = WifiConfiguration.Status.ENABLED;
int netId = mWifiManager.addNetwork(config);
// 这个方法的第一个参数是需要连接wifi网络的networkId,第二个参数是指连接当前wifi网络是否需要断开其他网络
boolean isConnection=mWifiManager.enableNetwork(netId, true);
if (isConnection) {
// ProcessBuilder.Redirect.
StringEntity stringEntity = null;
try {
stringEntity = new StringEntity("连接成功", "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
res.setEntity(stringEntity);
}else {
//它不能够立马连接上,所以要从头再走一遍
openWifi(userName, password, res);
}
}
//关闭热点
public void closeWifiHot() {
try {
Method method = mWifiManager.getClass().getMethod("setWifiApEnabled",WifiConfiguration.class, boolean.class);
method.invoke(mWifiManager, null, false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
本文标签: 全过程类似AndServerportalwifi
版权声明:本文标题:使用AndServer实现类似连接wifi需要portal认证全过程 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1728434390a1157956.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论