androidwifi开发,android wifi连接

Android的WiFi功能总结

先进行WiFi权限的声明

成都创新互联公司是一家企业级云计算解决方案提供商,超15年IDC数据中心运营经验。主营GPU显卡服务器,站群服务器,成都联通服务器托管,海外高防服务器,大带宽服务器,动态拨号VPS,海外云手机,海外云服务器,海外服务器租用托管等。

!-- 获取WiFi状态 --

uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /

uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /

!--  api = Android29调用setWifiEnabled  --

uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /

!-- 定位权限(api = 23 用于 WIFI扫描)--

uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /

uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /

注意:ACCESS_FINE_LOCATION和ACCESS_COARSE_LOCATION是危险级权限需要使用中还需要动态申请,申请其中一个即可。

WifiManager mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

if (Build.VERSION.SDK_INT =Build.VERSION_CODES.R) {

Intent panelIntent =new Intent(Settings.Panel.ACTION_WIFI);

startActivityForResult(panelIntent,null);

}else{ mWifiManager.setWifiEnabled(!isEnable);}

可以通过监听系统广播来了解WiFi状态。

WIFI_STATE_CHANGED_ACTION:WiFi开关状态;

SCAN_RESULTS_AVAILABLE_ACTION:WiFi列表变化;

RSSI_CHANGED_ACTION:WiFi信号强度变化。

IntentFilter mFilter =new IntentFilter();

mFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);

mFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);

mFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);

mReceiver =new BroadcastReceiver() {

@Override

public void onReceive(Context context,Intent intent) {

handleEvent(intent);

}

};

registerReceiver(mReceiver, mFilter);

private void handleEvent(Intent intent) {

String action = intent.getAction();

if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {

final int wifiState =mWifiManager.getWifiState();

if(wifiState ==WifiManager.WIFI_STATE_ENABLED){

postDelayed(()-{mRefreshLayout.autoRefresh();},1000);

}

}else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {

}else if (WifiManager.RSSI_CHANGED_ACTION.equals(action)) {

if(System.currentTimeMillis() -mLastUpateWifiRssiTime 1000){//刷新太快

        return;

}

}

updateWifiChange();

}

切记:wifistate切换到WifiManager.WIFI_STATE_ENABLED,mWifiManager.isWifiEnabled()需要有个等待时间切换。

3的基础上解析广播应答

private void updateWifiChange() {

final int wifiState =mWifiManager.getWifiState();

switch (wifiState) {

case WifiManager.WIFI_STATE_ENABLED:

ArrayListresults = (ArrayList)mWifiManager.getScanResults();

updateApListView(results);

mWlanSb.setChecked(mWifiManager.isWifiEnabled() );

break;

case WifiManager.WIFI_STATE_ENABLING:

break;

case WifiManager.WIFI_STATE_DISABLING:

mAdapter.clearData();

mWlanSb.setChecked(mWifiManager.isWifiEnabled() );

break;

case WifiManager.WIFI_STATE_DISABLED:

break;

}

}

android如何开发手动连接隐藏的wifi

您好,连接隐藏wifi的方法:

1、打开设置,进入WIFI开关界面

2、点击WIFI链接,选择要链接的WIFI.

3、选择下面的其他链接方式。

4、输入隐藏的WIFI名称,就是路由器发出来信号的名字。

5、点击安全性选择加密方式,一般加密方式都是WPA/WPA2PSK。

6、输入WIFI的密码,点击确定即可连接该wifi网络了。

Android WiFi开发,如何自动连接的代码

public class WifiAutoConnectManager {

private static final String TAG = WifiAutoConnectManager.class.getSimpleName();

WifiManager wifiManager;

// 定义几种加密方式,一种是WEP,一种是WPA,还有没有密码的情况 public enum WifiCipherType { WIFICIPHER_WEP, WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID }

// 构造函数 public WifiAutoConnectManager(WifiManager wifiManager) { this.wifiManager = wifiManager; }

// 提供一个外部接口,传入要连接的无线网 public void connect(String ssid, String password, WifiCipherType type) { Thread thread = new Thread(new ConnectRunnable(ssid, password, type)); thread.start(); }

// 查看以前是否也配置过这个网络 private WifiConfiguration isExsits(String SSID) { ListWifiConfiguration existingConfigs = wifiManager.getConfiguredNetworks(); for (WifiConfiguration existingConfig : existingConfigs) { if (existingConfig.SSID.equals("\"" + SSID + "\"")) { return existingConfig; } } return null; }

private WifiConfiguration createWifiInfo(String SSID, String Password, WifiCipherType Type) { WifiConfiguration config = new WifiConfiguration(); config.allowedAuthAlgorithms.clear(); config.allowedGroupCiphers.clear(); config.allowedKeyManagement.clear(); config.allowedPairwiseCiphers.clear(); config.allowedProtocols.clear(); config.SSID = "\"" + SSID + "\""; // nopass if (Type == WifiCipherType.WIFICIPHER_NOPASS) { config.wepKeys[0] = ""; config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.wepTxKeyIndex = 0; } // wep if (Type == WifiCipherType.WIFICIPHER_WEP) { if (!TextUtils.isEmpty(Password)) { if (isHexWepKey(Password)) { config.wepKeys[0] = Password; } else { config.wepKeys[0] = "\"" + Password + "\""; } } config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED); config.allowedKeyManagement.set(KeyMgmt.NONE); config.wepTxKeyIndex = 0; } // wpa if (Type == WifiCipherType.WIFICIPHER_WPA) { 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.allowedProtocols.set(WifiConfiguration.Protocol.WPA); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); config.status = WifiConfiguration.Status.ENABLED; } return config; }

// 打开wifi功能 private boolean openWifi() { boolean bRet = true; if (!wifiManager.isWifiEnabled()) { bRet = wifiManager.setWifiEnabled(true); } return bRet; }

class ConnectRunnable implements Runnable { private String ssid;

private String password;

private WifiCipherType type;

public ConnectRunnable(String ssid, String password, WifiCipherType type) { this.ssid = ssid; this.password = password; this.type = type; }

@Override public void run() { // 打开wifi openWifi(); // 开启wifi功能需要一段时间(我在手机上测试一般需要1-3秒左右),所以要等到wifi // 状态变成WIFI_STATE_ENABLED的时候才能执行下面的语句 while (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) { try { // 为了避免程序一直while循环,让它睡个100毫秒检测…… Thread.sleep(100); } catch (InterruptedException ie) { } }

WifiConfiguration wifiConfig = createWifiInfo(ssid, password, type); // if (wifiConfig == null) { Log.d(TAG, "wifiConfig is null!"); return; }

WifiConfiguration tempConfig = isExsits(ssid);

if (tempConfig != null) { wifiManager.removeNetwork(tempConfig.networkId); }

int netID = wifiManager.addNetwork(wifiConfig); boolean enabled = wifiManager.enableNetwork(netID, true); Log.d(TAG, "enableNetwork status enable=" + enabled); boolean connected = wifiManager.reconnect(); Log.d(TAG, "enableNetwork connected=" + connected); } }

private static boolean isHexWepKey(String wepKey) { final int len = wepKey.length();

// WEP-40, WEP-104, and some vendors using 256-bit WEP (WEP-232?) if (len != 10 len != 26 len != 58) { return false; }

return isHex(wepKey); }

private static boolean isHex(String key) { for (int i = key.length() - 1; i = 0; i--) { final char c = key.charAt(i); if (!(c = '0' c = '9' || c = 'A' c = 'F' || c = 'a' c = 'f')) { return false; } }

return true; }}

使用wifi要添加什么权限 Android开发

ACCESS_WIFI_STATE权限可以获取使用Wi-Fi等WLAN无线网络,加入android.permission.ACCESS_WIFI_STATE这句.

CHANGE_WIFI_STATE是改变WLAN状态的开关,如果打开或关闭Wi-Fi必需加入android.permission.CHANGE_WIFI_STATE的声明.

[img]

Android开发如何使用代码配置手机wifi的pac地址?

这里用到的手机型号为魅族M6120,其中的具体步骤如下:

1、打开手机的设置界面,需要选择无线网络这一项。

2、在无线网络中找到已经连接的WIFI网络,并点击旁边的详情按钮。

3、会进入WIFI的详情页面,选择底部的代理设置项目。

4、等来到代理设置窗口以后,点击里面的无选项。

5、下一步,在菜单中选择手动这一项。

6、这个时候可以填写相关的服务器和端口。

7、这样一来通过点击右上角的保存按钮,即可实现Android开发使用代码配置手机wifi的pac地址。


网页题目:androidwifi开发,android wifi连接
文章位置:http://pwwzsj.com/article/dsohojp.html