|
|
|
@ -35,6 +35,8 @@ import android.widget.ImageView;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
@ -44,10 +46,10 @@ import java.net.InetAddress;
|
|
|
|
|
import java.net.*;
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.widget.ToggleButton;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class VideoActivity extends AppCompatActivity {
|
|
|
|
|
private final static String SEND_to_IP = "192.168.39.47"; //填写接收方IP
|
|
|
|
|
private final static String Recieve_IP = "192.168.39.47";//填服务器IP,与send_to_IP为同一个
|
|
|
|
@ -304,7 +306,6 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
|
super.handleMessage(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* UDP数据接受线程
|
|
|
|
|
* */
|
|
|
|
@ -375,7 +376,6 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
|
// 创建 DatagramSocket 对象并发送数据报文
|
|
|
|
|
DatagramSocket socket = new DatagramSocket();
|
|
|
|
|
socket.send(packet);
|
|
|
|
|
|
|
|
|
|
// 关闭 DatagramSocket 对象
|
|
|
|
|
socket.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
@ -400,7 +400,7 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
|
private boolean running;
|
|
|
|
|
private byte[] buffer;
|
|
|
|
|
private static final int BUFFER_SIZE = 1024; // 设置接收缓冲区大小
|
|
|
|
|
|
|
|
|
|
private JSONObject jsonObject;
|
|
|
|
|
public UdpDataThread() {
|
|
|
|
|
buffer = new byte[BUFFER_SIZE];
|
|
|
|
|
}
|
|
|
|
@ -414,15 +414,17 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
|
while (running) {
|
|
|
|
|
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
|
|
|
|
socket.receive(packet);
|
|
|
|
|
String receivedData = new String(packet.getData(), 0, packet.getLength());
|
|
|
|
|
if (receivedData.equals("电量低于10%")) {
|
|
|
|
|
battery_warning.setText(receivedData);}else{
|
|
|
|
|
String receivedData = new String(packet.getData(), 0, packet.getLength(), "UTF-8");
|
|
|
|
|
jsonObject = new JSONObject(receivedData);
|
|
|
|
|
String batteryStatus = jsonObject.getString("battery");
|
|
|
|
|
if (batteryStatus.equals("10")) {
|
|
|
|
|
battery_warning.setText("电量仅剩"+batteryStatus+"%");}else{
|
|
|
|
|
battery_warning.setText("");
|
|
|
|
|
}
|
|
|
|
|
System.out.println("接收到数据:" + receivedData);
|
|
|
|
|
System.out.println("接收到数据:" + batteryStatus);
|
|
|
|
|
// 在这里处理收到的数据,可以根据实际需求进行解析或其他操作
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
} catch (IOException | JSONException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
if (socket != null) {
|
|
|
|
|