简体   繁体   中英

Manually starting 3G connection in Android, and keeping it on

How do you start the 3G data connection in Android at the same time WiFi is on? I tried

IConnectivityManager.setMobileDataEnabled(enabled); // via reflection

and it works in the emulator, but in my real phone (Droid 2), it briefly turns on then back off again.

From the shell (adb shell), ip link provides the details of the 3G connection:

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

However, it is only available when WiFi is off. When WiFi is on and I try to turn it on manually, it complains the ppp0 device doesn't exist.

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

When I try to list the device, I can't even find it

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

As I understand it's not possible to get 3g and WiFi simultaneously connected without modifying Android platform source code (at least versions 2.3 and 4). The main problem is hardcoded priorities of connections defined in 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>

This config.xml is then read by ConnectivityService which is subscribed to connect/disconnect events. And in connect handler it decides what it should do with other connections:

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-<--------------------------

You could try keeping both active at the same time by modifying your connectivityservice, but I'd advice against it, since it'll most likely destroy your battery life.

See here if you want to give it a try anyway (and make sure you have a backup, obviously)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM