Android Wear应用连接超时

Android Wear app connection timeout

我正在构建一个 Android 佩戴应用程序,它使用 HTTPUrlConnection() 从网页收集一些数据。这在模拟器中工作得很好,但在真实设备上却不行(我用于测试的 Moto 360)。我 运行 本地网络和外部网络上的服务器,但不知何故真实设备以连接超时结束。它也不是服务器,因为我用不同的路由器尝试了不同的计算机、不同的软件和不同的网络。 Android 清单似乎也不错。我做错了什么,或者这可能是一个错误?

try {
        // create the HttpURLConnection
        HttpURLConnection connection;

            connection = (HttpURLConnection) url.openConnection();

        // just want to do an HTTP GET here
        connection.setRequestMethod("GET");

        // uncomment this if you want to write output to this url
        //connection.setDoOutput(true);

        // give it 15 seconds to respond
        connection.setReadTimeout(15 * 1000);
        connection.connect();

        // read the output from the server
        reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        stringBuilder = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line + "\n");
        }
        bodyHtml = stringBuilder.toString();
    } catch (Exception e) {
        e.printStackTrace();
        //throw e;
    } finally {
        // close the reader; this can throw an exception too, so
        // wrap it in another try/catch block.
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="vendor.atestapp" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.type.watch" />
<service android:name=".WearMessageListenerService">
    <intent-filter>
        <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
    </intent-filter>
</service>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.DeviceDefault" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

日志输出:

09-09 00:50:50.177    3664-8913/vendor.atestapp W/System.err﹕ java.net.ConnectException: failed to connect to /192.168.1.39 (port 8080): connect failed: ETIMEDOUT (Connection timed out)
09-09 00:50:50.177    3664-8913/vendor.atestapp W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:124)
09-09 00:50:50.177    3664-8913/vendor.atestapp W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
09-09 00:50:50.177    3664-8913/vendor.atestapp W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
09-09 00:50:50.177    3664-8913/vendor.atestapp W/System.err﹕ at java.net.Socket.connect(Socket.java:882)

找到问题了! HTTPUrlConnection 不适用于 Wear。 Google 禁用它,但你可以完美地编码它。显然这是 Google 的某种政策,但他们忘记在他们的模拟器中禁用它。

备选方案(我现在就是这样做的):在您的 phone 上使用 HTTPUrlConnection,并使用 MessageApi 与您的 Wear 设备通信。

Android 手表将无法通过蓝牙连接到互联网 phone,UrlHTTPConnection 已禁用,因为这不是 Wear 问题,我会 但是 WiFi连接时会自动连接

不幸的是,2015 年中期之前的大多数设备不支持 WiFi :-( 更不幸的是,Android 手持设备的 Wear 应用程序不会将手表的蓝牙与世界各地的互联网连接起来。

UrlHTTPConnection 已启用,因为它不是 Wear 问题。罪魁祸首是手持设备上的 Wear-App。

显然,它不可行 "for life" 但出于好奇,如果您通过 WiFi 连接设备并禁用 phone [=18] 上的蓝牙=](出于功率考虑,它总是更喜欢BT)它会奇迹般地开始工作。