Wifi和Android应用程序通信

时间:2015-02-04 06:54:22

标签: android android-wifi serial-communication

嗨,我正在开发一个应用程序,我想将某种数据发送到我连接的WiFi设备。如何我可以这样做,如果我想远程使用它所以我需要我尝试与WiFi通信但我无法连接或配对

public static String ipAddress;// ur ip
    public static int portNumber;// portnumber

    private Socket client;

    private OutputStreamWriter printwriter;
    private String message;


new Thread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        try {
                            client = new Socket(ipAddress, portNumber);
                            printwriter = new OutputStreamWriter(client
                                    .getOutputStream(), "ISO-8859-1");
                            printwriter.write("any message");
                            printwriter.flush();
                            printwriter.close();
                            client.close();
                        }

                        catch (UnknownHostException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }).start();

1 个答案:

答案 0 :(得分:0)

你得到了什么错误?

要使您发布的代码有效,您可能必须在清单中获取Internet权限

<uses-permission android:name="android.permission.INTERNET" />

http://developer.android.com/reference/android/Manifest.permission.html#INTERNET

此外,如果你需要先做wifi,请先检查wifi(并在清单中请求相应的许可)