Android デバイスとwiFly (RN-171) モジュール間の通信をセットアップしようとしています。Android デバイスでアドホック ネットワークを作成し、wiFly をネットワークに接続します。wiFly は 169.254.1.1:2000 でリッスンするように構成されています。アプリでソケットを作成して、wiFly と通信します。コード:
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;
}
}
StrictMode を使用して申し訳ありません。これが実行されると、AsyncTask に切り替わります。以下は警告メッセージです。
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.<init>(Socket.java:177)
03-29 21:55:40.362: W/System.err(15118): at java.net.Socket.<init>(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.
Android 4.0.4で実行し、マニフェストファイルに必要なすべての許可を含めました。