|
|
|
@ -27,6 +27,7 @@ import android.view.MotionEvent;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
import android.widget.Button;
|
|
|
|
|
import android.widget.CompoundButton;
|
|
|
|
|
import android.widget.ImageButton;
|
|
|
|
|
import android.widget.ImageView;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
@ -43,6 +44,7 @@ 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
|
|
|
|
@ -71,6 +73,7 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
|
private ImageButton Setting_btn;
|
|
|
|
|
private ImageButton TakeOff_btn;
|
|
|
|
|
private ImageButton Landing_btn;
|
|
|
|
|
private ToggleButton Mode_btn;
|
|
|
|
|
private String mangle;
|
|
|
|
|
private float mdistance;
|
|
|
|
|
// 创建一个可缓存的线程池
|
|
|
|
@ -85,7 +88,6 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
|
setContentView(R.layout.activity_control);
|
|
|
|
|
imgShow = findViewById(R.id.imageView);
|
|
|
|
|
Forward_btn = findViewById(R.id.forward_button);
|
|
|
|
|
|
|
|
|
|
Back_btn = findViewById(R.id.backward_button);
|
|
|
|
|
Left_btn = findViewById(R.id.left_button);
|
|
|
|
|
Right_btn = findViewById(R.id.right_button);
|
|
|
|
@ -95,6 +97,19 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
|
Landing_btn = findViewById(R.id.landing_button);
|
|
|
|
|
TurnRight_btn = findViewById(R.id.rotate_right_button);
|
|
|
|
|
TakeOff_btn = findViewById(R.id.takeoff_button);
|
|
|
|
|
Mode_btn = findViewById(R.id.toggleButton2);
|
|
|
|
|
Setting_btn = findViewById(R.id.setting_button);
|
|
|
|
|
Intent i = getIntent();
|
|
|
|
|
mdistance = i.getFloatExtra("Distance",0);
|
|
|
|
|
mangle = i.getStringExtra("Angle");
|
|
|
|
|
try {
|
|
|
|
|
Condition_Init();
|
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private void Control_Button_Init(){
|
|
|
|
|
|
|
|
|
|
TakeOff_btn.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
@ -109,25 +124,14 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
|
Landing_btn.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
ReceiveHandler receiveHandler = new ReceiveHandler();
|
|
|
|
|
UdpReceiveThread StreamThread = new UdpReceiveThread(Recieve_IP,RECEIVE_PORT,receiveHandler);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
StreamThread.startListen();
|
|
|
|
|
StreamThread.start();
|
|
|
|
|
} catch (SocketException | UnknownHostException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
sendCommand("LAND");
|
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
Log.i(TAG, "onClick: LLLLLLLLLLLLLLLLLLand");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
Setting_btn = findViewById(R.id.setting_button);
|
|
|
|
|
Button_Init();
|
|
|
|
|
Intent i = getIntent();
|
|
|
|
|
mdistance = i.getFloatExtra("Distance",0);
|
|
|
|
|
mangle = i.getStringExtra("Angle");
|
|
|
|
|
}
|
|
|
|
|
private void Button_Init(){
|
|
|
|
|
Forward_btn.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
@ -224,6 +228,48 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Condition_Init() throws UnknownHostException {
|
|
|
|
|
sendCommand("AUTO");
|
|
|
|
|
ReceiveHandler receiveHandler = new ReceiveHandler();
|
|
|
|
|
UdpReceiveThread StreamThread = new UdpReceiveThread(Recieve_IP,RECEIVE_PORT,receiveHandler);
|
|
|
|
|
try {
|
|
|
|
|
StreamThread.startListen();
|
|
|
|
|
StreamThread.start();
|
|
|
|
|
} catch (SocketException | UnknownHostException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
Mode_btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
|
|
|
|
if (isChecked) {
|
|
|
|
|
// 执行功能2
|
|
|
|
|
try {
|
|
|
|
|
changetoManual();
|
|
|
|
|
Control_Button_Init();
|
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 执行功能1
|
|
|
|
|
try {
|
|
|
|
|
changetoAuto();
|
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void changetoAuto() throws UnknownHostException {
|
|
|
|
|
sendCommand("AUTO");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void changetoManual() throws UnknownHostException {
|
|
|
|
|
sendCommand("MAN");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ReceiveHandler extends Handler{
|
|
|
|
|
@Override
|
|
|
|
|
public void handleMessage(Message msg) {
|
|
|
|
@ -280,11 +326,9 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
|
byte[] inBuf = new byte[1024 * 1024];
|
|
|
|
|
DatagramPacket inPacket = new DatagramPacket(inBuf, inBuf.length);
|
|
|
|
|
reveSocket.receive(inPacket);
|
|
|
|
|
|
|
|
|
|
if (!inPacket.getAddress().equals(serverAddr)) {
|
|
|
|
|
throw new IOException("未知名的报文");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ByteArrayInputStream in = new ByteArrayInputStream(inPacket.getData());
|
|
|
|
|
receiveHandler.sendEmptyMessage(1);
|
|
|
|
|
bp = BitmapFactory.decodeStream(in);
|
|
|
|
@ -333,34 +377,7 @@ public class VideoActivity extends AppCompatActivity {
|
|
|
|
|
SendRunnable sendRunnable1 = new SendRunnable(sendData, address, SEND_PORT);
|
|
|
|
|
mThreadPool.execute(sendRunnable1);
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* UDP数据发送线程
|
|
|
|
|
* */
|
|
|
|
|
public static class UdpSendThread extends Thread {
|
|
|
|
|
|
|
|
|
|
private InetAddress sendToIP; // 目标 IP 地址
|
|
|
|
|
private int sendPort; // 目标端口号
|
|
|
|
|
private byte[] sendData; // 要发送的数据
|
|
|
|
|
|
|
|
|
|
public UdpSendThread(InetAddress sendToIP, int sendPort, byte[] sendData) {
|
|
|
|
|
this.sendToIP = sendToIP;
|
|
|
|
|
this.sendPort = sendPort;
|
|
|
|
|
this.sendData = sendData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
try {
|
|
|
|
|
DatagramSocket sendSocket = new DatagramSocket(); // 创建一个新的 DatagramSocket 对象
|
|
|
|
|
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, sendToIP, sendPort); // 将要发送的数据打包成一个 DatagramPacket 对象
|
|
|
|
|
sendSocket.send(sendPacket); // 发送 DatagramPacket
|
|
|
|
|
sendSocket.close(); // 发送完毕后关闭 DatagramSocket
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//初始化UDP发送线程
|
|
|
|
|
// private static void sendCommand(String Sendstr) throws UnknownHostException {
|
|
|
|
|
// InetAddress sendIP = InetAddress.getByName(SEND_to_IP); // 目标 IP 地址
|
|
|
|
|