當前位置:首頁 » 安卓系統 » androidwifi編程

androidwifi編程

發布時間: 2022-07-12 12:30:11

㈠ 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:我以前做過設計讀取系統硬體信息的時候用過,但是很長時間沒用了,這段注釋是從網上來的,希望能幫到你。

熱點內容
dag見證伺服器是虛擬ip嗎 發布:2025-01-28 10:07:04 瀏覽:605
dz上傳的圖片不顯示 發布:2025-01-28 09:37:42 瀏覽:887
joinsql多表 發布:2025-01-28 09:23:26 瀏覽:729
php數組循環賦值 發布:2025-01-28 09:23:25 瀏覽:134
android42系統 發布:2025-01-28 09:21:59 瀏覽:902
菜單設計c語言 發布:2025-01-28 09:21:54 瀏覽:274
sql多表查詢優化 發布:2025-01-28 09:21:05 瀏覽:503
iphone6便捷訪問 發布:2025-01-28 09:05:11 瀏覽:177
四位驗證密碼是多少 發布:2025-01-28 08:56:13 瀏覽:809
筆記本顯卡如何配置 發布:2025-01-28 08:49:49 瀏覽:603