java - Api 级别 29 及以上的 Android 点对点 wifi 连接问题
问题描述
我在 API 级别 29 及更高级别面临 Android 点对点 WIFI 连接问题,它在 API 级别低于 29 时工作正常
这里我分享代码
public static Task<Boolean> ConnectToWifi( String networkSSID, String password,Context context ){
final TaskCompletionSource<Boolean> tcs = new TaskCompletionSource<>();
try {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\""; // Please note the quotes. String should contain SSID in quotes
conf.preSharedKey = "\"" + password + "\"";
conf.status = WifiConfiguration.Status.ENABLED;
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
Log.d("connecting", conf.SSID + " " + conf.preSharedKey);
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
Log.d("after connecting", conf.SSID + " " + conf.preSharedKey);
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();
Log.d("re connecting", i.SSID + " " + conf.preSharedKey);
break;
}
}
//WiFi Connection success, return true
tcs.setResult(true);
} catch (Exception ex) {
System.out.println(Arrays.toString(ex.getStackTrace()));
Toast.makeText(context, Arrays.toString(ex.getStackTrace()), Toast.LENGTH_SHORT).show();
tcs.setResult(false);
}
return tcs.getTask();
}
我已经搜索但找不到任何解决方案,请有人帮我解决问题。谢谢
解决方案
推荐阅读
- python - Matplotlib 中散点图的不同标签
- php - 为什么将集合集合展平为所有数组值的数组?
- python - 错误:命令错误退出状态 1:python setup.py egg_info 检查日志以获取完整命令输出,同时 pip 安装遗传算法
- julia - 如何在 Julia 中制作 inf 矩阵?
- android - 无法在 android 11 中捕获图像
- python - 使用 Python Jenkins 我向 xml 文件添加了新参数,但它不更新配置
- reactjs - 我如何从 React 应用程序“外部化”material-ui
- shopify - 整个页面顶部的剖面层
- drag-and-drop - vuetify-如何在文本区域内更改 vuetify 中的箭头颜色和图标大小?
- javascript - 尝试使用 Contentful 将帖子列表添加到我的主页而不是 Gatsby 中的单独页面