Jyosna
Jyosna

Reputation: 4446

android screen as pc touchpad in intranet

I want to use android screen as a pc touchpad in intranet, so for that which protocol i should use and how to create pc server in java.

Help me. Thank you

Upvotes: 3

Views: 605

Answers (1)

Santanu
Santanu

Reputation: 420

Its very easy to use. Use UDP for connectivity within your intranet, because it's faster, connectionless, so there's no need to flush the buffer and wait for transmit. Your application should run in real time, so you require UDP socket connectivity.

In Server side java, just create a Datagram packet server with a port number. Use only byte stream for faster connectivity.

Shared a sample code. Here I used 8 byte packet size:

public class Server extends Thread
{

LinkedList<String[]> queue = new LinkedList<String[]>();

public static void main(String[] args)
{
    new Server().start();

}


@Override
public void run()
{
    try
    {
        int server_port = 12345;
        byte[] message = new byte[8];



        DatagramPacket p = new DatagramPacket(message, message.length);
        DatagramSocket s = new DatagramSocket(server_port);

        Robot r = new Robot();

        PointerInfo a = MouseInfo.getPointerInfo();
        Point b = a.getLocation();

        int curx = (int) b.getX();
        int cury = (int) b.getY();
        int prex = 0;
        int prey = 0;
        while (true)
        {
            p = new DatagramPacket(message, 8);
            s.receive(p);
            System.out.println(p.getAddress());
            int x = byteArrayToInt1(message);
            int y = byteArrayToInt2(message);

            if (x == 0 && y == 0)
            {
                prex = 0;
                prey = 0;

                a = MouseInfo.getPointerInfo();
                b = a.getLocation();

                curx = (int) b.getX();
                cury = (int) b.getY();

                r.mouseMove(curx, cury);
            }
            else
            {
                curx = curx - (prex - x);
                cury = cury - (prey - y);

                r.mouseMove(curx, cury);

                prex = x;
                prey = y;
            }


        }

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

}

//use reverse of this logic in your android to convert co-ordinate int to byte[]
public static final int byteArrayToInt1(byte[] b)
{
    return (b[0] << 24)
            + ((b[1] & 0xFF) << 16)
            + ((b[2] & 0xFF) << 8)
            + (b[3] & 0xFF);
}

public static final int byteArrayToInt2(byte[] b)
{
    return (b[4] << 24)
            + ((b[5] & 0xFF) << 16)
            + ((b[6] & 0xFF) << 8)
            + (b[7] & 0xFF);
    }
}

Upvotes: 3

Related Questions