android编程实现设置、打开wifi热点共享供他人连接的方法
作者:luoboo525 时间:2021-12-05 03:39:41
本文实例讲述了android编程实现设置、打开wifi热点共享供他人连接的方法。分享给大家供大家参考,具体如下:
用过快牙的朋友应该知道它们在两天设备之间传输文件的时候使用的是wifi热点,然后另一台便连接这个热点再进行传输。快牙传输速度惊人应该跟它的这种机制有关系吧。不知道它的搜索机制是怎样的,但我想应该可以通过热点的名字来进行判断吧。下面我们就来探讨一下如何自动创建一个wifi热点吧
创建wifi热点首先需要手机支持,建议开发的哥们整个好点的手机,我们公司那些个山寨设备,几近有一半是不支持热点的;其实创建热点很简单,先获取到wifi的服务,再配置热点名称、密码等等,然后再通过反射打开它就OK了。
下面我们看看创建热点的代码实现:
package com.tel.lajoin.wifi.hotspot;
import java.lang.reflect.Method;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class HotspotActivity extends Activity {
private WifiManager wifiManager;
private Button open;
private boolean flag=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取wifi管理服务
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
open=(Button)findViewById(R.id.open_hotspot);
//通过按钮事件设置热点
open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//如果是打开状态就关闭,如果是关闭就打开
flag=!flag;
setWifiApEnabled(flag);
}
});
}
// wifi热点开关
public boolean setWifiApEnabled(boolean enabled) {
if (enabled) { // disable WiFi in any case
//wifi和热点不能同时打开,所以打开热点的时候需要关闭wifi
wifiManager.setWifiEnabled(false);
}
try {
//热点的配置类
WifiConfiguration apConfig = new WifiConfiguration();
//配置热点的名称(可以在名字后面加点随机数什么的)
apConfig.SSID = "YRCCONNECTION";
//配置热点的密码
apConfig.preSharedKey="12122112";
//通过反射调用设置热点
Method method = wifiManager.getClass().getMethod(
"setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
//返回热点打开状态
return (Boolean) method.invoke(wifiManager, apConfig, enabled);
} catch (Exception e) {
return false;
}
}
}
布局就不写了吧,就一按钮,人人都知道的东西,写了也没啥意思。要实现文件传输,当然我们还需要写一个连接热点的客户端吧。连接热点的流程首先是搜索热点然后再判断热点是否符合规则然后再进行连接。
package com.tel.lajoin.wifiscan;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
public class MainActivity extends Activity {
private List<ScanResult> wifiList;
private WifiManager wifiManager;
private List<String> passableHotsPot;
private WifiReceiver wifiReceiver;
private boolean isConnected=false;
private Button connect;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
/* 初始化参数 */
public void init() {
setContentView(R.layout.main);
connect=(Button)findViewById(R.id.connect);
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiReceiver = new WifiReceiver();
//通过按钮事件搜索热点
connect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wifiManager.startScan();
}
});
}
/* 监听热点变化 */
private final class WifiReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
wifiList = wifiManager.getScanResults();
if (wifiList == null || wifiList.size() == 0 || isConnected)
return;
onReceiveNewNetworks(wifiList);
}
}
/*当搜索到新的wifi热点时判断该热点是否符合规格*/
public void onReceiveNewNetworks(List<ScanResult> wifiList){
passableHotsPot=new ArrayList<String>();
for(ScanResult result:wifiList){
System.out.println(result.SSID);
if((result.SSID).contains("YRCCONNECTION"))
passableHotsPot.add(result.SSID);
}
synchronized (this) {
connectToHotpot();
}
}
/*连接到热点*/
public void connectToHotpot(){
if(passableHotsPot==null || passableHotsPot.size()==0)
return;
WifiConfiguration wifiConfig=this.setWifiParams(passableHotsPot.get(0));
int wcgID = wifiManager.addNetwork(wifiConfig);
boolean flag=wifiManager.enableNetwork(wcgID, true);
isConnected=flag;
System.out.println("connect success? "+flag);
}
/*设置要连接的热点的参数*/
public WifiConfiguration setWifiParams(String ssid){
WifiConfiguration apConfig=new WifiConfiguration();
apConfig.SSID="\""+ssid+"\"";
apConfig.preSharedKey="\"12122112\"";
apConfig.hiddenSSID = true;
apConfig.status = WifiConfiguration.Status.ENABLED;
apConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
apConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
apConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
apConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
apConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
apConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
return apConfig;
}
@Override
protected void onDestroy() {
super.onDestroy();
/*销毁时注销广播*/
unregisterReceiver(wifiReceiver);
}
}
代码很简单,而且都有注释的,相信大伙儿能够看明白。 那就这样吧,至于文件传输建议还是去看看socket相关的文章吧。
希望本文所述对大家Android程序设计有所帮助。
标签:android,wifi
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java利用url实现网页内容的抓取
2023-05-14 11:10:21
![](https://img.aspxhome.com/file/2023/4/87214_0s.jpg)
java图形用户界面实现菜单功能
2023-11-23 11:50:58
![](https://img.aspxhome.com/file/2023/2/59032_0s.jpg)
Java8 如何正确高效的使用并行流
2021-06-01 20:29:19
![](https://img.aspxhome.com/file/2023/2/61032_0s.png)
Android事件分发机制(上) ViewGroup的事件分发
2022-05-28 12:40:05
![](https://img.aspxhome.com/file/2023/6/106006_0s.jpg)
IDEA 中使用 Hudi的示例代码
2021-08-27 21:51:04
![](https://img.aspxhome.com/file/2023/6/76236_0s.jpg)
浅谈Java多进程程序的运行模式
2021-07-06 11:06:49
MyBatis-Plus多表联查的实现方法(动态查询和静态查询)
2023-11-23 22:26:45
![](https://img.aspxhome.com/file/2023/9/59839_0s.png)
c#程序定期把内存信息记录到log日志示例
2022-01-11 16:33:31
探讨Object转为String的几种简易形式详解
2023-08-21 11:47:27
Flutter 分页功能表格控件详细解析
2023-09-22 20:02:45
![](https://img.aspxhome.com/file/2023/2/104692_0s.gif)
C#关键字async/await用法
2022-04-19 03:38:13
Java Apollo是如何实现配置更新的
2023-09-26 13:55:18
Android实现Service重启的方法
2021-10-01 09:25:25
Android控件CardView实现卡片布局
2022-12-05 02:10:32
![](https://img.aspxhome.com/file/2023/7/96027_0s.jpg)
Java常用数字工具类 数字转汉字(1)
2022-06-25 11:01:45
![](https://img.aspxhome.com/file/2023/3/63773_0s.jpg)
详解Java中List的正确的删除方法
2021-09-19 13:20:42
浅谈Java字符串比较的三种方法
2023-05-13 12:26:51
深入学习Java 热部署的知识
2023-10-16 18:23:24
![](https://img.aspxhome.com/file/2023/3/71673_0s.png)
Java API文档的使用方法详解
2022-06-15 20:12:03
![](https://img.aspxhome.com/file/2023/5/85915_0s.png)
MyBatis动态SQL标签的用法详解
2021-07-24 10:38:56