diff --git a/src/se/vidstige/jadb/JadbConnection.java b/src/se/vidstige/jadb/JadbConnection.java index fd2de35..289c0a1 100644 --- a/src/se/vidstige/jadb/JadbConnection.java +++ b/src/se/vidstige/jadb/JadbConnection.java @@ -22,19 +22,38 @@ public class JadbConnection { send("host:version"); verifyResponse(); } - + + public void getDevices() throws IOException, JadbException + { + send("host:devices"); + verifyResponse(); + String body = readString(); + System.out.println(body); + } + + private String readString() throws IOException { + String encodedLength = readString(4); + int length = Integer.parseInt(encodedLength, 16); + return readString(length); + } + public void close() throws IOException { _socket.close(); } private void verifyResponse() throws IOException, JadbException { + String response = readString(4); + if ("OKAY".equals(response) == false) throw new JadbException("command failed"); + } + + private String readString(int length) throws IOException { DataInput reader = new DataInputStream(_socket.getInputStream()); - byte[] responseBuffer = new byte[4]; + byte[] responseBuffer = new byte[length]; reader.readFully(responseBuffer); String response = new String(responseBuffer, Charset.forName("utf-8")); - if ("OKAY".equals(response) == false) throw new JadbException("command failed"); - } + return response; + } private String getCommandLength(String command) { return String.format("%04x", Integer.valueOf(command.length())); diff --git a/test/se/vidstige/jadb/test/JadbTestCases.java b/test/se/vidstige/jadb/test/JadbTestCases.java index da48ed7..75c5b47 100644 --- a/test/se/vidstige/jadb/test/JadbTestCases.java +++ b/test/se/vidstige/jadb/test/JadbTestCases.java @@ -7,8 +7,15 @@ import se.vidstige.jadb.JadbConnection; public class JadbTestCases { @Test - public void test() throws Exception { + public void testGetHostVersion() throws Exception { JadbConnection jadb = new JadbConnection(); jadb.getHostVersion(); } + + @Test + public void testGetDevices() throws Exception + { + JadbConnection jadb = new JadbConnection(); + jadb.getDevices(); + } }