怀念不如相见X 2022-07-06 01:50 采纳率: 100%
浏览 28
已结题

APP使用TCP连接下位机时出现错误,如何解决?

Android APP控制Stm32单片机继电器时,TCP连接出现错误,但是在使用网络调试助手和APP连接时就可以正常使用,该怎么解决?
TCPservice.class
package com.example.smarthome;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Looper;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

public class TCPService extends Service {
public static Socket socket;
public static PrintStream output;
boolean conn = false;
public String ip="192.168.31.179";
public String port="8080";
/*
String ip="192.168.31.179";
String port="8080";
*/

public TCPService() {
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    MyThread myThread = new MyThread();
    new Thread(myThread).start();
    ip = intent.getStringExtra("ip");
    port = intent.getStringExtra("port");
    return super.onStartCommand(intent, flags, startId);
}

//socket连接线程
class MyThread implements Runnable{
    @Override
    public void run(){
        try {
            socket = new Socket();
            SocketAddress socAddress = new InetSocketAddress(ip ,Integer.valueOf(port));
            socket.connect(socAddress, 5000);
            InputStream inputstream = socket.getInputStream();
            /* 获取输出流 */
            output = new PrintStream(socket.getOutputStream(), true, "utf-8");
            conn = true;
            byte buffer[] = new byte[1024];//接收数组的长度
            int len2 ;
            String receiveData;
            //非阻塞式连接
            while(conn){
                //接收网络数据
                if( (len2 = inputstream.read(buffer)) != -1){
                    receiveData = new String(buffer, 0, len2);
                    Intent CMDintent = new Intent();
                    CMDintent.setAction("com.example.communication.data");
                    CMDintent.putExtra("data", buffer);   //buffer为数组,receivedata为文本
                    sendBroadcast(CMDintent);
                }else{
                    break;
                }
            }
            output.close();
            socket.close();
            Looper.loop();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            // Looper.prepare();
        }
    }

};

//发送方法((可以把参数改成Byte[]):
public static void send(final byte[] arr)
{
    new Thread(new Runnable() {
        public void run() {
            if (socket.isConnected()) {
                try {
                    output.write(arr);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}

}

MeFragment.class

package com.example.smarthome;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

public class MeFragment extends Fragment{

private TextView tev_uname, tev_sex, tev_ip, tev_port, tev_ban;
Button bt1, bt_conn;
String ip="192.168.31.179";
String port="8080";

/* String ip="10.10.100.101";
String port="8899";*/
String banben = "1.0";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View Melayout = inflater.inflate(R.layout.fragment_me, container, false);

    tev_uname = Melayout.findViewById(R.id.me_uname);
    tev_sex = Melayout.findViewById(R.id.me_sex);
    tev_ip = Melayout.findViewById(R.id.ip);
    tev_port = Melayout.findViewById(R.id.port);
    tev_ban = Melayout.findViewById(R.id.banben);




    Intent intent = getActivity().getIntent();
    String uname = intent.getStringExtra("uname");
    String sex = intent.getStringExtra("sex");

    tev_uname.setText("   用户名:" + uname);
    tev_sex.setText("   性    别:" + sex);
    tev_ban.setText("版本号:" + banben);


    bt1 = Melayout.findViewById(R.id.btn_quit);
    bt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), MainActivity.class);
            startActivity(intent);
        }
    });

    tev_ip.setText(ip);
    tev_port.setText(port);

    bt_conn = Melayout.findViewById(R.id.btn_connect);
    bt_conn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ip = tev_ip.getText().toString();
            port = tev_port.getText().toString();
            Intent intent1 = new Intent(getActivity(), TCPService.class);
            intent1.putExtra("ip", ip);
            intent1.putExtra("port", port);
            //startActivity(intent1);
            getActivity().startService(intent1);
        }
    });

    return Melayout;
}

}

img

img

  • 写回答

2条回答 默认 最新

  • 怀念不如相见X 2022-07-06 15:31
    关注

    解决了,今天windows defender突然弹了个是否用于网络通信的东西。点下确认就ok了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月14日
  • 已采纳回答 7月6日
  • 修改了问题 7月6日
  • 创建了问题 7月6日

悬赏问题

  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动