gpt4 book ai didi

用于连接 Wifi 网络的 Android API

转载 作者:行者123 更新时间:2023-12-05 00:15:55 59 4
gpt4 key购买 nike

我完全迷失在 WiFi API 的版本中。我想以编程方式连接到已配置的 WiFi 网络。如这个问题所述: Connect to wifi network Android programmatically

我在 Android 10 上开发并希望编写也与旧 Android 版本兼容的代码。

在我的 android 10 上,描述的代码不起作用。我需要什么代码才能在 Android 10 上实现该功能?

如何编写也可以在我的其他 Android 9 手机上运行的应用程序?

问候于尔根

最佳答案

从 Android Q 开始,连接 Wifi 网络有了很大的变化。

首先,您正在使用的代码或@matdev 提到的使用 WifiManager 中的 API public int addNetwork (WifiConfiguration config) 在 Android 10 中已弃用,并将返回 -1作为网络 ID。

从 Android Q 开始,建议使用两个类来连接 Wifi。但它们各有优缺点。

1. WifiNetworkSpecifier

WifiUtil 库中的代码示例

WifiNetworkSpecifier.Builder wifiNetworkSpecifierBuilder = new WifiNetworkSpecifier.Builder()
.setSsid(scanResult.SSID)
.setBssid(MacAddress.fromString(scanResult.BSSID));

final String security = ConfigSecurities.getSecurity(scanResult);

ConfigSecurities.setupWifiNetworkSpecifierSecurities(wifiNetworkSpecifierBuilder, security, password);


NetworkRequest networkRequest = new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.setNetworkSpecifier(wifiNetworkSpecifierBuilder.build())
.build();

// not sure, if this is needed
if (networkCallback != null) {
connectivityManager.unregisterNetworkCallback(networkCallback);
}

networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(@NonNull Network network) {
super.onAvailable(network);

wifiLog("AndroidQ+ connected to wifi ");

// bind so all api calls are performed over this new network
connectivityManager.bindProcessToNetwork(network);
}

@Override
public void onUnavailable() {
super.onUnavailable();

wifiLog("AndroidQ+ could not connect to wifi");
}
};

connectivityManager.requestNetwork(networkRequest, networkCallback);

我对这个实现的观察是 - 它更像是 P2P 通信,此时来自同一设备的其他应用程序无法使用连接的 WiFi 网络的互联网

2. WifiNetworkSuggestion 

来自 developer.android.com 的代码示例

final WifiNetworkSuggestion suggestion1 =
new WifiNetworkSuggestion.Builder()
.setSsid("test111111")
.setIsAppInteractionRequired(true) // Optional (Needs location permission)
.build();

final WifiNetworkSuggestion suggestion2 =
new WifiNetworkSuggestion.Builder()
.setSsid("test222222")
.setWpa2Passphrase("test123456")
.setIsAppInteractionRequired(true) // Optional (Needs location permission)
.build();

final WifiNetworkSuggestion suggestion3 =
new WifiNetworkSuggestion.Builder()
.setSsid("test333333")
.setWpa3Passphrase("test6789")
.setIsAppInteractionRequired(true) // Optional (Needs location permission)
.build();

final PasspointConfiguration passpointConfig = new PasspointConfiguration();
// configure passpointConfig to include a valid Passpoint configuration

final WifiNetworkSuggestion suggestion4 =
new WifiNetworkSuggestion.Builder()
.setPasspointConfig(passpointConfig)
.setIsAppInteractionRequired(true) // Optional (Needs location permission)
.build();

final List<WifiNetworkSuggestion> suggestionsList =
new ArrayList<WifiNetworkSuggestion> {{
add(suggestion1);
add(suggestion2);
add(suggestion3);
add(suggestion4);
}};

final WifiManager wifiManager =
(WifiManager) context.getSystemService(Context.WIFI_SERVICE);

final int status = wifiManager.addNetworkSuggestions(suggestionsList);
if (status != WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
// do error handling here…
}

// Optional (Wait for post connection broadcast to one of your suggestions)
final IntentFilter intentFilter =
new IntentFilter(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION);

final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals(
WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION)) {
return;
}
// do post connect processing here...
}
};
context.registerReceiver(broadcastReceiver, intentFilter);

我对上述实现的观察是,当您调用 wifiManager.addNetworkSuggestions 时,它会返回成功并向用户显示连接通知。如果用户接受,设备将连接到 WiFi 网络,其他应用程序可以使用互联网。但是,如果用户断开网络连接并且您再次调用 wifiManager.addNetworkSuggestions,它将返回 WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_DUPLICATE 错误。

看起来这个 API 只是提供了设备可以自动连接的网络的建议列表。但连接将由操作系统决定。

但如果您真的需要一个解决方案,可以使用一种未记录的方式使用来自 Android 源的默认 Wifi QR 码扫描仪,它可以检测 QR 码方案 Zxing 和 DPP。

这是一个代码示例:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == REQUEST_CODE_WIFI_QR_SCANNER && resultCode == RESULT_OK)
{
//WIFI Connection is Successful
}
else
{
//.......
}
}

@RequiresApi(api = Build.VERSION_CODES.Q)
private void startWifiQRCodeScanner(Context context)
{
final String INTENT_ACTION_WIFI_QR_SCANNER = "android.settings.WIFI_DPP_ENROLLEE_QR_CODE_SCANNER";
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

if(wifiManager.isEasyConnectSupported())
{
final Intent intent = new Intent(INTENT_ACTION_WIFI_QR_SCANNER);
startActivityForResult(intent, REQUEST_CODE_WIFI_QR_SCANNER);
}
}

关于用于连接 Wifi 网络的 Android API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63884367/

59 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com