Android 入门项目(六)Android 的 wifi 开发,flutter 旋转动画
switch (intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN)) {
case WifiManager.WIFI_STATE_ENABLING:
Log.i(TAG, "onReceive: 正在打开 WIFI...");
break;
case WifiManager.WIFI_STATE_ENABLED:
Log.i(TAG, "onReceive: WIFI 已打开");
mCallBackHandler.sendEmptyMessage(WIFI_STATE_ENABLED);
break;
case WifiManager.WIFI_STATE_DISABLING:
Log.i(TAG, "onReceive: 正在关闭 WIFI...");
break;
case WifiManager.WIFI_STATE_DISABLED:
Log.i(TAG, "onReceive: WIFI 已关闭");
mCallBackHandler.sendEmptyMessage(WIFI_STATE_DISABLED);
break;
case WifiManager.WIFI_STATE_UNKNOWN:
default:
Log.i(TAG, "onReceive: WIFI 状态未知!");
break;
}
break;
// WIFI 扫描完成
case WifiManager.SCAN_RESULTS_AVAILABLE_ACTION:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
boolean isUpdated = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false);
Log.i(TAG, "onReceive: WIFI 扫描 " + (isUpdated ? "完成" : "未完成"));
} else {
Log.i(TAG, "onReceive: WIFI 扫描完成");
}
Message scanResultsMessage = Message.obtain();
scanResultsMessage.what = SCAN_RESULTS_UPDATE
D;
scanResultsMessage.obj = wifiManager.getScanResults();
mCallBackHandler.sendMessage(scanResultsMessage);
break;
//WIFI 网络状态变化通知
case WifiManager.NETWORK_STATE_CHANGED_ACTION:
WifiInfo wifiInfo = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
if (null != wifiInfo && wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
String ssid = wifiInfo.getSSID();
Log.i(TAG, "onReceive: 网络连接成功 ssid = " + ssid);
}
break;
//WIFI 连接状态变化的时候
case WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION:
boolean isConnected = intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
Log.i(TAG, "onReceive: SUPPLICANT_CONNECTION_CHANGE_ACTION isConnected = " + isConnected);
break;
//wifi 连接结果通知 WIFI 连接请求状态发生改变
case WifiManager.SUPPLICANT_STATE_CHANGED_ACTION:
// 获取连接状态
SupplicantState supplicantState = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
Message logMessage = Message.obtain();
logMessage.what = WIFI_CONNECT_LOG;
logMessage.obj = supplicantState.toString();
logMessage.obj = supplicantState.toString();
mCallBackHandler.sendMessage(logMessage);
switch (supplicantState) {
case INTERFACE_DISABLED: // 接口禁用
Log.i(TAG, "onReceive: INTERFACE_DISABLED 接口禁用");
break;
case DISCONNECTED:// 断开连接
// Log.i(TAG, "onReceive: DISCONNECTED:// 断开连接");
// break;
case INACTIVE: // 不活跃的
WifiInfo connectFailureInfo = wifiManager.getConnectionInfo();
Log.i(TAG, "onReceive: INACTIVE 不活跃的 connectFailureInfo = " + connectFailureInfo);
if (null != connectFailureInfo) {
Message wifiConnectFailureMessage = Message.obtain();
wifiConnectFailureMessage.what = WIFI_CONNECT_FAILURE;
wifiConnectFailureMessage.obj = connectFailureInfo.getSSID();
mCallBackHandler.sendMessage(wifiConnectFailureMessage);
// 断开连接
int networkId = connectFailureInfo.getNetworkId();
boolean isDisable = wifiManager.disableNetwork(networkId);
boolean isDisconnect = wifiManager.disconnect();
Log.i(TAG, "onReceive: 断开连接 = " + (isDisable && isDisconnect));
}
break;
case SCANNING: // 正在扫描
Log.i(TAG, "onReceive: SCANNING 正在扫描");
break;
case AUTHENTICATING: // 正在验证
Log.i(TAG, "onReceive: AUTHENTICATING: // 正在验证");
break;
case ASSOCIATING: // 正在关联
Log.i(TAG, "onReceive: ASSOCIATING: // 正在关联");
break;
case ASSOCIATED: // 已经关联
Log.i(TAG, "onReceive: ASSOCIATED: // 已经关联");
break;
case FOUR_WAY_HANDSHAKE: // 四次握手
Log.i(TAG, "onReceive: FOUR_WAY_HANDSHAKE:");
break;
case GROUP_HANDSHAKE: // 组握手
Log.i(TAG, "onReceive: GROUP_HANDSHAKE:");
break;
case COMPLETED: // 完成
Log.i(TAG, "onReceive: WIFI_CONNECT_SUCCESS: // 完成");
WifiInfo connectSuccessInfo = wifiManager.getConnectionInfo();
if (null != connectSuccessInfo) {
Message wifiConnectSuccessMessage = Message.obtain();
wifiConnectSuccessMessage.what = WIFI_CONNECT_SUCCESS;
wifiConnectSuccessMessage.obj = connectSuccessInfo.getSSID();
mCallBackHandler.sendMessage(wifiConnectSuccessMessage);
}
break;
case DORMANT: // 休眠
Log.i(TAG, "onReceive: DORMANT:");
break;
case UNINITIALIZED: // 未初始化
Log.i(TAG, "onReceive: UNINITIALIZED: // 未初始化");
break;
case INVALID: // 无效的
Log.i(TAG, "onReceive: INVALID: // 无效的");
break;
default:
break;
}
break;
default:
break;
}
}
}
相关 API 请参阅:[官网 WifiManagerAPI](
)
[](
)六,权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
[](
)七,打开 WIFI
/**
打开 Wifi
*/
public void openWiFi() {
if (!isWifiEnabled() && null != mWifiManager) {
mWifiManager.setWifiEnabled(true);
}
}
[](
)八,关闭 WIFI
/**
关闭 Wifi
*/
public void closeWiFi() {
if (isWifiEnabled() && null != mWifiManager) {
mWifiManager.setWifiEnabled(false);
}
}
[](
)九,扫描附近的 WIFI
/**
扫描附近的 WIFI
*/
public void startScan() {
if (null != mWifiManager) {
mWifiManager.startScan();
}
}
[](
)十,获取 WIFI 列表
/**
获取最近扫描的 WIFI 热点
@return WIFI 热点列表
*/
public List<ScanResult> getScanResults() {
// 得到扫描结果
if (null != mWifiManager) {
return mWifiManager.getScanResults();
}
return null;
}
[](
)十一,通过密码连接到 WIFI
/**
连接到开放网络
@param ssid 热点名
@return 配置是否成功
*/
public boolean connectOpenNetwork(@NonNull String ssid) {
// 获取 networkId
int networkId = setOpenNetwork(ssid);
if (-1 != networkId) {
// 保存配置
boolean isSave = saveConfiguration();
// 连接网络
boolean isEnable = enableNetwork(networkId);
return isSave && isEnable;
}
return false;
}
/**
连接到 WEP 网络
@param ssid 热点名
@param password 密码
@return 配置是否成功
*/
public boolean connectWEPNetwork(@NonNull String ssid, @NonNull String password) {
// 获取 networkId
int networkId = setWEPNetwork(ssid, password);
if (-1 != networkId) {
// 保存配置
boolean isSave = saveConfiguration();
// 连接网络
boolean isEnable = enableNetwork(networkId);
return isSave && isEnable;
}
return false;
}
/**
连接到 WPA2 网络
@param ssid 热点名
@param password 密码
@return 配置是否成功
*/
public boolean connectWPA2Network(@NonNull String ssid, @NonNull String password) {
// 获取 networkId
int networkId = setWPA2Network(ssid, password);
if (-1 != networkId) {
// 保存配置
boolean isSave = saveConfiguration();
// 连接网络
boolean isEnable = enableNetwork(networkId);
return isSave && isEnable;
}
return false;
}
这里就写了三种加密方式的连接,其中 ssid 是扫描到的,password 是单击 ListView 列表弹出 EditText 框输入的。
[](
)十二,直接连接配置过的 WIFI
这里的思路是通过 NetworkId 连接到 WIFI (配置过的网络可以直接获取到 NetworkID,从而不用再输入密码)。代码中采用回调方式来实现,这个具体请看项目代码。
[](
)十三,断开指定 WIFI
/**
断开指定 WIFI
@param netId netId
@return 是否断开
*/
public boolean disconnectWifi(int netId) {
评论