Reputation: 63
I'm currently developing an android application which can send commands to an AV Receiver through the network. I have gotten as far as making a button and coding it so that when the button is clicked it will send the command PWON\r to the IP and port specified. I have also given full internet permission in the manifest file. The problem is that the app stops working as soon as the button is pressed. I'm not sure what the problem is.
CODE:
package av.control.test;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AVControlActivity extends Activity {
Button mbutt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mbutt = (Button)findViewById(R.id.Button);
}
public void onClick(View view) throws UnknownHostException, IOException
{
new Thread() {
public void run() {
Socket socket = null;
try {
socket = new Socket("192.168.1.74", 23);
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
dataOutputStream.Object BUFFER = null;
write(BUFFER);
String buffer="PWON\r";
OutputStream os=socket.getOutputStream();
os.write(buffer.getBytes());
} catch(IOException e)
{
//error code
}
}
}.start();
}
private void write(Object BUFFER) {
// TODO Auto-generated method stub
}
}
Here is the main.xml as well if that is of any use:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Power On"
android:onClick="onClick" />
</LinearLayout>
I would appreciate any help given.
EDIT:
LogCat
09-15 14:31:30.742: I/dalvikvm(1209): threadid=3: reacting to signal 3
09-15 14:31:31.301: E/dalvikvm(1209): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
09-15 14:31:31.322: I/dalvikvm(1209): threadid=3: reacting to signal 3
09-15 14:31:31.421: I/dalvikvm(1209): Wrote stack traces to '/data/anr/traces.txt'
09-15 14:31:31.801: I/dalvikvm(1209): threadid=3: reacting to signal 3
09-15 14:31:31.901: I/dalvikvm(1209): Wrote stack traces to '/data/anr/traces.txt'
09-15 14:31:32.381: I/dalvikvm(1209): threadid=3: reacting to signal 3
09-15 14:31:32.451: I/dalvikvm(1209): Wrote stack traces to '/data/anr/traces.txt'
09-15 14:31:32.711: D/gralloc_goldfish(1209): Emulator without GPU emulation detected.
09-15 14:31:32.843: I/dalvikvm(1209): threadid=3: reacting to signal 3
09-15 14:31:32.901: I/dalvikvm(1209): Wrote stack traces to '/data/anr/traces.txt'
09-15 14:34:53.081: W/System.err(1209): java.net.ConnectException: failed to connect to /192.168.1.74 (port 23): connect failed: ETIMEDOUT (Connection timed out)
09-15 14:34:53.091: W/System.err(1209): at libcore.io.IoBridge.connect(IoBridge.java:114)
09-15 14:34:53.091: W/System.err(1209): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-15 14:34:53.101: W/System.err(1209): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
09-15 14:34:53.121: W/System.err(1209): at java.net.Socket.startupSocket(Socket.java:566)
09-15 14:34:53.121: W/System.err(1209): at java.net.Socket.tryAllAddresses(Socket.java:127)
09-15 14:34:53.132: W/System.err(1209): at java.net.Socket.<init>(Socket.java:177)
09-15 14:34:53.132: W/System.err(1209): at java.net.Socket.<init>(Socket.java:149)
09-15 14:34:53.161: W/System.err(1209): at av.control.test.AVControlActivity$1.run(AVControlActivity.java:27)
09-15 14:34:53.161: W/System.err(1209): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
09-15 14:34:53.182: W/System.err(1209): at libcore.io.Posix.connect(Native Method)
09-15 14:34:53.182: W/System.err(1209): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
09-15 14:34:53.191: W/System.err(1209): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
09-15 14:34:53.211: W/System.err(1209): at libcore.io.IoBridge.connect(IoBridge.java:112)
09-15 14:34:53.211: W/System.err(1209): ... 7 more
09-15 14:34:53.222: W/dalvikvm(1209): threadid=12: thread exiting with uncaught exception (group=0x409c01f8)
09-15 14:34:53.241: E/AndroidRuntime(1209): FATAL EXCEPTION: Thread-121
09-15 14:34:53.241: E/AndroidRuntime(1209): java.lang.NullPointerException
09-15 14:34:53.241: E/AndroidRuntime(1209): at av.control.test.AVControlActivity$1.run(AVControlActivity.java:39)
Upvotes: 0
Views: 1336
Reputation: 24820
You have a null pointer exception on
OutputStream os=socket.getOutputStream();
This is because below line gave an exception due to timeout.
socket = new Socket("192.168.1.74", 23);
So socket is still null. I dont think you should continue operation if socket was not created. So change your code like below.
try {
socket = new Socket("192.168.1.74", 23);
dataOutputStream.Object BUFFER = null;
write(BUFFER);
String buffer="PWON\r";
OutputStream os=socket.getOutputStream();
os.write(buffer.getBytes());
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
Further do check why you are getting timed out. It might be due to device not being on same network, or port not being open
Upvotes: 1