原本很早就想把这些东西写文章保留下来,但是只怪自己真的太懒啦,2年后的今天才开始整理当时工作时的思路
背景
- 借助Android 4G ,WiFi 双网络模式可以互补,大大的提高应用的稳定性
- 作为一款工具类产品,WiFi,4G流量,使我们的业务重心,如何将4G & WiFi
彻底打通,为用户提供更沉浸式的服务体验,使我们一直以来的最求
思路
- 非常感谢国外的一款产品,在实现上为我们提供了一个入门的思路 speedify
no bb show my code
如果有人感兴趣这个实现过程,可以单独来交流
具体实现方案
- 准备工作:
- 此方案借助了VPN来实现本地流量的劫持,并控制发送网络通道
- 因为Android版本的差异性,实现方案也将按照高低不同版本来设计
- 因为现在手上已经没有那么多资源,所以更多的是代码和思路讲解,如果你感兴趣可能需要自己做一部分前置工作
eg: VPN 搭建,后台配套服务
基本实现思路
API < 6.0
- 启动Android手机高速网卡,在启动成功后将高速网卡 和 VPN出口IP绑定
public class SetupHIPRIEnableTask extends TimerTask {
private static final String TAG = "SetupHIPRIEnableTask\t";
private Context mContext;
public SetupHIPRIEnableTask(Context context) {
mContext = context;
}
@Override
public void run() {
try {
DLog.i(TAG + "Polling mobile network to make sure it stays up");
ConnectivityManager connectivityManager = (ConnectivityManager) mContext
.getSystemService(CONNECTIVITY_SERVICE);
if (connectivityManager == null) {
Log.e(TAG, "ConnectivityManager is null, cannot try to force a mobile connection");
} else if (connectivityManager.getNetworkInfo(5) == null) {
Log.e(TAG, "Mobile Interface Not Found.");
} else {
int startUsingNetworkFeature = connectivityManager.startUsingNetworkFeature(0, "enableHIPRI");
Log.d(TAG, "startUsingNetworkFeature for enableHIPRI result: " + startUsingNetworkFeature);
if (-1 == startUsingNetworkFeature) {
Log.e(TAG, "Wrong result of startUsingNetworkFeature, maybe problems");
}
if (connectivityManager.getNetworkInfo(5).getState().compareTo(NetworkInfo.State.CONNECTED) == 0) {
Log.d(TAG, "Mobile network is connected");
} else {
Log.d(TAG, "Mobile network is not connected");
}
}
requestRouteToHost();
} catch (Exception e) {
Log.e(TAG, "Hit error in KeepMobileAliveTask ", e);
}
}
/**
* 等待 : TYPE_MOBILE_HIPRI类型的网络建立连接后
* <p/>
* deliver traffic to the specified host via the specified network interface
*/
private void requestRouteToHost() {
String ip = AuthDataManager.getInstance().getAuthData().getIp();
int i = AppUtil.convertIpToInt(ip);
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(CONNECTIVITY_SERVICE);
if (connectivityManager == null) {
DLog.i(TAG + "ConnectivityManager is null, cannot try to force a mobile connection");
} else if (connectivityManager.getNetworkInfo(5) == null) {
DLog.i(TAG + "Mobile Interface Not Found.");
} else {
int i2 = 0;
while (i2 < 20) {
try {
if (connectivityManager.getNetworkInfo(5).getState().compareTo(NetworkInfo.State.CONNECTED) == 0) {
break;
}
Thread.sleep(1000);
i2++;
} catch (InterruptedException ignored) {
}
}
DLog.i(TAG + "requestRouteToHost result: " + connectivityManager.requestRouteToHost(5, i));
}
}
}
API >= 6.0
- 通过Android标准API在WiFi连接的情况下,请求4G网络通道,
- 当4G网络通道成功后,Android会回调一个networkId ,
- 通过NDK将此networkId与VPN的上网了进程绑定,从而实现走VPN的数据流量都通过4G 网络
public void requestWorkNetForHigh(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
DLog.i("ConnectivityManager is null, cannot try to force a mobile connection");
return;
}
DLog.i("post50MobileEnable, requesting Cellular network");
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addTransportType(0);
builder.addCapability(12);
NetworkRequest build = builder.build();
unregisterNetworkCallback();
try {
if (mNetWorkCallBack == null) {
mNetWorkCallBack = new MNetWorkCallBack();
}
DLog.i("Requesting Connectivity Manager");
cm.requestNetwork(build, mNetWorkCallBack);
} catch (SecurityException e) {
DLog.i("com.speedify.speedifyAndroid.MobileController Cannot write settings. Requesting Permission");
} catch (Exception e2) {
DLog.e("Unknown Exception", e2);
e2.printStackTrace();
}
}
private class MNetWorkCallBack extends ConnectivityManager.NetworkCallback {
@Override
public void onAvailable(Network network) {
DLog.i(TAG + "NetWorkCallBack " + network);
super.onAvailable(network);
}
}
POC当时的技术结论:
--未完待补充