There are a lot of apps out there with which you can control your PC in various different ways. This article tries to explain how is it possible to write such apps. We’ll try to build a very simple remote for VLC media player. Using this remote, the user will be able to play/pause a video or jump to next/previous video in the playlist and also move the mouse around the screen.
To control our PC using an android app, we need a piece of code to run the server side (PC) to executecommands sent from the client side (android app) code. First, we’ll write the client side android app and then the server side java code.
CREATING CLIENT APP
As this example is meant for only for explaining the concept, we’ll create a very simple one page android app. It has only one activity which has 4 controls. One TextView which will act as our mouse pad and 3 buttons (Next, Play/Pause, Previous).
Create a new android project by the name RemoteDroid, with a blank activity (activity_main). Create the required textview and buttons either using the design view or text view. You can copy paste the following code to create the required activity layout.
We also need a button which will help us initiate connection to the server. We’ll represent this button in the action bar with “ic_action_cast” icon that comes with android Action Bar Icon Pack. Add this icon in your drawable folder and this line in your main menu (menu_main.xml under res->menu).
Once, the socket is connected, we need to create an output stream on that socket to send data to the server. Anything that we write on that output stream will be sent to the server.
PrintWriter outStream = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
outStream.println(“Hi”) //This will send “Hi” to server
When the user taps on play, next or previous button, we send “play”, “next” or “previous” text respectively to the server using outStream.println() and when the user drags on the mousePad text view, we send the movement in x and y direction in “x,y” format. To implement this complete functionality, paste the following code in your MainActivity.java file
We can ask for server IP from the user in an EditText and then connect to that IP on a pre-determined port number, but for this example, we have hard-coded both these values and you should replace them based on your requirement.
WRITING THE SERVER
The client just connects via an open socket and send some text over to the server. Server is where all the magic of remote control happens. To simulate keyboard and mouse events on the server, we use java.awt.Robot class. This class was built mainly for the purpose of test automation, but can be used for various other purposes.
First thing we need to do on server is to open a server socket and then wait for a client to connect to it. Let’s create a server socket on a port
ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
Socket clientSocket = server.accept();
Once the client is connected and is sending data, we’ll read the input line by line in a BufferedReader and then act on the basis of each input.
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line = in.readLine();
To simulate keyboard events, we’ll the keyPress and keyRelease methods of Robot class. So for example, when the user wants to move to next song in the play list, he/she taps on “Next” button in the app which sends the string “next” to the server and upon receiving this string, the server simulates the press and release of key “n” on keyboard. We are using following VLC shortcuts:-
Next – n
Previous – p
Play/Pause – space
These are default shortcuts, you need to modify code if you have changed your VLC shortcuts.
Both keyPress and keyRelease methods take an integer as input. These integers can be obtained from KeyEvent class. KeyEvent has static fields corresponding to each keyboard key and event. This is how we simulate press and release of “n”
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_N);
To simulate a mouse event, we need to use the mouse related methods in Robot class. To move the mouse pointer, use Robot.mouseMove(x,y) and to simulate mouse click, use Robot.mousePress() and Robot.mouseRelease().
The complete server code can be written as below:-
Save the above code in RemoteDroidServer.java file and then compile, execute using the usual Java way.
This will start the server and wait for a client to connect. After starting the server, run the android app and try to connect to your server.
NOTE:- make sure you have specified the correct server IP address in the app. Also, that the port numbers are same on both server and app.
SOURCE CODE AND MORE IDEAS
This code has been tested with a Windows 8.1 machine and a MacBook running OS X Yosemite, it should most probably work on all other operating systems as well. Server code is available above and can be further referenced from this gist and the app source is also available on github.
NOTE:- This code is only for demo purpose and should not be used as is in production.
A simple VLC remote might or might not be the thing you are looking for, but the underlying concept is very interesting. You can extend this to any level. Create a remote control for any specific app or a general purpose PC remote or even create a Game pad. Phones now a days have a lot of sensors which can be used in interesting ways, like a phone’s accelerometer and gyroscope can be used to know the tilt and control a car in any racing game. The possibilities are endless.