Note that there are some explanatory texts on larger screens.

plurals
  1. POConnection error while communicating to wiFly
    text
    copied!<p>I am trying to set up communication between my android device and <a href="https://www.sparkfun.com/products/9954" rel="nofollow" title="wiFly">wiFly</a>(RN-171) module. I create an ad-hoc network on the android device and connect the wiFly to the network. The wiFly is configured to listen on 169.254.1.1:2000. I create socket in the app to communicate with the wiFly. Code:</p> <pre><code>import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import android.os.Bundle; import android.os.StrictMode; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .permitAll().build(); StrictMode.setThreadPolicy(policy); Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; try { echoSocket = new Socket("169.254.1.1", 2000); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: wiFly."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to: wiFly."); } BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("echo: " + in.readLine()); } out.close(); in.close(); stdIn.close(); echoSocket.close(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } </code></pre> <p>Sorry for using StrictMode. Will switch to AsyncTask once this runs. Following is the warning message:</p> <pre><code>03-29 21:55:40.352: W/AdHoc(15118): failed to connect to /169.254.1.1 (port 2000): connect failed: ENETUNREACH (Network is unreachable) 03-29 21:55:40.352: W/System.err(15118): java.net.ConnectException: failed to connect to /169.254.1.1 (port 2000): connect failed: ENETUNREACH (Network is unreachable) 03-29 21:55:40.362: W/System.err(15118): at libcore.io.IoBridge.connect(IoBridge.java:114) 03-29 21:55:40.362: W/System.err(15118): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 03-29 21:55:40.362: W/System.err(15118): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 03-29 21:55:40.362: W/System.err(15118): at java.net.Socket.startupSocket(Socket.java:566) 03-29 21:55:40.362: W/System.err(15118): at java.net.Socket.tryAllAddresses(Socket.java:127) 03-29 21:55:40.362: W/System.err(15118): at java.net.Socket.&lt;init&gt;(Socket.java:177) 03-29 21:55:40.362: W/System.err(15118): at java.net.Socket.&lt;init&gt;(Socket.java:149) 03-29 21:55:40.362: W/System.err(15118): at com.example.udp.MainActivity.onCreate(MainActivity.java:33) 03-29 21:55:40.362: W/System.err(15118): at android.app.Activity.performCreate(Activity.java:4492) 03-29 21:55:40.362: W/System.err(15118): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 03-29 21:55:40.362: W/System.err(15118): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 03-29 21:55:40.362: W/System.err(15118): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 03-29 21:55:40.372: W/System.err(15118): at android.app.ActivityThread.access$600(ActivityThread.java:123) 03-29 21:55:40.372: W/System.err(15118): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 03-29 21:55:40.372: W/System.err(15118): at android.os.Handler.dispatchMessage(Handler.java:99) 03-29 21:55:40.372: W/System.err(15118): at android.os.Looper.loop(Looper.java:137) 03-29 21:55:40.372: W/System.err(15118): at android.app.ActivityThread.main(ActivityThread.java:4424) 03-29 21:55:40.372: W/System.err(15118): at java.lang.reflect.Method.invokeNative(Native Method) 03-29 21:55:40.372: W/System.err(15118): at java.lang.reflect.Method.invoke(Method.java:511) 03-29 21:55:40.382: W/System.err(15118): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 03-29 21:55:40.382: W/System.err(15118): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 03-29 21:55:40.382: W/System.err(15118): at dalvik.system.NativeStart.main(Native Method) 03-29 21:55:40.382: W/System.err(15118): Caused by: libcore.io.ErrnoException: connect failed: ENETUNREACH (Network is unreachable) 03-29 21:55:40.382: W/System.err(15118): at libcore.io.Posix.connect(Native Method) 03-29 21:55:40.382: W/System.err(15118): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85) 03-29 21:55:40.382: W/System.err(15118): at libcore.io.IoBridge.connectErrno(IoBridge.java:127) 03-29 21:55:40.382: W/System.err(15118): at libcore.io.IoBridge.connect(IoBridge.java:112) 03-29 21:55:40.382: W/System.err(15118): ... 21 more 03-29 21:55:40.392: W/System.err(15118): Couldn't get I/O for the connection to: wiFly. </code></pre> <p>I run it on android 4.0.4, and have included all the necessary permission needed in the Manifest file.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload