本文整理匯總了Java中android.net.wifi.WifiManager.enableNetwork方法的典型用法代碼示例。如果您正苦於以下問題:Java WifiManager.enableNetwork方法的具體用法?Java WifiManager.enableNetwork怎麽用?Java WifiManager.enableNetwork使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.net.wifi.WifiManager
的用法示例。
在下文中一共展示了WifiManager.enableNetwork方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: connectWifiSSID
import android.net.wifi.WifiManager; //導入方法依賴的package包/類
/**
* 給溫控器成功發送聯網命令後,連接溫控器連接的wifi節點
*
* @param context 上下文對象
* @param ssid ssid
* @param password 密碼
*/
public static void connectWifiSSID(Context context, WifiManager manager, String ssid, String
password) {
e("reSetNetwork----------連接設備連入的路由---" + ssid);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
e("當前手機係統>=Android6.0,采取兼容模式");
new WifiAutoConnectManager(manager).connect(ssid, password, WifiAutoConnectManager
.getCipherType(context, ssid));
} else {
int networkId = manager.addNetwork(getWifiConfiguration(manager, ssid, password));
if (networkId != -1) {
manager.enableNetwork(networkId, true);
}
}
}
示例2: updateNetwork
import android.net.wifi.WifiManager; //導入方法依賴的package包/類
/**
* Update the network: either create a new network or modify an existing network
* @param config the new network configuration
*/
private static void updateNetwork(WifiManager wifiManager, WifiConfiguration config) {
Integer foundNetworkID = findNetworkInExistingConfig(wifiManager, config.SSID);
if (foundNetworkID != null) {
Log.i(TAG, "Removing old configuration for network " + config.SSID);
wifiManager.removeNetwork(foundNetworkID);
wifiManager.saveConfiguration();
}
int networkId = wifiManager.addNetwork(config);
if (networkId >= 0) {
// Try to disable the current network and start a new one.
if (wifiManager.enableNetwork(networkId, true)) {
Log.i(TAG, "Associating to network " + config.SSID);
wifiManager.saveConfiguration();
} else {
Log.w(TAG, "Failed to enable network " + config.SSID);
}
} else {
Log.w(TAG, "Unable to add network " + config.SSID);
}
}
示例3: connectToKnownWifi
import android.net.wifi.WifiManager; //導入方法依賴的package包/類
public static void connectToKnownWifi(Context context, String ssid) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
}
}
}
示例4: disableAllButOne
import android.net.wifi.WifiManager; //導入方法依賴的package包/類
private static boolean disableAllButOne(@NonNull final WifiManager wifiManager, @Nullable final WifiConfiguration config) {
@Nullable final List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks();
if (configurations == null || config == null || configurations.isEmpty())
return false;
boolean result = false;
for (WifiConfiguration wifiConfig : configurations)
if (wifiConfig.networkId == config.networkId)
result = wifiManager.enableNetwork(wifiConfig.networkId, true);
else
wifiManager.disableNetwork(wifiConfig.networkId);
wifiLog("disableAllButOne " + result);
return result;
}
示例5: wifiConnection
import android.net.wifi.WifiManager; //導入方法依賴的package包/類
public static boolean wifiConnection(Context context, String wifiSSID, String password) {
WifiManager wifi = (WifiManager) context.getSystemService("wifi");
String strQuotationSSID = "\"" + wifiSSID + "\"";
WifiInfo wifiInfo = wifi.getConnectionInfo();
if (wifiInfo != null && (wifiSSID.equals(wifiInfo.getSSID()) || strQuotationSSID.equals(wifiInfo.getSSID()))) {
return true;
}
List<ScanResult> scanResults = wifi.getScanResults();
if (scanResults == null || scanResults.size() == 0) {
return false;
}
for (int nAllIndex = scanResults.size() - 1; nAllIndex >= 0; nAllIndex--) {
String strScanSSID = ((ScanResult) scanResults.get(nAllIndex)).SSID;
if (wifiSSID.equals(strScanSSID) || strQuotationSSID.equals(strScanSSID)) {
WifiConfiguration config = new WifiConfiguration();
config.SSID = strQuotationSSID;
config.preSharedKey = "\"" + password + "\"";
config.status = 2;
return wifi.enableNetwork(wifi.addNetwork(config), false);
}
}
return false;
}
示例6: updateNetwork
import android.net.wifi.WifiManager; //導入方法依賴的package包/類
/**
* Update the network: either create a new network or modify an existing network
*
* @param config the new network configuration
*/
private static void updateNetwork(WifiManager wifiManager, WifiConfiguration config) {
Integer foundNetworkID = findNetworkInExistingConfig(wifiManager, config.SSID);
if (foundNetworkID != null) {
Log.i(TAG, "Removing old configuration for network " + config.SSID);
wifiManager.removeNetwork(foundNetworkID);
wifiManager.saveConfiguration();
}
int networkId = wifiManager.addNetwork(config);
if (networkId >= 0) {
// Try to disable the current network and start a new one.
if (wifiManager.enableNetwork(networkId, true)) {
Log.i(TAG, "Associating to network " + config.SSID);
wifiManager.saveConfiguration();
} else {
Log.w(TAG, "Failed to enable network " + config.SSID);
}
} else {
Log.w(TAG, "Unable to add network " + config.SSID);
}
}
示例7: connectToConfiguredNetwork
import android.net.wifi.WifiManager; //導入方法依賴的package包/類
private static boolean connectToConfiguredNetwork(@NonNull WifiManager wifiManager, @Nullable WifiConfiguration config, boolean reassociate) {
if (config == null)
return false;
if (Build.VERSION.SDK_INT >= 23)
return disableAllButOne(wifiManager, config) && (reassociate ? wifiManager.reassociate() : wifiManager.reconnect());
int oldPri = config.priority;
// Make it the highest priority.
int newPri = getMaxPriority(wifiManager) + 1;
if (newPri > MAX_PRIORITY) {
newPri = shiftPriorityAndSave(wifiManager);
config = ConfigSecurities.getWifiConfiguration(wifiManager, config);
if (config == null)
return false;
}
// Set highest priority to this configured network
config.priority = newPri;
int networkId = wifiManager.updateNetwork(config);
if (networkId == -1)
return false;
// Do not disable others
if (!wifiManager.enableNetwork(networkId, false)) {
config.priority = oldPri;
return false;
}
if (!wifiManager.saveConfiguration()) {
config.priority = oldPri;
return false;
}
// We have to retrieve the WifiConfiguration after save.
config = ConfigSecurities.getWifiConfiguration(wifiManager, config);
return config != null && disableAllButOne(wifiManager, config) && (reassociate ? wifiManager.reassociate() : wifiManager.reconnect());
}
示例8: reEnableNetworkIfPossible
import android.net.wifi.WifiManager; //導入方法依賴的package包/類
public static boolean reEnableNetworkIfPossible(@NonNull final WifiManager wifiManager, @Nullable final ScanResult scanResult) {
@Nullable final List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks();
if (configurations == null || scanResult == null || configurations.isEmpty())
return false;
boolean result = false;
for (WifiConfiguration wifiConfig : configurations)
if (Objects.equals(scanResult.BSSID, wifiConfig.BSSID) && Objects.equals(scanResult.SSID, trimQuotes(wifiConfig.SSID))) {
result = wifiManager.enableNetwork(wifiConfig.networkId, true);
break;
}
wifiLog("reEnableNetworkIfPossible " + result);
return result;
}
示例9: doInBackground
import android.net.wifi.WifiManager; //導入方法依賴的package包/類
@Override
protected Void doInBackground(Void... params) {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager = (WifiManager) getBaseContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
wifiManager.startScan();
}
int netId = wifiManager.addNetwork(conf);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Joined to " + networkSSID, Toast.LENGTH_SHORT).show();
System.out.println("SUCCESS ");
}
});
return null;
}
示例10: connectToSocketWifi
import android.net.wifi.WifiManager; //導入方法依賴的package包/類
/**
* 連接指定
*
* @param manager
* @param wifiSSID
* @return
*/
public static boolean connectToSocketWifi(WifiManager manager, String wifiSSID) {
LogUtils.i("要連接的socket wifi====>" + wifiSSID);
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = "\"" + wifiSSID + "\"";
wifiConfiguration.allowedKeyManagement.set(KeyMgmt.NONE);
wifiConfiguration.wepKeys[0] = "\"" + "\""; //小米手機MIUI7/華為EMUI4.1 需要webKey
int networkId = manager.addNetwork(wifiConfiguration);
if (networkId != -1) {
manager.enableNetwork(networkId, true);
e("連接設備成功");
return true;
} else {
e("第一次連接失敗,嘗試第二次。");
WifiConfiguration wifiConfiguration2 = new WifiConfiguration();
wifiConfiguration2.SSID = "\"" + wifiSSID + "\"";
//wifiConfiguration.wepKeys[0] = "\"" + "\"";//去掉webKey //小米手機MIUI8不能有webKey
wifiConfiguration2.allowedKeyManagement.set(KeyMgmt.NONE);
networkId = manager.addNetwork(wifiConfiguration2);
if (networkId != -1) {
manager.enableNetwork(networkId, true);
e("連接設備成功");
return true;
}
e("連接設備失敗");
}
return false;
}
示例11: connect
import android.net.wifi.WifiManager; //導入方法依賴的package包/類
public void connect(String ssid, String pwd, WiFiStaConnectCallback callback){
if(callback == null){
return;
}
mCallback = callback;
mSSID = ssid;
mPWD = pwd;
WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration config = new WifiConfiguration();
config.SSID = "\"" + mSSID + "\"";
if(pwd.equals("")){
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}
else{
config.preSharedKey = "\"" + mPWD + "\"";
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
}
config.status = WifiConfiguration.Status.ENABLED;
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
if(!wifiManager.isWifiEnabled()){
wifiManager.setWifiEnabled(true);
}
wifiManager.startScan();
int networkId = wifiManager.addNetwork(config);
wifiManager.disconnect();
wifiManager.enableNetwork(networkId, true);
wifiManager.reconnect();
}
示例12: connectToAP
import android.net.wifi.WifiManager; //導入方法依賴的package包/類
public int connectToAP(Context context, String networkSSID, String networkPasskey) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
for (ScanResult result : wifiManager.getScanResults()) {
if (result.SSID.equals(networkSSID)) {
String securityMode = getScanResultSecurity(result);
WifiConfiguration wifiConfiguration = createAPConfiguration(networkSSID, networkPasskey, securityMode);
int res = wifiManager.addNetwork(wifiConfiguration);
Log.d(TAG, "# addNetwork returned " + res);
boolean b = wifiManager.enableNetwork(res, true);
Log.d(TAG, "# enableNetwork returned " + b);
wifiManager.setWifiEnabled(true);
boolean changeHappen = wifiManager.saveConfiguration();
if (res != -1 && changeHappen) {
Log.d(TAG, "# Change happen: " + networkSSID);
} else {
Log.d(TAG, "# Change NOT happen");
}
return res;
}
}
return -1;
}
示例13: connectWifi
import android.net.wifi.WifiManager; //導入方法依賴的package包/類
@ProtoMethod(description = "Connect to mContext given Wifi network with mContext given 'wpa', 'wep', 'open' type and mContext password", example = "")
@ProtoMethodParam(params = {"ssidName", "type", "password"})
public void connectWifi(String networkSSID, String type, String networkPass) {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\""; // Please note the quotes. String
// should contain ssid in quotes
if (type.equals("wep")) {
// wep
conf.wepKeys[0] = "\"" + networkPass + "\"";
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
} else if (type.equals("wpa")) {
// wpa
conf.preSharedKey = "\"" + networkPass + "\"";
} else if (type.equals("open")) {
// open
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}
WifiManager wifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
}
示例14: connectToNtutcc
import android.net.wifi.WifiManager; //導入方法依賴的package包/類
public static boolean connectToNtutcc(Context context) {
try {
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + NTUTCC_SSID + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
if (i.SSID != null && i.SSID.equals("\"" + NTUTCC_SSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
return true;
}
}
} catch (Exception e) {
}
return false;
}
示例15: reenableAllHotspots
import android.net.wifi.WifiManager; //導入方法依賴的package包/類
static void reenableAllHotspots(@NonNull WifiManager wifi) {
final List<WifiConfiguration> configurations = wifi.getConfiguredNetworks();
if (configurations != null && !configurations.isEmpty())
for (final WifiConfiguration config : configurations)
wifi.enableNetwork(config.networkId, false);
}