gpt4 book ai didi

android - 在没有网络访问的本地 wifi 上强制 Android 使用 3G

转载 作者:可可西里 更新时间:2023-11-01 19:07:21 28 4
gpt4 key购买 nike

我有一个无线局域网设置,但无法访问互联网。只是连接到它的各种其他本地 wifi 设备。 DHCP 配置为不返回网关或 DNS 服务器。只有 IP 和网络掩码。

当我将我的安卓连接到这个 wifi AP 时,它连接正常,但手机上的所有互联网连接都停止工作。

我希望,由于 wifi 没有网关设置,因此 android 应该意识到互联网无法通过该连接,而是应该通过 5 格的 3G 连接进行路由。

我也试过在安卓手机上设置一个静态IP,但这没有帮助。

此设置的主要原因是 android 设备可以将此远程网络上的数据传输到基于 internet 的服务器,因为它可以毫无问题地连接到本地设备。然而,一旦设置了 wifi,3G 端就坏了。

关于如何解决这个问题有什么想法吗?

最佳答案

经过一些编码和测试后,我合并了 Squonk 和 this解决方案。这是我创建的类:

package it.helian.exampleprj.network;

import java.net.InetAddress;
import java.net.UnknownHostException;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiManager;
import android.text.TextUtils;
import android.util.Log;

public class NetworkUtils {
private static final String TAG_LOG = "ExamplePrj";

Context context;
WifiManager wifiMan = null;
WifiManager.WifiLock wifiLock = null;

public NetworkUtils(Context context) {
super();
this.context = context;
}

/**
* Enable mobile connection for a specific address
* @param context a Context (application or activity)
* @param address the address to enable
* @return true for success, else false
*/
public boolean forceMobileConnectionForAddress(Context context, String address) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null == connectivityManager) {
Log.d(TAG_LOG, "ConnectivityManager is null, cannot try to force a mobile connection");
return false;
}

//check if mobile connection is available and connected
State state = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState();
Log.d(TAG_LOG, "TYPE_MOBILE_HIPRI network state: " + state);
if (0 == state.compareTo(State.CONNECTED) || 0 == state.compareTo(State.CONNECTING)) {
return true;
}

//activate mobile connection in addition to other connection already activated
int resultInt = connectivityManager.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "enableHIPRI");
Log.d(TAG_LOG, "startUsingNetworkFeature for enableHIPRI result: " + resultInt);

//-1 means errors
// 0 means already enabled
// 1 means enabled
// other values can be returned, because this method is vendor specific
if (-1 == resultInt) {
Log.e(TAG_LOG, "Wrong result of startUsingNetworkFeature, maybe problems");
return false;
}
if (0 == resultInt) {
Log.d(TAG_LOG, "No need to perform additional network settings");
return true;
}

//find the host name to route
String hostName = extractAddressFromUrl(address);
Log.d(TAG_LOG, "Source address: " + address);
Log.d(TAG_LOG, "Destination host address to route: " + hostName);
if (TextUtils.isEmpty(hostName)) hostName = address;

//create a route for the specified address
int hostAddress = lookupHost(hostName);
if (-1 == hostAddress) {
Log.e(TAG_LOG, "Wrong host address transformation, result was -1");
return false;
}
//wait some time needed to connection manager for waking up
try {
for (int counter=0; counter<30; counter++) {
State checkState = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState();
if (0 == checkState.compareTo(State.CONNECTED))
break;
Thread.sleep(1000);
}
} catch (InterruptedException e) {
//nothing to do
}
boolean resultBool = connectivityManager.requestRouteToHost(ConnectivityManager.TYPE_MOBILE_HIPRI, hostAddress);
Log.d(TAG_LOG, "requestRouteToHost result: " + resultBool);
if (!resultBool)
Log.e(TAG_LOG, "Wrong requestRouteToHost result: expected true, but was false");

state = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState();
Log.d(TAG_LOG, "TYPE_MOBILE_HIPRI network state after routing: " + state);

return resultBool;
}

/**
* This method extracts from address the hostname
* @param url eg. http://some.where.com:8080/sync
* @return some.where.com
*/
public String extractAddressFromUrl(String url) {
String urlToProcess = null;

//find protocol
int protocolEndIndex = url.indexOf("://");
if(protocolEndIndex>0) {
urlToProcess = url.substring(protocolEndIndex + 3);
} else {
urlToProcess = url;
}

// If we have port number in the address we strip everything
// after the port number
int pos = urlToProcess.indexOf(':');
if (pos >= 0) {
urlToProcess = urlToProcess.substring(0, pos);
}

// If we have resource location in the address then we strip
// everything after the '/'
pos = urlToProcess.indexOf('/');
if (pos >= 0) {
urlToProcess = urlToProcess.substring(0, pos);
}

// If we have ? in the address then we strip
// everything after the '?'
pos = urlToProcess.indexOf('?');
if (pos >= 0) {
urlToProcess = urlToProcess.substring(0, pos);
}
return urlToProcess;
}

/**
* Transform host name in int value used by {@link ConnectivityManager.requestRouteToHost}
* method
*
* @param hostname
* @return -1 if the host doesn't exists, elsewhere its translation
* to an integer
*/
private int lookupHost(String hostname) {
InetAddress inetAddress;
try {
inetAddress = InetAddress.getByName(hostname);
} catch (UnknownHostException e) {
return -1;
}
byte[] addrBytes;
int addr;
addrBytes = inetAddress.getAddress();
addr = ((addrBytes[3] & 0xff) << 24)
| ((addrBytes[2] & 0xff) << 16)
| ((addrBytes[1] & 0xff) << 8 )
| (addrBytes[0] & 0xff);
return addr;
}

@SuppressWarnings("unused")
private int lookupHost2(String hostname) {
InetAddress inetAddress;
try {
inetAddress = InetAddress.getByName(hostname);
} catch (UnknownHostException e) {
return -1;
}
byte[] addrBytes;
int addr;
addrBytes = inetAddress.getAddress();
addr = ((addrBytes[3] & 0xff) << 24)


| ((addrBytes[2] & 0xff) << 16)
| ((addrBytes[1] & 0xff) << 8 )
| (addrBytes[0] & 0xff);
return addr;
}

public Boolean disableWifi() {
wifiMan = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (wifiMan != null) {
wifiLock = wifiMan.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, "HelianRCAWifiLock");
}
return wifiMan.setWifiEnabled(false);
}

public Boolean enableWifi() {
Boolean success = false;

if (wifiLock != null && wifiLock.isHeld())
wifiLock.release();
if (wifiMan != null)
success = wifiMan.setWifiEnabled(true);
return success;
}
}

这是用法:

使用代码

            boolean mobileRoutingEnabled = checkMobileInternetRouting();

if(!mobileRoutingEnabled) {
networkUtils.disableWifi();

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

networkUtils.forceMobileConnectionForAddress(context, RCA_URL);

if(!mobileRoutingEnabled) {
networkUtils.enableWifi();
}

// This second check is for testing purpose
checkMobileInternetRouting();

return callWebService(RCA_COMPLETE_URL, _plate);

checkMobileInternetRouting 是:

private boolean checkMobileInternetRouting() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

State state = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState();
return 0 == state.compareTo(State.CONNECTED) || 0 == state.compareTo(State.CONNECTING);
}

使用步骤

  1. 检查是否启用了到主机的路由
  2. 如果是,则无论 wifi 是否连接,都进行通信并仅执行第 6 点(第 4 点将仅检查路由是否已启用,而不执行任何相关操作)。否则会暂时禁用 wifi。
  3. 线程休眠约 3 秒,让 3g 连接恢复
  4. 将 3g 路由设置为给定的 url
  5. 重新启用 wifi
  6. 现在即使没有网络访问的 wifi 连接也可以调用给定的 url

结论

这有点 hacky 但工作正常。唯一的问题是这个路由有几秒的超时时间(比如 20-30),迫使你再次执行上面的整个过程。将此超时设置为更高的值会非常好。

关于android - 在没有网络访问的本地 wifi 上强制 Android 使用 3G,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8844846/

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