androidwifi编程
㈠ Android系统手机WiFi通信功能开发
IDE就用Eclipse就足够了 对于Wifi的控制 android.net.wifi下有不少接口 但是要分别开发电视端和手机端 多思考就能找到电视和手机之间通讯的最好方式
㈡ 安卓怎么编程实现wifi安全检测
在Android中对Wifi操作,android本身提供了一些有用的包,在android.net.wifi包下面。主要包括以下几个类和接口: 1.ScanResult 主要用来描述已经检测出的接入点,包括接入点的地址,接入点的名称,身份认证,频率,信号强度等信息。 2.WifiConfiguration Wifi网络的配置,包括安全设置等。 3.WifiInfo wifi无线连接的描述,包括接入点,网络连接状态,隐藏的接入点,IP地址,连接速度,MAC地址,网络ID,信号强度等信息。这里简单介绍一下这里的方法: getBSSID() 获取BSSID getDetailedStateOf() 获取客户端的连通性 getHiddenSSID() 获得SSID 是否被隐藏 getIpAddress() 获取IP 地址 getLinkSpeed() 获得连接的速度 getMacAddress() 获得Mac 地址 getRssi() 获得802.11n 网络的信号 getSSID() 获得SSID getSupplicanState() 返回具体客户端状态的信息 4.WifiManager 这个不用说,就是用来管理我们的wifi 连接,这里已经定义好了一些类,可以供我们使用。 获取WIFI网卡的状态 WIFI网卡的状态是由一系列的整形常量来表示的。 1.WIFI_STATE_DISABLED : WIFI网卡不可用(1) 2.WIFI_STATE_DISABLING : WIFI网卡正在关闭(0) 3.WIFI_STATE_ENABLED : WIFI网卡可用(3) 4.WIFI_STATE_ENABLING : WIFI网正在打开(2) (WIFI启动需要一段时间) 5.WIFI_STATE_UNKNOWN : 未知网卡状态 最重要的一个就是 你要设置权限 最重要的一个就是 你要设置权限 希望帮助到你
㈢ 【android编程】android端怎么通过wifi连接电脑端的mysql数据库
最简单就是,安装xampp服务器,已经带有mysql,用android连接就行了
㈣ 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) { List<WifiConfiguration> 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; }}
㈤ 如何以编程方式创建和读取的WEP / EAP无线网络配置中的Android
1. 第1部分:创建一个无线网络的WEP配置编程 。
第2部分:阅读一个WEP无线网络配置编程
再次Straighforward。
第3部分:读一个EAP的WiFi配置编程
现在,这是棘手的。你可以找到它通过香草的Android用户界面中WifiDialog.java节省了EAP的WiFi配置的代码。唔够方便我们在我们的应用程序的代码,那么不要!如果你碰巧去尝试这一点,你会得到错误说找不到符号eap,phase,client_cert等。有点详细的调查EnterpriseFieldis private内WiFiConfiguration类和所有的符号,我们无法找到是类型EnterpriseField。好了,我们已经打了一个路障,我们需要这些字段读取/保存一个EAP配置,但我们并没有以编程方式访问他们!Java Reflection API救援
好吧,我不是一个Java专家,所以我不会越来越到的反射API的细节,例如,你可以谷歌的教程或到达这里。
为了保持简短而亲切,反射API允许你检查类,接口,字段在不知道的类,方法等,还可以实例化新对象,并获取/设置现场reflection.And,重要的是能思考的帮助您访问私有的类里面嗯,这是我们需要做的不是吗? :)
让我们来检查代码示例现在它显示了如何读取一个EAP的WiFi反射API。作为一个片段将记录配置到一个文件,并将其保存在SD卡....非常漂亮..诶;)反射API概述一点点,我相信ING下面的代码是很容易。
private static final String INT_PRIVATE_KEY = "private_key";
private static final String INT_PHASE2 = "phase2";
private static final String INT_PASSWORD = "password";
private static final String INT_IDENTITY = "identity";
private static final String INT_EAP = "eap";
private static final String INT_CLIENT_CERT = "client_cert";
private static final String INT_CA_CERT = "ca_cert";
private static final String INT_ANONYMOUS_IDENTITY = "anonymous_identity";
final String INT_ENTERPRISEFIELD_NAME = "android.net.wifi.WifiConfiguration$EnterpriseField";
第4部分:储存的EAP无线网络配置编程
如果你已经读过的部分3,您已经了解了反射mojo,在这里工作,如果你是直接跳跃到本节,请在第3部分的代码段之前,看了介绍,你会加快速度通过代码来这里微风!
void saveEapConfig(String passString, String userName)
{
/********************************Configuration Strings****************************************************/
final String ENTERPRISE_EAP = "TLS";
final String ENTERPRISE_CLIENT_CERT = " CodeGo.net
final String ENTERPRISE_PRIV_KEY = " CodeGo.net
//CertificateName = Name given to the certificate while installing it
/*Optional Params- My wireless Doesn't use these*/
final String ENTERPRISE_PHASE2 = "";
final String ENTERPRISE_ANON_IDENT = "ABC";
final String ENTERPRISE_CA_CERT = "";
/********************************Configuration Strings****************************************************/
/*Create a WifiConfig*/
WifiConfiguration selectedConfig = new WifiConfiguration();
/*AP Name*/
selectedConfig.SSID = "\"SSID_Name\"";
/*Priority*/
selectedConfig.priority = 40;
/*Enable Hidden SSID*/
selectedConfig.hiddenSSID = true;
/*Key Mgmnt*/
selectedConfig.allowedKeyManagement.clear();
selectedConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
selectedConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
/*Group Ciphers*/
selectedConfig.allowedGroupCiphers.clear();
selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
/*Pairwise ciphers*/
selectedConfig.allowedPairwiseCiphers.clear();
selectedConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
selectedConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
/*Protocols*/
selectedConfig.allowedProtocols.clear();
selectedConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
selectedConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
// Enterprise Settings
// Reflection magic here too, need access to non-public APIs
try {
// Let the magic start
Class[] wcClasses = WifiConfiguration.class.getClasses();
// null for overzealous java compiler
Class wcEnterpriseField = null;
for (Class wcClass : wcClasses)
if (wcClass.getName().equals(INT_ENTERPRISEFIELD_NAME))
{
wcEnterpriseField = wcClass;
break;
}
boolean noEnterpriseFieldType = false;
if(wcEnterpriseField == null)
noEnterpriseFieldType = true; // Cupcake/Donut access enterprise settings directly
Field wcefAnonymousId = null, wcefCaCert = null, wcefClientCert = null, wcefEap = null, wcefIdentity = null, wcefPassword = null, wcefPhase2 = null, wcefPrivateKey = null;
Field[] wcefFields = WifiConfiguration.class.getFields();
// Dispatching Field vars
for (Field wcefField : wcefFields)
{
if (wcefField.getName().equals(INT_ANONYMOUS_IDENTITY))
wcefAnonymousId = wcefField;
else if (wcefField.getName().equals(INT_CA_CERT))
wcefCaCert = wcefField;
else if (wcefField.getName().equals(INT_CLIENT_CERT))
wcefClientCert = wcefField;
else if (wcefField.getName().equals(INT_EAP))
wcefEap = wcefField;
else if (wcefField.getName().equals(INT_IDENTITY))
wcefIdentity = wcefField;
else if (wcefField.getName().equals(INT_PASSWORD))
wcefPassword = wcefField;
else if (wcefField.getName().equals(INT_PHASE2))
wcefPhase2 = wcefField;
else if (wcefField.getName().equals(INT_PRIVATE_KEY))
wcefPrivateKey = wcefField;
}
Method wcefSetValue = null;
if(!noEnterpriseFieldType){
for(Method m: wcEnterpriseField.getMethods())
//System.out.println(m.getName());
if(m.getName().trim().equals("setValue"))
wcefSetValue = m;
}
/*EAP Method*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefEap.get(selectedConfig), ENTERPRISE_EAP);
}
else
{
wcefEap.set(selectedConfig, ENTERPRISE_EAP);
}
/*EAP Phase 2 Authentication*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefPhase2.get(selectedConfig), ENTERPRISE_PHASE2);
}
else
{
wcefPhase2.set(selectedConfig, ENTERPRISE_PHASE2);
}
/*EAP Anonymous Identity*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefAnonymousId.get(selectedConfig), ENTERPRISE_ANON_IDENT);
}
else
{
wcefAnonymousId.set(selectedConfig, ENTERPRISE_ANON_IDENT);
}
/*EAP CA Certificate*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefCaCert.get(selectedConfig), ENTERPRISE_CA_CERT);
}
else
{
wcefCaCert.set(selectedConfig, ENTERPRISE_CA_CERT);
}
/*EAP Private key*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefPrivateKey.get(selectedConfig), ENTERPRISE_PRIV_KEY);
}
else
{
wcefPrivateKey.set(selectedConfig, ENTERPRISE_PRIV_KEY);
}
/*EAP Identity*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefIdentity.get(selectedConfig), userName);
}
else
{
wcefIdentity.set(selectedConfig, userName);
}
/*EAP Password*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefPassword.get(selectedConfig), passString);
}
else
{
wcefPassword.set(selectedConfig, passString);
}
/*EAp Client certificate*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefClientCert.get(selectedConfig), ENTERPRISE_CLIENT_CERT);
}
else
{
wcefClientCert.set(selectedConfig, ENTERPRISE_CLIENT_CERT);
}
// Adhoc for CM6
// if non-CM6 fails gracefully thanks to nested try-catch
try{
Field wcAdhoc = WifiConfiguration.class.getField("adhocSSID");
Field wcAdhocFreq = WifiConfiguration.class.getField("frequency");
//wcAdhoc.setBoolean(selectedConfig, prefs.getBoolean(PREF_ADHOC,
// false));
wcAdhoc.setBoolean(selectedConfig, false);
int freq = 2462; // default to channel 11
//int freq = Integer.parseInt(prefs.getString(PREF_ADHOC_FREQUENCY,
//"2462")); // default to channel 11
//System.err.println(freq);
wcAdhocFreq.setInt(selectedConfig, freq);
} catch (Exception e)
{
e.printStackTrace();
}
} catch (Exception e)
{
// TODO Auto-generated catch block
// FIXME As above, what should I do here?
e.printStackTrace();
}
WifiManager wifiManag = (WifiManager) getSystemService(Context.WIFI_SERVICE);
boolean res1 = wifiManag.setWifiEnabled(true);
int res = wifiManag.addNetwork(selectedConfig);
Log.d("WifiPreference", "add Network returned " + res );
boolean b = wifiManag.enableNetwork(selectedConfig.networkId, false);
Log.d("WifiPreference", "enableNetwork returned " + b );
boolean c = wifiManag.saveConfiguration();
Log.d("WifiPreference", "Save configuration returned " + c );
boolean d = wifiManag.enableNetwork(res, true);
Log.d("WifiPreference", "enableNetwork returned " + d );
}
以及多数民众赞成它!我希望这可以帮助开发者丢失,:)
希望这会有所帮助的
㈥ Android 代码设置Wifi 热点密码设置的问题
netConfig.allowedKeyManagement.set(4);
//WifiConfiguration.KeyMgmt.WPA2_PSK=4,但是不能直接拿到,所以参数直接设成4就行了
㈦ android开发中如果我想代码实现打开wifi热点如何实现
1·申请权限:
android.permission.ACCESS_WIFI_STATE
android.permission.CHANGE_WIFI_STATE
android.permission.WAKE_LOCK
2·获取WifiManager
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
3·开启、关闭wifi
if (wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
} else {
wifiManager.setWifiEnabled(true);
}
4·注意
如果遇到force-close, 选wait即可, 因为启动wifi需要几秒钟, UI如果5妙钟还没反映的话, 系统会给你这个force close exception
PS:我以前做过设计读取系统硬件信息的时候用过,但是很长时间没用了,这段注释是从网上来的,希望能帮到你。