从我的Android应用程序发送/接收消息到chrome

时间:2018-04-05 20:53:51

标签: android google-chrome web mobile chrome-remote-debugging

我正在尝试在android上运行我的android应用程序和chrome之间的连接。我正在使用LocalSocket进行套接字通信,如下所示:

        JSONObject request = new JSONObject();
        request.put("method", "Page.reload");
        LocalSocket s = new LocalSocket();
    try {
        s.connect(new LocalSocketAddress("chrome_devtools_remote", LocalSocketAddress.Namespace.ABSTRACT));
        Log.i("Chrome: ", "After local socket connect");
        //StringBuilder request = new StringBuilder().append("GET /json HTTP/1.0\r\n");
        OutputStream oss = s.getOutputStream();
        byte[] buffer = jo.toString().getBytes("utf-8");
        oss.write(buffer, 0, buffer.length);
        Log.i("Chrome: ", "outbuf size " + Integer.toString(s.getSendBufferSize()));
        InputStream iss = s.getInputStream();
        Integer i;
        String res = "";
        while ((i = iss.read()) != -1) {
            res += i.toString();
        }
        Log.i("Chrome: ", res);
    } catch (Exception e) {
        Log.e("Error", "Connecting Local Socket "+e.toString());
    }

我能够在Chrome和我的应用之间建立连接,但我无法发送和接收消息以自动化Chrome中的页面加载。

1 个答案:

答案 0 :(得分:2)

Chrome不允许每个应用程序都连接到devtool套接字。 确保连接的应用程序使用相同的密钥签名,或者用户为 root shell 。可以在here中找到此安全逻辑的源代码。

万一安全检查失败,它会显示以下消息,您应该在logcat中看到该消息:

  

DevTools:连接尝试来自

相关问题