车载系统Android WIFI开发小结

1、WifiManager 操作wifi的管理类 ,通过系统服务获取:

wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true/false); //打开或者关闭wifi
wifiManager.startScan();  //开始扫描
wifiManager.getScanResults();  //获取扫描的热点结果
wifiManager.removeNetwork( int networkId);  //移除某个缓存的连接,可以用作忘记之前连接过的wifi密码
		
wifiManager.saveConfiguration();  //保存当前的连接缓存配置

2、WifiConfiguration 表示wifi网络连接的信息

WifiConfiguration.SSID  // WIFI网络的名字,也就是显示在我们wifi列表上的名字
WifiConfiguration.networkId  //网络连接号,只要进行过连接的,不管成功与否,都会存在网络连接号
WifiConfiguration.getIpAddress()  //获取连接的ip地址
WifiConfiguration.getMacAddress()  //获取连接的mac地址
WifiConfiguration.getLinkSpeed() //获取连接速度
//判断wifi连接中的加密类型
 public static int getSecurity(WifiConfiguration config) {
        if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
            return SECURITY_PSK;
        }
        if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP) ||
                config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) {
            return SECURITY_EAP;
        }
        return (config.wepKeys[0] != null) ? SECURITY_WEP : SECURITY_NONE;
    }

3、ScanResult 扫描结果类

ScanResult.SSID //扫描的网络名称
ScanResult.level //网络的信号等级
//获取扫描的网络加密的类型
 public static int getSecurity(ScanResult result) {
        if (result.capabilities.contains("WEP")) {
            return SECURITY_WEP;
        } else if (result.capabilities.contains("PSK")) {
            return SECURITY_PSK;
        } else if (result.capabilities.contains("EAP")) {
            return SECURITY_EAP;
        }
        return SECURITY_NONE;
    }
//通过注册监听扫描变化的广播,来获取系统扫描结果的变化
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION 

4、WifiInfo 当前正在连接的wifi网络信息

WifiInfo.getSSID()  //获取网络连接名称,但是不是每一次都能够获取到
WifiInfo.getNetworkId()  //连接的网络号
//通过注册连接状态改变的广播,来更新连接状态
WifiManager.SUPPLICANT_STATE_CHANGED_ACTION
...
//获取连接状态
SupplicantState state = (SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
//SupplicantState是一个枚举类型
SupplicantState.ASSOCIATED   //关联完成
SupplicantState.ASSOCIATING  //正在进行关联
SupplicantState.COMPLETED   //连接和认证已经完成
SupplicantState.DISCONNECTED  //当前网络不能接入
SupplicantState.SCANNING      //扫描中
// 密码错误,清除保存
int linkWifiResult = intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, 123);
if (wifiSateChangeListener != null && linkWifiResult == WifiManager.ERROR_AUTHENTICATING) {
		removeNetworkLink(); //清除该网络名称对应的网络连接的缓存
	}

5、清除特定的网络

   public void removeNetworkLink(String SSID) {		
		List<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
		for (WifiConfiguration existingConfig : existingConfigs) {			
			if (existingConfig.SSID.equals("\"" + SSID + "\"")) {
				wifiManager.removeNetwork(existingConfig.networkId);
				wifiManager.saveConfiguration();
				return;
			}
		}		
	}

6、实战经验: 连接一个没有密码的热点,然后修改热点为WAP2 PSK密码协议。如果在热点密码修改之后,我们没有清除已经存在的连接,那么后面是无法再连上这个网络的了。开始我是在热点连接状态变为SupplicantState.DISCONNECTED 的时候,去移除当前的网络,但是发现我获取到的WifiInfo里面,SSID是空的;后面改为在WifiManager.SCAN_RESULTS_AVAILABLE_ACTION广播变化的时候,去对比缓存配置信息和扫描的信息,如果发现同一个SSID的网络中的加密类型不一样,那么我们就手动移除这个网络。下面是代码片段:

	List<ScanResult> results = wifiManager.getScanResults();  
	List<WifiConfiguration> configs = wifiManager.getConfiguredNetworks();
		if (results != null) {
			for (ScanResult result : results) {
				if (result.SSID == null || result.SSID.length() == 0 || result.capabilities.contains("[IBSS]")) {
					continue;
				}				
				for(WifiConfiguration config : configs){	
					if(removeDoubleQuotes(config).equals(result.SSID) && getSecurity(config)!=getSecurity(result)){
						//如果SSID相同且加密方式不同,清除该网络
						removeNetworkLink(config.networkId);
					}
				}
			}
		}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android车载操作系统开发揭秘是一份专门介绍Android车载系统开发的资料。这份资料提供了关于Android车载系统的海量知识,帮助读者更系统地了解Android车载系统的相关内容。它包括了一系列章节,如蓝牙、Android AutoMotive与车机之间物理互联原理、Android Automotive OS启动的详细解析等等。这份资料的目的是为了帮助程序员们快速积累知识并提升技能。所以如果你对Android车载操作系统开发有兴趣,可以点击提供的链接获取这份资料。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Android车载系统开发兴起,谷歌能否在智能汽车上再造辉煌?](https://blog.csdn.net/zzz777qqq/article/details/127983122)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [2023最新整理,Android车载操作系统开发揭秘,无偿分享!](https://blog.csdn.net/Android23333/article/details/128461391)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值