package org.las2mile.scrcpy; import java.io.Closeable; import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.net.InetAddress; import android.os.Build; import java.net.Inet4Address; import java.net.InetAddress; import java.net.InetSocketAddress; public final class DroidConnection implements Closeable { private static Socket socket = null; private OutputStream outputStream; private InputStream inputStream; private DroidConnection(Socket socket) throws IOException { this.socket = socket; inputStream = socket.getInputStream(); outputStream = socket.getOutputStream(); } private static Socket listenAndAccept() throws IOException { //只允许连接公司的设备 Ln.d("The device start listenAndAccept"); ServerSocket serverSocket = new ServerSocket(); serverSocket.bind(new InetSocketAddress("192.168.43.1",7007)); InetAddress serverIP = serverSocket.getInetAddress(); String deviceIP = serverIP.getHostAddress(); boolean isBound = serverSocket.isBound(); Ln.d("The device isBound " +isBound); if (!"192.168.43.1".equals(deviceIP)) { Ln.d("The device is not produced"); serverSocket.close(); System.exit(0); return null; } Socket sock = null; try { sock = serverSocket.accept(); } finally { serverSocket.close(); } return sock; } public static DroidConnection open(String ip) throws IOException { socket = listenAndAccept(); DroidConnection connection = null; if (socket.getInetAddress().toString().equals(ip)) { connection = new DroidConnection(socket); } return connection; } public void close() throws IOException { socket.shutdownInput(); socket.shutdownOutput(); socket.close(); } public OutputStream getOutputStream() { return outputStream; } public int[] NewreceiveControlEvent() throws IOException { byte[] buf = new byte[16]; int n = inputStream.read(buf, 0, 16); if (n == -1) { throw new EOFException("Event controller socket closed"); } final int[] array = new int[buf.length / 4]; for (int i = 0; i < array.length; i++) array[i] = (((int) (buf[i * 4]) << 24) & 0xFF000000) | (((int) (buf[i * 4 + 1]) << 16) & 0xFF0000) | (((int) (buf[i * 4 + 2]) << 8) & 0xFF00) | ((int) (buf[i * 4 + 3]) & 0xFF); return array; } }