summaryrefslogtreecommitdiff
path: root/same-android/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'same-android/src/main')
-rw-r--r--same-android/src/main/java/com/orbekk/HelloAndroidActivity.java12
-rw-r--r--same-android/src/main/java/com/orbekk/discovery/Broadcaster.java4
-rw-r--r--same-android/src/main/java/com/orbekk/discovery/SameService.java28
3 files changed, 36 insertions, 8 deletions
diff --git a/same-android/src/main/java/com/orbekk/HelloAndroidActivity.java b/same-android/src/main/java/com/orbekk/HelloAndroidActivity.java
index 81eebc4..c8c27f3 100644
--- a/same-android/src/main/java/com/orbekk/HelloAndroidActivity.java
+++ b/same-android/src/main/java/com/orbekk/HelloAndroidActivity.java
@@ -1,5 +1,7 @@
package com.orbekk;
+import java.net.InetAddress;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -14,6 +16,7 @@ import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
+import android.widget.EditText;
import android.widget.TextView;
public class HelloAndroidActivity extends Activity {
@@ -30,6 +33,9 @@ public class HelloAndroidActivity extends Activity {
logger.info("Joining network");
Intent intent = new Intent(this, SameService.class);
intent.setAction("join");
+ // InetAddress address = new Broadcaster(this).getBroadcastAddress();
+ EditText t = (EditText)findViewById(R.id.editText1);
+ intent.putExtra("ip", t.getText().toString());
startService(intent);
}
@@ -39,6 +45,11 @@ public class HelloAndroidActivity extends Activity {
t.append(new Broadcaster(this).getWlanAddress().getHostAddress());
}
+ private void showBroadcastAddress() {
+ EditText t = (EditText)findViewById(R.id.editText1);
+ t.setText(new Broadcaster(this).getBroadcastAddress().getHostAddress());
+ }
+
/**
* Called when the activity is first created.
* @param savedInstanceState If the activity is being re-initialized after
@@ -54,6 +65,7 @@ public class HelloAndroidActivity extends Activity {
setContentView(R.layout.main);
showIpAddress();
+ showBroadcastAddress();
// ClientApp client = new ClientApp();
// SameInterface client_ = client.getClient(10015, "ClientNetwork",
diff --git a/same-android/src/main/java/com/orbekk/discovery/Broadcaster.java b/same-android/src/main/java/com/orbekk/discovery/Broadcaster.java
index 167eda1..6744651 100644
--- a/same-android/src/main/java/com/orbekk/discovery/Broadcaster.java
+++ b/same-android/src/main/java/com/orbekk/discovery/Broadcaster.java
@@ -51,11 +51,11 @@ public class Broadcaster {
return fromInt(broadcast);
}
- public synchronized boolean sendBroadcast(byte[] data, int port) {
+ public synchronized boolean sendUdpData(byte[] data, InetAddress ip, int port) {
try {
socket = new DatagramSocket(0);
socket.setBroadcast(true);
- DatagramPacket packet = new DatagramPacket(data, data.length, getBroadcastAddress(), port);
+ DatagramPacket packet = new DatagramPacket(data, data.length, ip, port);
socket.send(packet);
return true;
} catch (SocketException e) {
diff --git a/same-android/src/main/java/com/orbekk/discovery/SameService.java b/same-android/src/main/java/com/orbekk/discovery/SameService.java
index abc92e8..0dd6593 100644
--- a/same-android/src/main/java/com/orbekk/discovery/SameService.java
+++ b/same-android/src/main/java/com/orbekk/discovery/SameService.java
@@ -1,6 +1,8 @@
package com.orbekk.discovery;
import java.net.DatagramPacket;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
import android.app.Service;
import android.content.Intent;
@@ -46,7 +48,7 @@ public class SameService extends Service {
String port = words[1];
String url = "http://" + packet.getAddress().getHostAddress() +
- ":" + port + "/";
+ ":" + port + "/ClientService.json";
listener.discover(url);
Message message = Message.obtain();
@@ -79,13 +81,22 @@ public class SameService extends Service {
}
}
- private void sendBroadcastDiscovery() {
+ private void sendBroadcastDiscovery(InetAddress ip) {
+ Broadcaster broadcaster = new Broadcaster(this);
String message = "Discover " + (PORT + 2);
byte[] data = message.getBytes();
- new Broadcaster(this).sendBroadcast(data, PORT);
+ if (ip.equals(broadcaster.getBroadcastAddress())) {
+ broadcaster.sendUdpData(data, ip, PORT);
+ } else {
+ String remoteAddress =
+ String.format("http://%s:%s/ClientService.json",
+ ip.getHostAddress(), PORT + 2);
+ sameController.getClient().sendDiscoveryRequest(
+ remoteAddress);
+ }
}
- private void joinNetwork() {
+ private void joinNetwork(InetAddress ip) {
sameController.getClient().setNetworkListener(
new NetworkNotificationListener() {
@Override
@@ -96,7 +107,7 @@ public class SameService extends Service {
toastHandler.sendMessage(message);
}
});
- sendBroadcastDiscovery();
+ sendBroadcastDiscovery(ip);
}
@Override
@@ -125,7 +136,12 @@ public class SameService extends Service {
if (intent.getAction().equals("create")) {
createNetwork();
} else if (intent.getAction().equals("join")) {
- joinNetwork();
+ try {
+ InetAddress ip = InetAddress.getByName(intent.getExtras().getString("ip"));
+ joinNetwork(ip);
+ } catch (UnknownHostException e) {
+ logger.error("Unknown host.", e);
+ }
}
return START_STICKY;
}