Android programming realizes information transmission based on WiFi direct connection

The code is as follows:
1. First declare the relevant variables:

private ServerSocket serverSocket = null;
    private int ServerPort = 29898;
    private Socket socket = null;
    private OutputStream outputStream = null;
    private InputStream inputStream = null;
    private PrintWriter printWriter = null;
    private BufferedReader reader = null;

2. Core code writing:

            new Thread(new Runnable() {
    
    
                @Override
                public void run() {
    
    
                    try {
    
    
                        serverSocket = new ServerSocket(ServerPort);
                        Log.d(TAG,"Create A ServerSocket...");
                        socket = serverSocket.accept();
                        Log.d(TAG, "Client is Connected...");
                        outputStream = socket.getOutputStream();
                        inputStream = socket.getInputStream();
                        printWriter = new PrintWriter(outputStream,true);
                        reader = new BufferedReader(new InputStreamReader(inputStream));
                        if (isAcceptDevice){
    
    
                            //作为接收方,进行一些操作
                            while (true){
    
    
                                //生成Json字符串格式的JSON对象
                                String message = reader.readLine();
                                JSONObject jsonObject= null;
                                try {
    
    
                                    jsonObject = new JSONObject(message);
                                    String AssisDeviceName = jsonObject.getString("devicename");
                                    String AssisTime = jsonObject.getString("time");
                                } catch (JSONException e) {
    
    
                                    e.printStackTrace();
                                    Log.d(TAG, "Reading Jsonobject Error:"+e);
                                }
                            }
                        }else{
    
    
                            if (socket.isConnected()){
    
    
                                if (!socket.isOutputShutdown()){
    
    
                                    //向接收方发送消息
                                    JSONObject jsonObject=new JSONObject();
            try {
    
    
                jsonObject.put("devicename", MainActivity.MyDeviceName);
                jsonObject.put("time", time);
            } catch (JSONException e) {
    
    
                e.printStackTrace();
            }
            result=jsonObject.toString();
            printWriter.println(result);
            printWriter.flush();
            Log.d(TAG,"Send:"+result);
                                }
                            }
                        }
                    } catch (IOException e) {
    
    
                        e.printStackTrace();
                    }finally {
    
    
                    }
                }
            }).start();

Note:
1. It is best to create a new thread to reduce the impact of the time-consuming operation of transferring data;
2. The above code is just an example of transferring "device name" and "current time", and you can add data to Json according to your needs in the string.
——————————————————————————————
Finally, post my personal public account: WeChat search "Chaqian" or scan the picture below. Usually, some programming-related articles will be updated, and everyone is welcome to pay attention~
tea move

Guess you like

Origin blog.csdn.net/weixin_46269688/article/details/110455966