在Android中手动启动3G连接并保持开启状态

时间:2012-03-01 03:35:19

标签: android linux networking android-networking

如何在启动WiFi的同时启动Android中的3G数据连接?我试过了

IConnectivityManager.setMobileDataEnabled(enabled); // via reflection

它可以在模拟器中运行,但在我的真实手机(Droid 2)中,它会短暂打开然后再次关闭。

在shell(adb shell)中,ip link提供了3G连接的详细信息:

15: ppp0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 3 link/ppp

但是,仅在WiFi关闭时才可用。当WiFi打开并且我尝试手动打开它时,它会抱怨ppp0设备不存在。

bash-3.2# ip link set ppp0 up
ip link set ppp0 up
Cannot find device "ppp0"

当我尝试列出设备时,我甚至找不到它

bash-3.2# ls /dev/ppp*
ls /dev/ppp*
/dev/ppp

3 个答案:

答案 0 :(得分:2)

据我了解,如果不修改Android平台源代码(至少版本2.3和4),就不可能同时连接3g和WiFi。主要问题是frameworks/base/core/res/res/values/config.xml中定义的硬编码连接优先级:

<!-- This string array should be overridden by the device to present a list of network
attributes. This is used by the connectivity manager to decide which networks can coexist
based on the hardware -->
    <!-- An Array of "[Connection name],[ConnectivityManager connection type],
[associated radio-type],[priority] -->
 <!--                   ^^^^^^^^^^---------- Connection priority -->

    <string-array translatable="false" name="networkAttributes">
        <item>"wifi,1,1,1"</item>
        <item>"mobile,0,0,0"</item>
        <item>"mobile_mms,2,0,2"</item>
        <item>"mobile_supl,3,0,2"</item>
        <item>"mobile_hipri,5,0,3"</item>
    </string-array>

然后ConnectivityService读取此config.xml,订阅连接/断开连接事件。在连接处理程序中,它决定了它应该对其他连接做什么:

private void handleConnect(NetworkInfo info) {

        //------------8-<--------------------------

        // if this is a default net and other default is running
        // kill the one not preferred
        if (mNetAttributes[type].isDefault()) {
            if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
                if ((type != mNetworkPreference &&
                        mNetAttributes[mActiveDefaultNetwork].mPriority >
                        //                                    ^^^^^^^^^ --- From config.xml
                        mNetAttributes[type].mPriority) ||
                        //                   ^^^^^^^^^-------- From config.xml
                        mNetworkPreference == mActiveDefaultNetwork) {
                        // don't accept this one
                        if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
                                "to torn down network " + info.getTypeName());
                        teardown(thisNet);
                        return;
          //------------8-<--------------------------

答案 1 :(得分:0)

您可以尝试通过修改连接服务来同时保持两者都处于活动状态, 但我建议反对它,因为它很可能会破坏你的电池续航时间。

如果您想尝试一下,请参阅here(并确保您有备份,显然)

答案 2 :(得分:0)

如果您尝试连接到特定计算机,可以尝试ConnectivityManager.requestRouteToHost