安卓wifi模块开发
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
由于本人是一枚新接触安卓的萌新小白,接触的第一个任务就是wifi模块的代码调试
提示:以下是本篇文章正文内容,下面案例可供参考
一、Wifi是什么?
WiFi又称“行动热点”,是通过无线电波来连网,
二、写wifi模块使用到的相关的类
1、WifiManager
2、WifiConfiguration
3、BroadCastReceiver
4、ArrayList
三、wifi操作的简单步骤及方法
1、怎样获取一个wifi对象
在操作任何一个wifi设备之前,我们首要的操作就是获取一个wifi对象。我使用的是如下语句:
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
2、获得操作对象之后进行的操作
1)、打开和关闭WiFi,这里我们调用WifiManager类的自带方法,布尔值为true则打开,布尔值为false就关闭
wifiManager.setWifiEnabled(boolean isOpen)
2)、打开wifi之后,我们想做到的就是知道附近wifi都有哪些,这时我们就需要使用扫描方法得到得到周围的wifi
startScan() 开始扫描
3)、光扫描不行,我们还需要得到扫描之后的wifi信息并且呈现出来以便提供我们选择,这里我使用到了RecyclerView来呈现wifi列表,recyclerView的相关知识这里不过多阐述。
getScanResulats() 获取扫描测试的结果
recyclerView.setAdapter(wifiInfoAdapter);
呈现出扫描结果之后,别忘了更新!!!使用适配器自带的方法更新适配器内容
notifyDataSetChanged()更新适配器内容
4)、准备连接wifi,这里我们又要分成几种情况:
1、已经连接过且密码正确的wifi
2、已经连接过但密码错误的wifi
3、公共wifi
4、有密码且未连接过的wifi
其中1、3都可以看做不需要输入密码就可以连接;2、4就是需要密码正确才能连接。
//公共wifi的连接,AccseePoint是我们自己写的类,注意替换
public void connectPublicWifi(AccessPoint accessPoint) {
Log.d(TAG, "connect public wifi: ");
WifiConfiguration wifiConfiguration = accessPoint.getConfig();
accessPoint.generateOpenNetworkConfig();
int netId = wifiManager.addNetwork(wifiConfiguration);
wifiManager.enableNetwork(netId, true);
}
//连接有密码的wifi
public boolean connectNewWifi(AccessPoint accessPoint, String password) {
Log.d(TAG, "connect to access point: ");
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", getSsId(accessPoint));
wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
int security = accessPoint.getSecurity();
switch (security) {
case AccessPoint.SECURITY_NONE:
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiConfig.allowedAuthAlgorithms.clear();
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
break;
case AccessPoint.SECURITY_WEP:
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wifiConfig.wepKeys[0] = "\"" + password + "\"";
wifiConfig.wepTxKeyIndex = 0;
break;
case AccessPoint.SECURITY_PSK:
case AccessPoint.SECURITY_EAP:
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wifiConfig.preSharedKey = String.format("\"%s\"", password);
break;
default:
Log.w(TAG, "invalid security type: " + security);
break;
}
int netId = wifiManager.addNetwork(wifiConfig); //返回-1就不能正常连接
Log.d(TAG, "connectToAccessPoint--netId=" + netId);
if (netId == -1) {
Log.d(TAG, "connect to access point:false ");
return false;
} else {
Log.d(TAG, "connect to access point:true ");
wifiManager.enableNetwork(netId, true);
}
return true;
}
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅从一个零基础小白介绍了wifi的开关,扫描,连接等操作,有不对的还请各位大神指点,轻喷!