Reputation: 17
Below is my code for my main activity, the program run with no error but i cannot get the IP Address of a server which is run by an arduino
with WiFi
shield. i want to update the ListView
when he sees a server and want to display its IP Address.
public class MainActivity extends Activity {
ArrayList<PostList> server;
ArrayAdapter<PostList> adapter;
ListView server_list;
Socket socket = null;
int port = 50000;
int timeout = 1000;
final ExecutorService es = Executors.newFixedThreadPool(20);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button scan = (Button)findViewById(R.id.scan);
server_list = (ListView)findViewById(R.id.serverlist);
server = new ArrayList<PostList>();
scan.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String iIPv4 = "192.168.254.0";
iIPv4 = iIPv4.substring(0, iIPv4.lastIndexOf("."));
iIPv4 += ".";
for (int i = 1; i < 255; i++) {
findSocket(es, iIPv4 + i, port, timeout);
}
adapter = new PostAdapter(MainActivity.this, server);
server_list.setAdapter(adapter);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public static Future<Boolean> findSocket(final ExecutorService es,
final String ip, final int port, final int timeout) {
return es.submit(new Callable<Boolean>() {
@Override
public Boolean call() {
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), timeout);
socket.close();
ArrayList<PostList> server = new ArrayList<PostList>();
server.add(new PostList(ip));
return true;
} catch (Exception ex) {
return false;
}
}
});
}
}
Upvotes: 0
Views: 260
Reputation: 17
got an answer, but another problem is that it runs too slow to scan the ip address;
public class MainActivity extends Activity {
private Button connectPhones;
private boolean connected = false;
private String serverIP = "192.168.254.";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectPhones = (Button) findViewById(R.id.scan);
connectPhones.setOnClickListener(connectListener);
}
private OnClickListener connectListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (!connected) {
Thread cThread = new Thread(new ClientThread());
cThread.start();
}
}
};
public class ClientThread implements Runnable {
public void run() {
for (int i = 0; i < 255; i++){
String ipadd = serverIP + i;
try {
Log.d("ClientActivity", "C: Connecting...");
Socket socket = new Socket(ipadd, 50000);
connected = true;
if (connected) {
try {
Log.d("ClientActivity", "C: Sending command.");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
out.println("Hey Server im " + ipadd);
Log.d("ClientActivity", "C: Sent.");
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
}
socket.close();
Log.d("ClientActivity", "C: Closed.");
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
connected = false;
}
}
}
}
}
Upvotes: 1