提问人:Manuel Lucas 提问时间:11/9/2023 更新时间:11/13/2023 访问量:72
Android 7 不接受 WifiConfiguration
WifiConfiguration is not been accepted by Android 7
问:
我正在开发一些应用程序,我尝试创建一个 wifi 接口,并将我的支付设备连接到它。
我的代码在 Android 8 (A920 Pro) 中有效,但在 Android 7 (A920) 上不起作用:
所有逻辑都由我的 viewModel 管理,如下所示:
SharedViewModel.kt
@HiltViewModel
class SharedViewModel @Inject constructor(@ApplicationContext context: Context) : ViewModel() {
private val wifiManager: WifiManager =
context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
private val _isLoading: MutableStateFlow<Boolean> = MutableStateFlow(false)
val isLoading: StateFlow<Boolean> = _isLoading.asStateFlow()
fun launchWifiConfiguration(context: Context) {
viewModelScope.launch(Dispatchers.IO) {
_isLoading.emit(true)
if (!wifiManager.isWifiEnabled) {
wifiManager.isWifiEnabled = true
}
if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return@launch
}
val wifiConfiguration = createWifiConfiguration()
try {
connectWifi(
context = context,
wifiConfiguration = wifiConfiguration
).also {
_isLoading.emit(false)
}
} catch (e: Exception) {
_isLoading.emit(false)
}
}
}
private fun createWifiConfiguration(): WifiConfiguration {
/**
* Set the following wifi params:
* Seguridad: 802.1x EAP
* Método EAP: EAP PEAP
* Autenticación fase 2: MSCHAPv2
* Certificado CA: Sin certificado
* Identidad: ***********
* Identidad anónima: Nada
* Contraseña: ******
*
*/
val wifiEnterpriseConfig = WifiEnterpriseConfig()
val wifiConfiguration = WifiConfiguration()
wifiConfiguration.status = WifiConfiguration.Status.ENABLED
wifiEnterpriseConfig.eapMethod = WifiEnterpriseConfig.Eap.PEAP
wifiEnterpriseConfig.phase2Method = WifiEnterpriseConfig.Phase2.MSCHAPV2
wifiEnterpriseConfig.caCertificate = null
wifiEnterpriseConfig.anonymousIdentity = null
wifiEnterpriseConfig.identity = "\""+BuildConfig.WIFI_USER +"\""
wifiEnterpriseConfig.password = "\""+BuildConfig.WIFI_PASS +"\""
wifiConfiguration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.LEAP)
wifiConfiguration.enterpriseConfig = wifiEnterpriseConfig
wifiConfiguration.SSID = "\""+BuildConfig.WIFI_SSID +"\""
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP)
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X)
wifiConfiguration.preSharedKey ="\""+BuildConfig.WIFI_PASS+"\""
wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP)
wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP)
wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN)
wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA)
return wifiConfiguration
}
private fun connectWifi(
context: Context,
wifiConfiguration: WifiConfiguration,
) {
if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return
}
if(!wifiManager.isWifiEnabled){
wifiManager.isWifiEnabled = true
}
//add the network
val ssid = wifiManager.addNetwork(
wifiConfiguration
)
//Enable all the interface
wifiManager.configuredNetworks.map {
it.status = WifiConfiguration.Status.ENABLED
}
wifiManager.disconnect()
wifiManager.enableNetwork(ssid, true) // this initiates the connection
wifiManager.reconnect()
}
fun getNetworkId(context: Context, ssid: String): Int {
if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return -1
}
val configurations = wifiManager.configuredNetworks
for (config in configurations) {
if (config.SSID != null && config.SSID.equals(ssid, true)) {
return config.networkId
}
}
return -1
}
}
关于我的 MainActivity 类,它包含 UI,非常简单,显示一个圆形加载组件,直到该过程是芬兰语。
MainActivity.kt (英语)
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
private val viewModel: SharedViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setUI()
processWifiConfiguration(applicationContext)
}
private fun processWifiConfiguration(context: Context) {
try {
viewModel.launchWifiConfiguration(context = context)
}catch (e:NullPointerException){
e.printStackTrace()
}
}
private fun setUI() {
setContent {
val viewModel: SharedViewModel = hiltViewModel()
val isLoading by viewModel.isLoading.collectAsState()
val context = LocalContext.current
WifiCepsaManagerTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
if(isLoading){
CenterLoadingText(applicationContext.getString(R.string.wait))
}else{
//Finnish activity
context.startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
this.finish()
}
}
}
}
}
}
@Composable
fun CenterLoadingText(name: String) {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.fillMaxSize()
) {
CircularProgressIndicator()
Text(
text = name,
textAlign = TextAlign.Center
)
}
}
在阅读了大量有关使用 WifiConfiguration 的 android 中的 Wifi 管理后,我得出结论,问题出在 WifiConfiguration 对象未被 Android 7 接受。为了解决这个问题,我尝试删除一些配置参数,例如“allowedPairwiseCiphers”或放置与我家 WiFi 相同的“allowedProtocols”。
我还尝试删除一些我需要的参数,例如“allowedKeyManagement”,但也无法正常工作,此外我需要这种配置。
从几天前开始,我真的陷入了困境,我开始没有选择。
如果你在我之前陷入这样的困境,请提前感谢!
答: 暂无答案
评论