Skip to content

Commit

Permalink
Overall working
Browse files Browse the repository at this point in the history
  • Loading branch information
jololhuet committed Apr 16, 2017
1 parent 90b26bc commit 6055b3a
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 157 deletions.
1 change: 1 addition & 0 deletions SmartMouse/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
Expand Down
1 change: 1 addition & 0 deletions SmartMouse/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="com.lesbobets.smartmouse">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class MainActivity extends AppCompatActivity implements

private String mServerIP;

public static int PORT_DISCOVERY = 7777;
public static int PORT_DISCOVERY = 7776;
public static int PORT_PACKETS = 7777;

TwoFingersDetector multiTouchListener = new TwoFingersDetector() {
Expand Down Expand Up @@ -61,6 +61,9 @@ private void initClient() {
mServerContacter = new ServerContacter(mServerIP, MainActivity.PORT_PACKETS);
}


private int count = 0;

@Override
public boolean onTouchEvent(MotionEvent event) {
this.multiTouchListener.onTouchEvent(event);
Expand All @@ -84,18 +87,23 @@ public boolean onTouchEvent(MotionEvent event) {
if (System.currentTimeMillis() - lastPushDownTime < 10) {
return super.onTouchEvent(event);
}
mVelocityTracker.addMovement(event);
mVelocityTracker.computeCurrentVelocity(1000);
float xVelocity = VelocityTrackerCompat.getXVelocity(mVelocityTracker, pointerId);
float yVelocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, pointerId);

Log.d(TAG, " Velocities: " + xVelocity + "\t\t" + yVelocity);

final Double[] velocities = new Double[]{Double.valueOf(xVelocity), Double.valueOf(yVelocity)};
if (mServerContacter != null) {
mServerContacter.sendVelocities(velocities);
}

// Send only sometimes
// if (count < 9) {
mVelocityTracker.addMovement(event);
mVelocityTracker.computeCurrentVelocity(1000);
float xVelocity = VelocityTrackerCompat.getXVelocity(mVelocityTracker, pointerId);
float yVelocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, pointerId);

// Log.d(TAG, " Velocities: " + xVelocity + "\t\t" + yVelocity);

final Double[] velocities = new Double[]{Double.valueOf(xVelocity), Double.valueOf(yVelocity)};
if (mServerContacter != null) {
mServerContacter.sendVelocities(velocities);
}
// } else if (count == 9) {
// count = 0;
// }
// count++;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,25 @@ public class ServerContacter {
private DatagramSocket clientSocket;
private InetAddress serverAdress;


public ServerContacter(final String ipAddress, final int port) {

new Thread(new Runnable() {
@Override
public void run() {
try {
// Sleep to let the server change process
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}

serverAdress = InetAddress.getByName(ipAddress);
Log.d("<<<<", ipAddress + " is the server ip !");
clientSocket = new DatagramSocket(port, serverAdress);
Log.d("<<<<", serverAdress + " is the server ip !");
clientSocket = new DatagramSocket(port);
clientSocket.connect(serverAdress, port);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}


public void sendVelocities(final double xVelocity, final double yVelocity) {

new Thread(new Runnable() {
Expand All @@ -50,8 +46,6 @@ public void run() {
} catch (IOException e) {
e.printStackTrace();
}
// out.println("V," + xVelocity + "," + yVelocity);
// out.flush();
}
}
}).start();
Expand All @@ -74,8 +68,6 @@ public void run() {
} catch (IOException e) {
e.printStackTrace();
}
// out.println("LC");
// out.flush();
}
}
}).start();
Expand All @@ -95,8 +87,6 @@ public void run() {
} catch (IOException e) {
e.printStackTrace();
}
// out.println("RC");
// out.flush();
}
}
}).start();
Expand All @@ -116,8 +106,6 @@ public void run() {
} catch (IOException e) {
e.printStackTrace();
}
// out.println("S,"+distanceX+","+distanceY);
// out.flush();
}
}
}).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -17,12 +20,14 @@
import android.widget.ListView;

import java.io.IOException;
import java.math.BigInteger;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.nio.ByteOrder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -36,7 +41,7 @@ public class ServersAndParamsActivity extends AppCompatActivity {

private SetAdapter<String> listAdapter;
private ListView serversList;
private static DatagramSocket c;
private DatagramSocket c;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -56,28 +61,8 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
String text = (serversList.getItemAtPosition(position)).toString();
final InetAddress ip = listAdapter.getValue(text);

// Send a packet in another thread
new Thread(new Runnable() {
@Override
public void run() {
try {
byte[] sendData;
sendData = ("START_REQUEST").getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ip, MainActivity.PORT_DISCOVERY);
c.send(sendPacket);

//Close the port!
c.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();


Intent intent = new Intent(getApplicationContext(), MainActivity.class);
String ipString = ip.toString().substring(1);
Log.d("<<<<<", "IP is " + ipString);
String ipString = ip.getHostAddress();
intent.putExtra("IP", ipString);
startActivity(intent);
}
Expand Down Expand Up @@ -107,37 +92,27 @@ public void run() {

byte[] sendData = "DISCOVER_REQUEST".getBytes();

//Try the 255.255.255.255 first
try {
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getByName("255.255.255.255"), MainActivity.PORT_DISCOVERY);
c.send(sendPacket);
Log.d("BROADCAST", getClass().getName() + ">>> Request packet sent to: 255.255.255.255 (DEFAULT)");
} catch (Exception e) {
}

// Broadcast the message over all the network interfaces
Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = (NetworkInterface) interfaces.nextElement();
if (networkInterface.toString().contains("wlan")) {
for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
InetAddress broadcast = interfaceAddress.getBroadcast();
if (broadcast == null) {
continue;
}

if (networkInterface.isLoopback() || !networkInterface.isUp()) {
continue; // Don't want to broadcast to the loopback interface
}

for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
InetAddress broadcast = interfaceAddress.getBroadcast();
if (broadcast == null) {
continue;
}
// Send the broadcast package!
try {
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, broadcast, MainActivity.PORT_DISCOVERY);
c.send(sendPacket);
} catch (Exception e) {
}

// Send the broadcast package!
try {
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, broadcast, MainActivity.PORT_DISCOVERY);
c.send(sendPacket);
} catch (Exception e) {
Log.d("BROADCAST", getClass().getName() + ">>> Request packet sent to: " + broadcast.getHostAddress() + "; Interface: " + networkInterface.getDisplayName());
}

Log.d("BROADCAST", getClass().getName() + ">>> Request packet sent to: " + broadcast.getHostAddress() + "; Interface: " + networkInterface.getDisplayName());
break;
}
}

Expand Down Expand Up @@ -168,7 +143,8 @@ public void run() {
});
}
}

// Close port
// c.close();
} catch (IOException ex) {
ex.printStackTrace();
}
Expand Down
Binary file modified SmartMouseServer/out/production/SmartMouseServer/Server.class
Binary file not shown.
Binary file not shown.
Binary file modified SmartMouseServer/out/production/SmartMouseServer/TestClient.class
Binary file not shown.
89 changes: 75 additions & 14 deletions SmartMouseServer/src/DiscoveryThread.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import java.awt.*;
import java.awt.event.InputEvent;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
* Created by joachim on 4/14/17.
* Singleton for the discovery thread
*/
public class DiscoveryThread implements Runnable {
private DatagramSocket socket;
private int port = 7777;
private int port = 7776;

@Override
public void run() {
Expand All @@ -20,7 +21,7 @@ public void run() {

while (true) {
/// Receive a packet
System.out.println(getClass().getName() + ">>>Ready to receive broadcast packets !");
// System.out.println(getClass().getName() + ">>>Ready to receive broadcast packets !");
byte[] recvBuf = new byte[1000];
DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length);
socket.receive(packet);
Expand All @@ -31,19 +32,23 @@ public void run() {

/// See if the packet holds the right command
String message = new String(packet.getData()).trim();
if (message.equals("DISCOVER_REQUEST")) {
byte[] sendData = ("DISCOVER_RESPONSE, " + InetAddress.getLocalHost().getHostName()).getBytes();

/// Send a response
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, packet.getAddress(), packet.getPort());
socket.send(sendPacket);
String[] stringPos = message.split(",");
String matcher = stringPos[0];
switch (matcher) {
case "DISCOVER_REQUEST":
byte[] sendData = ("DISCOVER_RESPONSE, " + InetAddress.getLocalHost().getHostName()).getBytes();

/// Send a response
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, packet.getAddress(), packet.getPort());
socket.send(sendPacket);

System.out.println(getClass().getName() + ">>>Sent packet to: " + sendPacket.getAddress().getHostAddress());
break;

default:
System.err.println("Problem, default value should not be reached !");

System.out.println(getClass().getName() + ">>>Sent packet to: " + sendPacket.getAddress().getHostAddress());
}
if (message.equals("START_REQUEST")) {
System.out.println(">>>>Received a start request !!");
// We can just stop waiting for broadcasts message
break;
}
}

Expand All @@ -61,4 +66,60 @@ public static DiscoveryThread getInstance() {
private static class DiscoveryThreadHolder {
private static final DiscoveryThread INSTANCE = new DiscoveryThread();
}




private static void updateCursor(double vx, double vy) {
double alpha = 0.02; /// This is a factor of speed we could be changing (User speed)

try {
/// The robot useful to move the mouse then
Robot robot = new Robot();

/// Get current mouse position
Point pos = MouseInfo.getPointerInfo().getLocation();

// Auto delay
robot.mouseMove((int)(pos.x + alpha*vx), (int)(pos.y + alpha*vy));

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

private static void leftClick() {
try {
/// The robot useful to click
Robot robot = new Robot();
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);

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

private static void rightClick() {
try {
/// The robot useful to click
Robot robot = new Robot();
robot.mousePress(InputEvent.BUTTON3_MASK);
robot.mouseRelease(InputEvent.BUTTON3_MASK);

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

private static void scroll(double x, double y) {
try {
/// The robot useful to click
Robot robot = new Robot();
robot.mouseWheel((int)x);

} catch (AWTException e) {
e.printStackTrace();
}
}
}
Loading

0 comments on commit 6055b3a

Please sign in to comment.