Hello all,

I would like to issue a request to the adbd from within my application
so i can retrieve the framebuffer contents. I've setup a small test
which connects to 127.0.0.1:5037 and sends the "framebuffer:" command.
The response i got was "Got reply 'FAIL', diag='closed'"

public class ScreenProjector extends Activity {
    // Where to find the ADB bridge.
    final static String ADB_HOST = "127.0.0.1"; //$NON-NLS-1$
    final static int ADB_PORT = 5037;
    static final String DEFAULT_ENCODING = "ISO-8859-1"; //$NON-NLS-1$

    static InetAddress sHostAddr;
    static InetSocketAddress sSocketAddr;

        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                Thread.setDefaultUncaughtExceptionHandler(new
TopExceptionHandler(this));

                SocketChannel adbChan = null;

        try {
                        sHostAddr = InetAddress.getByName(ADB_HOST);
                        sSocketAddr = new InetSocketAddress(sHostAddr, 
ADB_PORT);

            adbChan = SocketChannel.open(sSocketAddr);
            adbChan.configureBlocking(false);

            byte[] request = formAdbRequest("framebuffer:"); //$NON-
NLS-1$
            ByteBuffer buf = ByteBuffer.wrap(request, 0,
request.length);
            adbChan.write(buf);

            byte[] reply = new byte[4];
            buf = ByteBuffer.wrap(reply, 0, reply.length);
            adbChan.read(buf);

            boolean readDiagString = true;

            while (readDiagString) {
                // length string is in next 4 bytes
                byte[] lenBuf = new byte[4];
                buf = ByteBuffer.wrap(lenBuf, 0, lenBuf.length);
                adbChan.read(buf);

                String lenStr = replyToString(lenBuf);

                int len;
                try {
                    len = Integer.parseInt(lenStr, 16);
                } catch (NumberFormatException nfe) {
                    Log.w("ddms", "Expected digits, got '" + lenStr +
"': "
                            + lenBuf[0] + " " + lenBuf[1] + " " +
lenBuf[2] + " "
                            + lenBuf[3]);
                    Log.w("ddms", "reply was " +
replyToString(reply));
                    break;
                }

                byte[] msg = new byte[len];
                buf = ByteBuffer.wrap(msg, 0, msg.length);
                adbChan.read(buf);

                String message = replyToString(msg);
                Log.v("ddms", "Got reply '" + replyToString(reply) +
"', diag='"
                        + message + "'");

                break;
            }
                } catch (Exception e) {
                        Log.e("ScreenProjector", "", e);
                }

                setContentView(R.layout.main); // This displays the content on 
the
        }

    /**
     * Create an ASCII string preceeded by four hex digits. The
opening "####"
     * is the length of the rest of the string, encoded as ASCII hex
(case
     * doesn't matter). "port" and "host" are what we want to forward
to. If
     * we're on the host side connecting into the device, "addrStr"
should be
     * null.
     */
    static byte[] formAdbRequest(String req) {
        String resultStr = String.format("%04X%s", req.length(),
req); //$NON-NLS-1$
        byte[] result;
        try {
            result = resultStr.getBytes(DEFAULT_ENCODING);
        } catch (UnsupportedEncodingException uee) {
            uee.printStackTrace(); // not expected
            return null;
        }
        assert result.length == req.length() + 4;
        return result;
    }

    /**
     * Converts an ADB reply to a string.
     */
    static String replyToString(byte[] reply) {
        String result;
        try {
            result = new String(reply, DEFAULT_ENCODING);
        } catch (UnsupportedEncodingException uee) {
            uee.printStackTrace(); // not expected
            result = "";
        }
        return result;
    }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to