我正在尝试在我的 android 设备和wiFly (RN-171) 模块之间建立通信。我在 android 设备上创建了一个 ad-hoc 网络并将 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;
}
}
很抱歉使用严格模式。一旦运行,将切换到 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 上运行它,并且在 Manifest 文件中包含了所有必要的权限。