|
|
@ -23,6 +23,7 @@ import android.os.Message;
|
|
|
|
import android.provider.ContactsContract;
|
|
|
|
import android.provider.ContactsContract;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.ViewGroup;
|
|
|
@ -35,6 +36,8 @@ import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
@ -44,10 +47,10 @@ import java.net.InetAddress;
|
|
|
|
import java.net.*;
|
|
|
|
import java.net.*;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.widget.ToggleButton;
|
|
|
|
import android.widget.ToggleButton;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class VideoActivity extends AppCompatActivity {
|
|
|
|
public class VideoActivity extends AppCompatActivity {
|
|
|
|
private final static String SEND_to_IP = "192.168.39.47"; //填写接收方IP
|
|
|
|
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为同一个
|
|
|
|
private final static String Recieve_IP = "192.168.39.47";//填服务器IP,与send_to_IP为同一个
|
|
|
@ -76,12 +79,17 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
private ImageButton TakeOff_btn;
|
|
|
|
private ImageButton TakeOff_btn;
|
|
|
|
private ImageButton Landing_btn;
|
|
|
|
private ImageButton Landing_btn;
|
|
|
|
private ToggleButton Mode_btn;
|
|
|
|
private ToggleButton Mode_btn;
|
|
|
|
|
|
|
|
private ImageButton selfHome_btn;
|
|
|
|
private String mangle;
|
|
|
|
private String mangle;
|
|
|
|
private float mdistance;
|
|
|
|
private float mdistance;
|
|
|
|
// 创建一个可缓存的线程池
|
|
|
|
// 创建一个可缓存的线程池
|
|
|
|
private ExecutorService mThreadPool = Executors.newCachedThreadPool();
|
|
|
|
private ExecutorService mThreadPool = Executors.newCachedThreadPool();
|
|
|
|
private UdpDataThread warning_thread;
|
|
|
|
private UdpDataThread warning_thread;
|
|
|
|
private TextView battery_warning;
|
|
|
|
private TextView battery_warning;
|
|
|
|
|
|
|
|
private ImageButton show_status;
|
|
|
|
|
|
|
|
private String batteryStatus="未获取";
|
|
|
|
|
|
|
|
private String speedStatus="未获取";
|
|
|
|
|
|
|
|
private String distanceStatus="未获取";
|
|
|
|
//ReceiveData()显示到imageView
|
|
|
|
//ReceiveData()显示到imageView
|
|
|
|
//manager部分:打开线程,接收
|
|
|
|
//manager部分:打开线程,接收
|
|
|
|
//model部分:发送视频流
|
|
|
|
//model部分:发送视频流
|
|
|
@ -103,7 +111,26 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
TakeOff_btn = findViewById(R.id.takeoff_button);
|
|
|
|
TakeOff_btn = findViewById(R.id.takeoff_button);
|
|
|
|
Mode_btn = findViewById(R.id.toggleButton2);
|
|
|
|
Mode_btn = findViewById(R.id.toggleButton2);
|
|
|
|
Setting_btn = findViewById(R.id.setting_button);
|
|
|
|
Setting_btn = findViewById(R.id.setting_button);
|
|
|
|
|
|
|
|
Setting_btn.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
|
|
|
Intent intent = new Intent(VideoActivity.this, SettingActivity.class);
|
|
|
|
|
|
|
|
startActivity(intent);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
selfHome_btn = findViewById(R.id.self_home);
|
|
|
|
|
|
|
|
selfHome_btn.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void onClick(View view) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
sendCommand("DIRECTHOME");
|
|
|
|
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
battery_warning =findViewById(R.id.warning);
|
|
|
|
battery_warning =findViewById(R.id.warning);
|
|
|
|
|
|
|
|
show_status=findViewById(R.id.show_status_button);
|
|
|
|
battery_warning.setTextColor(Color.RED);
|
|
|
|
battery_warning.setTextColor(Color.RED);
|
|
|
|
Animation anim = new AlphaAnimation(0.0f, 1.0f);
|
|
|
|
Animation anim = new AlphaAnimation(0.0f, 1.0f);
|
|
|
|
anim.setDuration(500); // 闪烁时间间隔
|
|
|
|
anim.setDuration(500); // 闪烁时间间隔
|
|
|
@ -121,6 +148,13 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
show_status.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
|
|
|
// 在这里编写 ImageButton 的点击事件处理逻辑
|
|
|
|
|
|
|
|
showPopup(speedStatus,batteryStatus,distanceStatus);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private void Control_Button_Init(){
|
|
|
|
private void Control_Button_Init(){
|
|
|
|
|
|
|
|
|
|
|
@ -147,16 +181,16 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
Forward_btn.setOnClickListener(new View.OnClickListener() {
|
|
|
|
Forward_btn.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
public void onClick(View v) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
sendCommand("FORWARD");
|
|
|
|
sendCommand("FORWARD");
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Log.i(TAG, "onClick: FFFFFFFForward");
|
|
|
|
Log.i(TAG, "onClick: FFFFFFFForward");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
Back_btn.setOnClickListener(new View.OnClickListener() {
|
|
|
|
Back_btn.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
public void onClick(View v) {
|
|
|
@ -234,13 +268,7 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
Log.i(TAG, "onClick: TTTTTTTTTTTTurnRRRRRRRRRRight");
|
|
|
|
Log.i(TAG, "onClick: TTTTTTTTTTTTurnRRRRRRRRRRight");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
Setting_btn.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
|
|
|
Intent intent = new Intent(VideoActivity.this, SettingActivity.class);
|
|
|
|
|
|
|
|
startActivity(intent);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Condition_Init() throws UnknownHostException {
|
|
|
|
private void Condition_Init() throws UnknownHostException {
|
|
|
@ -304,7 +332,6 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
super.handleMessage(msg);
|
|
|
|
super.handleMessage(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* UDP数据接受线程
|
|
|
|
* UDP数据接受线程
|
|
|
|
* */
|
|
|
|
* */
|
|
|
@ -375,7 +402,6 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
// 创建 DatagramSocket 对象并发送数据报文
|
|
|
|
// 创建 DatagramSocket 对象并发送数据报文
|
|
|
|
DatagramSocket socket = new DatagramSocket();
|
|
|
|
DatagramSocket socket = new DatagramSocket();
|
|
|
|
socket.send(packet);
|
|
|
|
socket.send(packet);
|
|
|
|
|
|
|
|
|
|
|
|
// 关闭 DatagramSocket 对象
|
|
|
|
// 关闭 DatagramSocket 对象
|
|
|
|
socket.close();
|
|
|
|
socket.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
@ -393,14 +419,14 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
*UDP数据接收线程
|
|
|
|
*UDP数据接收线程
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class UdpDataThread extends Thread {
|
|
|
|
public class UdpDataThread extends Thread {
|
|
|
|
private DatagramSocket socket;
|
|
|
|
private DatagramSocket socket;
|
|
|
|
private boolean running;
|
|
|
|
private boolean running;
|
|
|
|
private byte[] buffer;
|
|
|
|
private byte[] buffer;
|
|
|
|
private static final int BUFFER_SIZE = 1024; // 设置接收缓冲区大小
|
|
|
|
private static final int BUFFER_SIZE = 1024; // 设置接收缓冲区大小
|
|
|
|
|
|
|
|
private JSONObject jsonObject;
|
|
|
|
public UdpDataThread() {
|
|
|
|
public UdpDataThread() {
|
|
|
|
buffer = new byte[BUFFER_SIZE];
|
|
|
|
buffer = new byte[BUFFER_SIZE];
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -408,21 +434,26 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
public void run() {
|
|
|
|
running = true;
|
|
|
|
running = true;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
socket = new DatagramSocket(6666); // 设置接收端口号
|
|
|
|
socket = new DatagramSocket(6666); // 设置接收端口号
|
|
|
|
while (running) {
|
|
|
|
while (running) {
|
|
|
|
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
|
|
|
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
|
|
|
socket.receive(packet);
|
|
|
|
socket.receive(packet);
|
|
|
|
String receivedData = new String(packet.getData(), 0, packet.getLength());
|
|
|
|
String receivedData = new String(packet.getData(), 0, packet.getLength(), "UTF-8");
|
|
|
|
if (receivedData.equals("电量低于10%")) {
|
|
|
|
jsonObject = new JSONObject(receivedData);
|
|
|
|
battery_warning.setText(receivedData);}else{
|
|
|
|
batteryStatus = jsonObject.getString("battery");
|
|
|
|
|
|
|
|
speedStatus = jsonObject.getString("speed");
|
|
|
|
|
|
|
|
distanceStatus = jsonObject.getString("distance");
|
|
|
|
|
|
|
|
if (batteryStatus.equals("10")) {
|
|
|
|
|
|
|
|
battery_warning.setText("电量仅剩"+batteryStatus);}else{
|
|
|
|
battery_warning.setText("");
|
|
|
|
battery_warning.setText("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
System.out.println("接收到数据:" + receivedData);
|
|
|
|
System.out.println("接收到数据:" + batteryStatus);
|
|
|
|
|
|
|
|
System.out.println("接收到数据:" + speedStatus);
|
|
|
|
|
|
|
|
System.out.println("接收到数据:" + distanceStatus);
|
|
|
|
// 在这里处理收到的数据,可以根据实际需求进行解析或其他操作
|
|
|
|
// 在这里处理收到的数据,可以根据实际需求进行解析或其他操作
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException | JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
if (socket != null) {
|
|
|
|
if (socket != null) {
|
|
|
@ -435,43 +466,29 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
running = false;
|
|
|
|
running = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//初始化UDP发送线程
|
|
|
|
public void showPopup(String speedStatus,String batteryStatus,String distanceStatus) {
|
|
|
|
// private static void sendCommand(String Sendstr) throws UnknownHostException {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
|
|
// InetAddress sendIP = InetAddress.getByName(SEND_to_IP); // 目标 IP 地址
|
|
|
|
LayoutInflater inflater = getLayoutInflater();
|
|
|
|
// int sendPort = 8888; // 目标端口号
|
|
|
|
View dialogView = inflater.inflate(R.layout.custom_dialog, null);
|
|
|
|
// byte[] sendData = Sendstr.getBytes(); // 要发送的数据
|
|
|
|
TextView speedstatus = dialogView.findViewById(R.id.speed);
|
|
|
|
// UdpSendThread sendCommandThread = new UdpSendThread(sendIP, sendPort, sendData); // 创建新的线程对象
|
|
|
|
speedstatus.setText("当前速度为:"+speedStatus);
|
|
|
|
// sendCommandThread.start(); // 启动线程发送数据
|
|
|
|
TextView batterystatus = dialogView.findViewById(R.id.battery);
|
|
|
|
// }
|
|
|
|
batterystatus.setText("当前电量为:"+batteryStatus);
|
|
|
|
|
|
|
|
TextView distancestatus = dialogView.findViewById(R.id.distance);
|
|
|
|
|
|
|
|
distancestatus.setText("当前距离为:"+distanceStatus);
|
|
|
|
|
|
|
|
builder.setView(dialogView);
|
|
|
|
|
|
|
|
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
//设置按钮
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
// public void showDialog(View view) {
|
|
|
|
// 点击确定按钮后的操作
|
|
|
|
// Dialog dialog = new Dialog(VideoActivity.this);
|
|
|
|
dialog.dismiss();
|
|
|
|
// dialog.setContentView(R.layout.dialog_takeoff);
|
|
|
|
}
|
|
|
|
//
|
|
|
|
});
|
|
|
|
// SlideUnlockView slideUnlockView = (SlideUnlockView) dialog.findViewById(R.id.slide_unlock_view);
|
|
|
|
|
|
|
|
// slideUnlockView.setOnUnlockListener(new SlideUnlockView.OnUnlockListener() {
|
|
|
|
AlertDialog dialog = builder.create();
|
|
|
|
// @Override
|
|
|
|
dialog.show();
|
|
|
|
// public void onUnlock() {
|
|
|
|
}
|
|
|
|
// // 在用户滑动解锁后执行操作
|
|
|
|
|
|
|
|
//// try {
|
|
|
|
|
|
|
|
//// sendCommand("TAKEOFF"+"___"+angle+"___"+String.valueOf(distance));
|
|
|
|
|
|
|
|
//// } catch (UnknownHostException e) {
|
|
|
|
|
|
|
|
//// throw new RuntimeException(e);
|
|
|
|
|
|
|
|
//// }
|
|
|
|
|
|
|
|
// UdpReceiveThread udpReceiveThread = new UdpReceiveThread();
|
|
|
|
|
|
|
|
// udpReceiveThread.start();
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// Log.i(TAG, "onClick: 2222222222222222222222222");
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// dialog.show();
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//////////////////////////////滑动开关//////////////////////////////
|
|
|
|
//////////////////////////////滑动开关//////////////////////////////
|
|
|
|
public static class SlideUnlockView extends View{
|
|
|
|
public static class SlideUnlockView extends View{
|
|
|
|
|
|
|
|
|
|
|
@ -653,4 +670,25 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
return super.onTouchEvent(event);
|
|
|
|
return super.onTouchEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void shutdown(){
|
|
|
|
|
|
|
|
warning_thread.stopThread();
|
|
|
|
|
|
|
|
mThreadPool.shutdownNow();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void onBackPressed() {
|
|
|
|
|
|
|
|
// 返回到 MainActivity
|
|
|
|
|
|
|
|
shutdown();
|
|
|
|
|
|
|
|
Intent intent = new Intent(this, MainActivity.class);
|
|
|
|
|
|
|
|
startActivity(intent);
|
|
|
|
|
|
|
|
finish(); // 结束 VideoActivity
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
|
|
|
// 关闭线程
|
|
|
|
|
|
|
|
shutdown();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|