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;
}
}