Adding get devices method

This commit is contained in:
Samuel Carlsson 2013-07-25 21:33:53 +02:00
parent 87ee4c5d83
commit 2799f05250
2 changed files with 31 additions and 5 deletions

View File

@ -22,19 +22,38 @@ public class JadbConnection {
send("host:version"); send("host:version");
verifyResponse(); 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 public void close() throws IOException
{ {
_socket.close(); _socket.close();
} }
private void verifyResponse() throws IOException, JadbException { 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()); DataInput reader = new DataInputStream(_socket.getInputStream());
byte[] responseBuffer = new byte[4]; byte[] responseBuffer = new byte[length];
reader.readFully(responseBuffer); reader.readFully(responseBuffer);
String response = new String(responseBuffer, Charset.forName("utf-8")); 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) { private String getCommandLength(String command) {
return String.format("%04x", Integer.valueOf(command.length())); return String.format("%04x", Integer.valueOf(command.length()));

View File

@ -7,8 +7,15 @@ import se.vidstige.jadb.JadbConnection;
public class JadbTestCases { public class JadbTestCases {
@Test @Test
public void test() throws Exception { public void testGetHostVersion() throws Exception {
JadbConnection jadb = new JadbConnection(); JadbConnection jadb = new JadbConnection();
jadb.getHostVersion(); jadb.getHostVersion();
} }
@Test
public void testGetDevices() throws Exception
{
JadbConnection jadb = new JadbConnection();
jadb.getDevices();
}
} }