I am doing a project which sends data over a WiFi router to a destination IP. I have placed a switch button. When the switch button is turned on, some pre-written data is sent to the IP.
I am unable to listen anything at the destination. I have attached the code.
MainActivity.java:
package com.project.saket.switchsocket;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.CompoundButton.OnCheckedChangeListener;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class MainActivity extends AppCompatActivity {
private Switch switch1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
switch1 = (Switch) findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) {
new SuriSocket("$ASAS,F1#");
} else {
new SuriSocket("$ASAS,F0#");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
SuriSocket.java:
package com.project.saket.switchsocket;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class SuriSocket {
private Socket output;
private static final int SERVERPORT = 8080;
private static final String SERVER_IP = "192.168.0.131";
private boolean setup() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
output = new Socket(serverAddr, SERVERPORT);
} catch (UnknownHostException e1) {
e1.printStackTrace();
return false;
} catch (IOException e1) {
e1.printStackTrace();
return false;
}
return true;
}
public SuriSocket(String sMessage) {
if (setup()) {
try {
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(output.getOutputStream())),
true);
out.println(sMessage);
out.flush();
out.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}