I am trying to communicate with Chrome developer tools using their socket protocol inside Android. The USB debugging option in chrome is enabled, and if I do adb forward from my computer, I can interact with the socket. But I need to access it programmatically inside the phone.
Here is the code:
package something;
import java.lang.StringBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import java.io.BufferedOutputStream;
import java.io.BufferedInputStream;
public class SomeService extends Service {
private final String TAG = SomeService.class.toString();
private int cnt = 0;
public static String SOCKET_ADDRESS = "chrome_devtools_remote";
private String message = "/json";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "service started");
Thread t = new Thread(new Runnable() {
public void run()
{
try {
LocalSocket receiver = new LocalSocket(LocalSocket.SOCKET_STREAM);
receiver.connect(new LocalSocketAddress(SOCKET_ADDRESS, LocalSocketAddress.Namespace.ABSTRACT));
StringBuilder request = new StringBuilder().append("GET /json HTTP/1.0\r\n");
BufferedOutputStream outbuf = new BufferedOutputStream(receiver.getOutputStream());
BufferedInputStream inbuf = new BufferedInputStream(receiver.getInputStream());
byte[] buffer = request.toString().getBytes("utf-8");
Log.d(TAG, "outbuf size " + Integer.toString(receiver.getSendBufferSize()));
while (true) {
Log.d(TAG, "sending " + request.toString());
outbuf.write(buffer, 0, buffer.length);
outbuf.flush();
Log.d(TAG, "sent!");
byte[] output = new byte[1000];
int read = inbuf.read();
int size = 0;
int capacity = 0;
byte[] bytes = new byte[capacity];
while (read != -1) {
Log.d(TAG, "recieving");
capacity = (capacity * 3)/2 + 1;
byte[] copy = new byte[capacity];
System.arraycopy(bytes, 0, copy, 0, bytes.length);
bytes = copy;
bytes[size++] = (byte)read;
read = inbuf.read();
}
Log.d(TAG, new String(bytes));
try {
Thread.sleep(3000);
} catch(Exception e) {
}
}
} catch (IOException e) {
Log.e(getClass().getName(), e.getMessage(),e);
}
}
});
t.start();
return Service.START_STICKY;
}
}
This always results in an error:
Starting service: Intent { cmp=something/.MyService (has extras) }
D/class something.MyService(18717): service started
D/class something.MyService(18717): outbuf size 163840
D/class something.MyService(18717): sending GET /json HTTP/1.0
E/something.MyService$1(18717): Broken pipe
E/something.MyService$1(18717): java.io.IOException: Broken pipe
E/something.MyService$1(18717): at android.net.LocalSocketImpl.writeba_native(Native Method)
E/something.MyService$1(18717): at android.net.LocalSocketImpl.access$600(LocalSocketImpl.java:33)
E/something.MyService$1(18717): at android.net.LocalSocketImpl$SocketOutputStream.write(LocalSocketImpl.java:134)
E/something.MyService$1(18717): at java.io.BufferedOutputStream.flushInternal(BufferedOutputStream.java:185)
E/something.MyService$1(18717): at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:85)
E/something.MyService$1(18717): at something.MyService$1.run(RTCService.java:49)
E/something.MyService$1(18717): at java.lang.Thread.run(Thread.java:841)
The socket is definitely open, which I check with the isConnected()socket object method . Any ideas what I'm doing wrong?
Munim source
share