Android screen as intranet PC touchpad

I want to use the Android screen as a touchpad for a PC on the intranet, so for which protocol I should use and how to create a pc server in java.

Help me. thank you

+4
source share
1 answer

It is very easy to use. Use UDP to connect within your intranet because it is faster without a connection, so there is no need to clear the buffer and wait for the transfer. Your application should work in real time, so you need to connect UDP sockets.

On the java server side, just create a datagram packet server with a port number. Use only byte stream for faster connection.

General example code. Here I used an 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); } } 
+4
source

Source: https://habr.com/ru/post/1385290/


All Articles