Android编程实现基于WiFi直连的信息传输

代码如下:
1.首先进行相关变量的声明:

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.核心代码编写:

            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();

注:
1.最好新建一个线程,将传输数据这种耗时操作的影响降低;
2.上述代码只是以传输“设备名”和“当前时间”为例,可以按照个人需要将数据添加到Json字符串中。
——————————————————————————
最后贴一下我的个人公众号:微信搜索“茶迁”或扫描下图。平时会更新一些编程相关的文章,欢迎大家关注~
茶迁

猜你喜欢

转载自blog.csdn.net/weixin_46269688/article/details/110455966