introduction
Send commands to the PC on the Android side through Bluetooth, and the Java program receives commands and performs corresponding actions. The instruction format is standardized, and the java program on the PC side operates through the robot library.
Code
Control class
import ; import ; import ; import ; import ; import ; import ; public class remotePC { //Save the coordinates of the current mouse pointer (px, py) private static int px; private static int py; //Maximum delay time: 1 second public static final int MAX_DELAY = 1000; //Minimum interval time: 1 millisecond public static final int SAMPLE_TIME_DIV = 1; //Magic number, used to set the default event delay time interval private final double magicX = 1.0; //Visual delay: default 100ms private final int VISIBAL_MOVEMENT = 100; //PC screen size private int screenWidth; private int screenHeight; //Mobile screen size private int mobileWidth; private int mobileHeight; //The ratio of mobile phone computer size conversion private double widScale; private double heiScale; //The robot class for control private Robot robot; //Default constructor public remotePC() throws AWTException{ this(1366, 768); } //Constructor, specify the screen size of the mobile phone public remotePC(int mobileWidth, int mobileHeight) throws AWTException{ robot = new Robot(); ((int)magicX); setScreenSize(); = mobileHeight; = mobileWidth; setScale(); } public void moveToCenter(){ (screenWidth/2, screenHeight/2, 1); } //[Mouse cursor movement] //dt: interval time, time length corresponds to speed //dx,dy: The relative horizontal displacement of moving on the mobile phone will automatically convert to the size that should be moved on the PC public void move(int dx, int dy, int dt){ double deltaX = (1.0*dx/widScale); double deltaY = (1.0*dy/heiScale); int dxpms = (int)deltaX/dt; int dypms = (int)deltaY/dt; for(int i=0; i<dt; i++){ px += dxpms; py += dypms; if(px <= 0){ px = 0; }else if(px >= screenWidth){ px = screenWidth; } if(py <= 0){ py = 0; }else if(py >= screenHeight){ py = screenHeight; } ((int)px, (int)py); } } //[Press the left mouse button] public void pressMouseL(){ (InputEvent.BUTTON1_DOWN_MASK); } //[Release the left mouse button] public void releaseMouseL(){ (InputEvent.BUTTON1_DOWN_MASK); } //[Click the left mouse button] public void clickMouseL(){ (); (); } //[Press the right mouse button] public void pressMouseR(){ (InputEvent.BUTTON3_DOWN_MASK); } //[Release the right mouse button] public void releaseMouseR(){ (InputEvent.BUTTON3_DOWN_MASK); } //[Right click] public void clickMouseR(){ (); (); } //[Press the scroll wheel] public void pressWheel(){ (InputEvent.BUTTON2_DOWN_MASK); } //[Release the roller] public void releaseWheel(){ (InputEvent.BUTTON2_DOWN_MASK); } //[Move the roller down]: step is the number of movement steps, relative to the roller public void wheelDown(int step){ for(int i=0; i<step; i++){ (1); } } //[Move the roller upward]: step is the number of movement steps, relative to the roller public void wheelUp(int step){ for(int i=0; i<step; i++){ (-1); } } //[Touch a square character: A-Z]: c is a char type character with the letter, and must be capitalized public void printChar(char c){ if(c <= 'Z' && c >= 'A'){ ((int)c); ((int)c); } } //[Click a space] public void printSpace(){ (KeyEvent.VK_SPACE); (KeyEvent.VK_SPACE); } //[Zoom in] is equivalent to ctrl+roller moving up public void zoomIn(int step){ (KeyEvent.VK_CONTROL); for(int i=0; i<step; i++) (-1); (KeyEvent.VK_CONTROL); } //[Shrink] is equivalent to ctrl+roller moving down public void zoomOut(int step){ (KeyEvent.VK_CONTROL); for(int i=0; i<step; i++) (1); (KeyEvent.VK_CONTROL); } //[Show app switch bar] is equivalent to alt+tab, and alt is not released. Once the function is called, closeSwitchApps() needs to be manually called to release the alt key public void showSwitchApps(){ (KeyEvent.VK_ALT); (KeyEvent.VK_TAB); (KeyEvent.VK_TAB); //(KeyEvent.VK_ALT); } //[App switch to the right] is equivalent to pressing tab again when alt+tab is pressed public void tabRight(){ (KeyEvent.VK_TAB); (KeyEvent.VK_TAB); } //[App switch to left] is equivalent to pressing shift+tab again when pressing alt+tab public void tabLeft(){ (KeyEvent.VK_SHIFT); (KeyEvent.VK_TAB); (KeyEvent.VK_TAB); (KeyEvent.VK_SHIFT); } //[Close the app switch bar] After using showSwitchApps(), this function must be called to free alt public void closeSwitchApps(){ (KeyEvent.VK_ALT); } //[App cut back/left once]: Press shift+alt+tab and release public void simpleLeftSwitchApp(){ (); (VISIBAL_MOVEMENT); (); (); (VISIBAL_MOVEMENT); (); } //[App switch/Cut right once]: Press alt+tab and release public void simpleRightSwitchApp(){ (); (VISIBAL_MOVEMENT); (); } //[Show all apps under the current window]: equivalent to window+tab public void listAppsWindow(){ (KeyEvent.VK_WINDOWS); (KeyEvent.VK_TAB); (KeyEvent.VK_TAB); (KeyEvent.VK_WINDOWS); } //[Show desktop/hide app]: equivalent to window+M public void showDesktop(){ (KeyEvent.VK_WINDOWS); (KeyEvent.VK_M); (KeyEvent.VK_M); (KeyEvent.VK_WINDOWS); } //[Current window maximization]: equivalent to window+UP public void windowUp(){ (KeyEvent.VK_WINDOWS); (KeyEvent.VK_UP); (KeyEvent.VK_UP); (KeyEvent.VK_WINDOWS); } //[Cancel the current window maximization]: equivalent to window+DOWN public void windowDown(){ (KeyEvent.VK_WINDOWS); (KeyEvent.VK_DOWN); (KeyEvent.VK_DOWN); (KeyEvent.VK_WINDOWS); } //[Window is placed on the left/multiple windows side by side]: equivalent to window+LEFT public void windowLeft(){ //TODO: window + Left (KeyEvent.VK_WINDOWS); (KeyEvent.VK_LEFT); (KeyEvent.VK_LEFT); (KeyEvent.VK_WINDOWS); } //[Window is placed on the right/multiple windows side by side]: equivalent to window+RIGHT public void windowRight(){ //TODO: window + Right (KeyEvent.VK_WINDOWS); (KeyEvent.VK_RIGHT); (KeyEvent.VK_RIGHT); (KeyEvent.VK_WINDOWS); } //[Switch desktop: Switch left]: window+control+left public void leftSwitchWindow(){ (KeyEvent.VK_WINDOWS); (KeyEvent.VK_CONTROL); (KeyEvent.VK_LEFT); (KeyEvent.VK_LEFT); (KeyEvent.VK_CONTROL); (KeyEvent.VK_WINDOWS); } //[Switch desktop: right]: window+control+right public void rightSwitchWindow(){ (KeyEvent.VK_WINDOWS); (KeyEvent.VK_CONTROL); (KeyEvent.VK_RIGHT); (KeyEvent.VK_RIGHT); (KeyEvent.VK_CONTROL); (KeyEvent.VK_WINDOWS); } //[Quickly open Notepad] public void openNotepad(){ try { ().exec("notepad"); } catch (IOException e) { // TODO Auto-generated catch block (); } } //[Quickly open the drawing board] public void openPaint(){ try { ().exec("mspaint"); } catch (IOException e) { // TODO Auto-generated catch block (); } } //[ppt pen] public void setDraw(){ (KeyEvent.VK_CONTROL); (KeyEvent.VK_P); (KeyEvent.VK_CONTROL); (KeyEvent.VK_P); } //[ppt laser pen] public void setLaser(){ (KeyEvent.VK_CONTROL); (KeyEvent.VK_L); (KeyEvent.VK_CONTROL); (KeyEvent.VK_L); } //[ppt highlighter] public void setMark(){ (KeyEvent.VK_CONTROL); (KeyEvent.VK_I); (KeyEvent.VK_CONTROL); (KeyEvent.VK_I); } //[ppt hide mouse] public void hideMouse(){ (KeyEvent.VK_CONTROL); (KeyEvent.VK_H); (KeyEvent.VK_CONTROL); (KeyEvent.VK_H); } //[ppt Previous] public void prevSlide(){ (KeyEvent.VK_LEFT); (KeyEvent.VK_LEFT); } //[ppt next photo] public void nextSlide(){ (KeyEvent.VK_RIGHT); (KeyEvent.VK_RIGHT); } //Delay function, delay ms milliseconds public void delay(int ms){ (ms); } //Get the current PC screen size public void setScreenSize(){ Dimension screensize = ().getScreenSize(); screenWidth = (int)(); screenHeight = (int)(); } //Print the current PC screen size public String getScreenInfo(){ return "screenSize:"+screenWidth+"*"+screenHeight; } //Set the conversion ratio of the screen size of the mobile phone and PC private void setScale(){ heiScale = -1.0 / 1.5; widScale = 1.0 / 3.0; } }
The PC side interacts with the Android app via Bluetooth:
/** * Created by luyudi on 2016/11/9. * Modified by Lannooo on 2016/12/4. */ // server import .*; import ; import ; import ; import ; import ; import ; public class BlueToothServer implements Runnable { private remotePC Controller; // The following UUID must be consistent with the UUID of the mobile client private static UUID ECHO_SERVER_UUID= new UUID("aeb9f938a1a34947ace29ebd0c67adf1", false); // Stream connection notification is used to create a stream connection private StreamConnectionNotifier myPCConnNotifier = null; // Streaming connection private StreamConnection streamConn = null; // Accept data byte stream // Receive x y coordinates private byte[] acceptedByteArray = new byte[1024]; // Read (input) stream private InputStream inputStream = null; // Main thread public static void main(String[] args) { new BlueToothServer(); } public BlueToothServer() { try { String url = "btspp://localhost:" + ECHO_SERVER_UUID.toString(); // Get notification of streaming connection myPCConnNotifier = (StreamConnectionNotifier) (url); Controller = new remotePC(); } catch (Exception e) { // TODO Auto-generated catch block (); } // Open the connection channel and read the stream thread new Thread(this).start(); } public static int getInt(byte[] bytes){ return (0xff & bytes[3]) | (0xff00 & (bytes[2] << 8)) | (0xff0000 & (bytes[1] << 16)) | (0xff000000 & (bytes[0] << 24)); } public static float getFloat(byte[] b){ return (getInt(b)); } @Override public void run() { try { boolean isMouseLPressed = false; boolean isWheelPressed = false; boolean end = false; while (true) { // Continue to listen to client connection requests // Get streaming connection streamConn = (); // Get the stream channel inputStream = (); // Loop reading of byte stream, judging code type and x and y coordinates while ((acceptedByteArray) != -1) { String acceptString = new String(acceptedByteArray); int index; if((index = ("RemoteTouch")) != -1) { byte[] codeBytes = new byte[4]; byte[] dxBytes = new byte[6]; byte[] dyBytes = new byte[6]; (acceptedByteArray, index + 11, codeBytes, 0, 4); (acceptedByteArray, index + 15, dyBytes, 0, 6); (acceptedByteArray, index + 21, dxBytes, 0, 6); int dy = (new String(dyBytes)); int dx = (new String(dxBytes)); int code = getInt(codeBytes); if (end) { (); if (streamConn != null) { (); ("Disconnected..."); } break; } switch (code) { case 1://Press the left mouse button isMouseLPressed = true; (); ("Pressing mouse L"); break; case 2://Release the left mouse button if (isMouseLPressed) { (); ("Released mouse L"); isMouseLPressed=false; } break; case 3://Click the left mouse button (); ("Clicked mouse L"); break; case 4://Click the right mouse button (); ("Clicked mouse R"); break; case 5://Press the scroll wheel// isWheelPressed = true; // (); // ("Pressing wheel"); break; case 6://Release the roller// if(isWheelPressed){ // (); // ("Released wheel"); // } break; case 7://The roller rolls int step = (dy) / 40; ("wheel"); if (dy > 0) { (step); ("Wheel Down:%d steps. dy=%d\n", step, dy); } else { (step); ("Wheel Up:%d steps. dy=%d\n", step, dy); } break; case 8://Zoom in and out double s = ((double) (dx * dx + dy * dy)); if (dx < 0) { ((int) s / 20); ("Zoom out %d steps. dx=%d,dy=%d\n", (int) s / 20, dx, dy); } else { ((int) s / 20); ("Zoom in %d steps. dx=%d,dy=%d\n", (int) s / 20, dx, dy); } break; case 9://Show switchable apps (); ("show Switch apps"); break; case 10://Show desktop (); ("show desktop"); break; case 11://app (); ("switch app: right"); break; case 12://app (); ("switch app: left"); break; case 13://Window right (); ("switch window right"); break; case 14://window (); ("switch window left"); break; case 15://Double click on the left mouse button (); (1); (); ("clicked double mouse L"); break; case 16://Mouse Move (dx, dy, 1); //("Move mouse:dx=%d,dy=%d\n", dx, dy); break; case 17://Left split screen (); ("Window divide: left"); break; case 18://Right split screen (); ("Window divide: right"); break; case 19: //Previous ppt (); ("previous slide"); break; case 20: (); ("Next Slide"); break; case 32: // PPT is set to hide the mouse (); ("Hide"); break; case 33: // ppt laser pen (); ("Laser"); break; case 34: // ppt pen (); ("Draw"); break; case 35: // ppt highlighter (); ("Mark"); break; case 100://quit end = true; ("Quit."); break; default://Not recognized ("Unknown code"); break; } } // clear data acceptedByteArray = new byte[1024]; } } } catch (IOException e) { (); } } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.