Android应用程序连接到本地IP设备,这是一个访问点

时间:2016-01-21 20:42:53

标签: android sockets android-wifi esp8266

我有一个硬件设备,它充当接入点。 我可以将移动设备的wifi与路由器断开连接,然后将其连接到硬件设备的wifi。 设备IP具有本地IP并托管Web服务器。 我打开与设备的套接字连接并ping它,但在我关闭设备的移动数据之前,它无法连接到本地IP。 有没有办法在不关闭移动数据的情况下与设备(即接入点)进行通信。

代码: - 下面是创建套接字数据的类

class ClientAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            Log.d("SocketProg", "Creating socket");
            String result = null;
            try {
                Log.d("SocketProg", String.valueOf(params[0]));
                Log.d("SocketProg", String.valueOf(params[1]));
                Log.d("SocketProg", String.valueOf(params[2]));
                //Create a client socket and define internet address and the port of the server
                Socket socket = new Socket(params[0],
                        Integer.parseInt(params[1]));
                //Get the input stream of the client socket
                InputStream is = socket.getInputStream();
                //Get the output stream of the client socket
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                //Write data to the output stream of the client socket
                out.println(params[2]);
                out.flush();
                Log.d("SocketProg", "Sending data");
                //Buffer the data coming from the input stream
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(is));
                //Read data in the input buffer
                result = br.readLine();
                //Close the client socket
                socket.close();
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }
    }

0 个答案:

没有答案