修改udp线程相关实现

chiyanzhen_branch
CYZ 2 years ago
parent 0f44c4ccb5
commit 30b6dbde0c

@ -1,133 +0,0 @@
` java:S112h"FDefine and throw a dedicated exception instead of using a generic one.(Ò‚Òéüÿÿÿÿ
a java:S112"FDefine and throw a dedicated exception instead of using a generic one.(Ò‚Òéüÿÿÿÿ
a java:S112"FDefine and throw a dedicated exception instead of using a generic one.(Ò‚Òéüÿÿÿÿ
a java:S112"FDefine and throw a dedicated exception instead of using a generic one.(Ò‚Òéüÿÿÿÿ
a java:S112¡"FDefine and throw a dedicated exception instead of using a generic one.(Ò‚Òéüÿÿÿÿ
a java:S112¬"FDefine and throw a dedicated exception instead of using a generic one.(Ò‚Òéüÿÿÿÿ
a java:S112·"FDefine and throw a dedicated exception instead of using a generic one.(Ò‚Òéüÿÿÿÿ
h java:S112Â"FDefine and throw a dedicated exception instead of using a generic one.(Ò‚Òéüÿÿÿÿ8á̇á<E280A1>1
h java:S112Í"FDefine and throw a dedicated exception instead of using a generic one.(Ò‚Òéüÿÿÿÿ8á̇á<E280A1>1
s java:S117©"QRename this local variable to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(¦á®¾ÿÿÿÿÿ8ý̇á<E280A1>1
>
java:S1604b"(Make this anonymous inner class a lambda(Æ·»°
>
java:S1604m"(Make this anonymous inner class a lambda(í×ó€
>
java:S1604z"(Make this anonymous inner class a lambda(êáÇñ
K
java:S1604"(Make this anonymous inner class a lambda(Ëݳ­ûÿÿÿÿ8þ̇á<E280A1>1
D
java:S1604<18>"(Make this anonymous inner class a lambda(ßó¤—ùÿÿÿÿ
D
java:S1604"(Make this anonymous inner class a lambda(‰‘¼“ÿÿÿÿÿ
D
java:S1604¦"(Make this anonymous inner class a lambda(÷³¿­úÿÿÿÿ
?
java:S1604±"(Make this anonymous inner class a lambda(Ÿ…³Á
?
java:S1604¼"(Make this anonymous inner class a lambda(ÚŠõ¨
?
java:S1604Ç"(Make this anonymous inner class a lambda(¶±»ù
F
java:S1604Ò"(Make this anonymous inner class a lambda(±ë¦þ8ÿ̇á<E280A1>1
]
java:S2184¬"@Cast one of the operands of this integer division to a "double".(—­½[8„͇á<E280A1>1
_
java:S2184­"ACast one of the operands of this division operation to a "float".(ÒÏ“×8„͇á<E280A1>1
d
java:S2184³"ACast one of the operands of this division operation to a "float".(‹öÚÈùÿÿÿÿ8…͇á<E280A1>1
b
java:S2184´"DCast one of the operands of this subtraction operation to a "float".(¸õÀ½8…͇á<E280A1>1
d
java:S2184À"ACast one of the operands of this division operation to a "float".(‹öÚÈùÿÿÿÿ8†Í‡á<E280A1>1
b
java:S2184Á"DCast one of the operands of this subtraction operation to a "float".(¸õÀ½8†Í‡á<E280A1>1
b
java:S2184Â"DCast one of the operands of this subtraction operation to a "float".(¬Óé<C393>8‡Í‡á<E280A1>1
d
java:S2184Ñ"ACast one of the operands of this addition operation to a "float".(•‹öƒýÿÿÿÿ8‡Í‡á<E280A1>1
g
java:S2184Ñ"DCast one of the operands of this subtraction operation to a "float".(•‹öƒýÿÿÿÿ8ˆÍ‡á<E280A1>1
d
java:S2184Ò"ACast one of the operands of this division operation to a "float".(ÝÒŒÑþÿÿÿÿ8ˆÍ‡á<E280A1>1
c
java:S2184è"@Cast one of the operands of this integer division to a "double".(ÂÀ‰öùÿÿÿÿ8‰Í‡á<E280A1>1
j
java:S2095ž"LUse try-with-resources or close this "DatagramSocket" in a "finally" clause.(©Áá‚8Ð͇á<E280A1>1
o java:S115/"ZRename this constant name to match the regular expression '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.(ƴư
t java:S1150"ZRename this constant name to match the regular expression '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.(¨Öܶüÿÿÿÿ
k java:S1163"WRename this field "TimeInterval" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(öÿãn
t java:S116<"ZRename this field "Trans_to_Detect" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(¾¥Âöûÿÿÿÿ
k java:S116?"VRename this field "Forward_btn" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(¸ÂÑ•
m java:S116@"SRename this field "Back_btn" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(ÛÇãôþÿÿÿÿ
h java:S116A"SRename this field "Left_btn" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(¥÷œë
i java:S116B"TRename this field "Right_btn" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(<28>Οƒ
m java:S116C"SRename this field "Down_btn" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(ß缎øÿÿÿÿ
k java:S116D"QRename this field "Up_btn" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(ŒÑ„›ùÿÿÿÿ
q java:S116E"WRename this field "TurnLeft_btn" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(ëÝŽ–þÿÿÿÿ
l java:S116F"XRename this field "TurnRight_btn" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(€‡p
k java:S116G"VRename this field "Setting_btn" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(à°úö
p java:S116H"VRename this field "TakeOff_btn" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(€‚òœüÿÿÿÿ
p java:S116I"VRename this field "Landing_btn" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(µÀç‹úÿÿÿÿ
u
java:S1450I"ZRemove the "Landing_btn" field and declare it as a local variable in the relevant methods.(µÀç‹úÿÿÿÿ
u
java:S1450H"ZRemove the "TakeOff_btn" field and declare it as a local variable in the relevant methods.(€‚òœüÿÿÿÿ
o
java:S1450J"TRemove the "angle" field and declare it as a local variable in the relevant methods.(§úþóþÿÿÿÿ
r
java:S1450K"WRemove the "distance" field and declare it as a local variable in the relevant methods.(×¾¶Ðøÿÿÿÿ
[
java:S1124/"EReorder the modifiers to comply with the Java Language Specification.(ƴư
`
java:S11240"EReorder the modifiers to comply with the Java Language Specification.(¨Öܶüÿÿÿÿ
[
java:S11241"EReorder the modifiers to comply with the Java Language Specification.(ß×á
[
java:S11242"EReorder the modifiers to comply with the Java Language Specification.(Ó¾þá
o java:S100y"NRename this method name to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(ü™âîùÿÿÿÿ8þ͇á<E280A1>1
t
java:S3776y"RRefactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.(ü™âîùÿÿÿÿ8ÿ͇á<E280A1>1
K
java:S1185ä"(Remove this method to simply inherit it.(ñ‰ÿÜýÿÿÿÿ8ŽÎ‡á<E280A1>1
S
java:S2696ô"6Make the enclosing method "static" or remove this set.(èª8“·á<E280A1>1
^ java:S125õ"<This block of commented-out lines of code should be removed.(<28>­éäøÿÿÿÿ8“·á<E280A1>1
^ java:S125ú"<This block of commented-out lines of code should be removed.(çéŸþÿÿÿÿ8·á<E280A1>1
Y
java:S2696<18>"6Make the enclosing method "static" or remove this set.(Ôø±üûÿÿÿÿ8—·á<E280A1>1
M
java:S2093ñ"*Change this "try" to a try-with-resources.(ç£ê<C2A3>úÿÿÿÿ8˜Î‡á<E280A1>1
p java:S116Ð"SRename this field "BG_COLOR" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(þ¨ò‘8œÎ‡á<E280A1>1
r
java:S1450Þ"TRemove the "mPath" field and declare it as a local variable in the relevant methods.(ä“<C3A4>Â8 Î‡á<E280A1>1
Y java:S125"<This block of commented-out lines of code should be removed.(Ê蘋8¡Î‡á<E280A1>1
c
java:S1610é"@Convert the abstract class "OnUnlockListener" into an interface.(׋›Îøÿÿÿÿ8¢Î‡á<E280A1>1
D java:S131""Add a default case to this switch.(<28>äÃùÿÿÿÿÿ8©Î‡á<E280A1>1
D java:S131Ü""Add a default case to this switch.(ãÁð™øÿÿÿÿ8²Î‡á<E280A1>1
9
java:S1128,"Remove this duplicated import.(°þÊóøÿÿÿÿ
C
java:S10681"-Remove this unused "SEND_PORT" private field.(ß×á
B
java:S10685"'Remove this unused "buf" private field.(ÅØè<C398>þÿÿÿÿ
D
java:S10688".Remove this unused "sendSocket" private field.(éêÖé
J
java:S1068:"/Remove this unused "sendHandler" private field.(ã¹Ð«þÿÿÿÿ
N
java:S1068<"3Remove this unused "Trans_to_Detect" private field.(¾¥Âöûÿÿÿÿ
B
java:S1068>"'Remove this unused "btn" private field.(ãȧ<C388>üÿÿÿÿ
D
java:S1068J")Remove this unused "angle" private field.(§úþóþÿÿÿÿ
G
java:S1068K",Remove this unused "distance" private field.(×¾¶Ðøÿÿÿÿ
J
java:S1068M"/Remove this unused "mThreadPool" private field.(ÄÐâµúÿÿÿÿ
G
java:S1068Þ")Remove this unused "mPath" private field.(ä“<C3A4>Â8µÎ‡á<E280A1>1
M
java:S1068ä"/Remove this unused "mIsUnlocked" private field.(ˆø•ö8µÎ‡á<E280A1>1

@ -1,84 +0,0 @@
l java:S117Û"QRename this local variable to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(ÓÔÒÞùÿÿÿÿ
p
java:S1104D"[Make mLocationClient a static final constant or non-public and provide accessors if needed.(òó•L
q
java:S1104F"[Make mLocationOption a static final constant or non-public and provide accessors if needed.(ÈÀ‚§
d
java:S2259<18>"MA "NullPointerException" could be thrown; "mLocationClient" is nullable here.(Ô¹¤<C2B9>
c
java:S2095ó"LUse try-with-resources or close this "DatagramSocket" in a "finally" clause.(ù<>Èö
o java:S115d"ZRename this constant name to match the regular expression '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.(ë¿§Ï
s java:S116I"YRename this field "Current_latlng" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(æàÁÆÿÿÿÿÿ
o java:S116["ZRename this field "ClearMarker_btn" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(ëþ¥È
r java:S116]"XRename this field "ChangeAct_btn" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(ÂîœÃúÿÿÿÿ
d java:S116i"PRename this field "Angle" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(ÒáÒD
q java:S116j"WRename this field "MonitorAngle" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(þ‰Ô€ûÿÿÿÿ
o java:S116l"ZRename this field "MonitorDistance" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(ƒæ¬Ê
˜
java:S1186ã"€Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation.(±€¿ë
i
java:S1450i"TRemove the "Angle" field and declare it as a local variable in the relevant methods.(ÒáÒD
w
java:S1450]"\Remove the "ChangeAct_btn" field and declare it as a local variable in the relevant methods.(ÂîœÃúÿÿÿÿ
i
java:S1450H"SRemove the "city" field and declare it as a local variable in the relevant methods.(“øÌà
m
java:S1450W"WRemove the "cityCode" field and declare it as a local variable in the relevant methods.(êýð†
r
java:S1450k"WRemove the "distance" field and declare it as a local variable in the relevant methods.(×¾¶Ðøÿÿÿÿ
G
java:S1874S"1Remove this use of "PoiSearch"; it is deprecated.(–ƒëÖ
L
java:S1874U"1Remove this use of "PoiSearch"; it is deprecated.(²¹·‘ÿÿÿÿÿ
[
java:S1124d"EReorder the modifiers to comply with the Java Language Specification.(ë¿§Ï
`
java:S1124e"EReorder the modifiers to comply with the Java Language Specification.(<28>”µÆüÿÿÿÿ
Q java:S125h"<This block of commented-out lines of code should be removed.(‡µÍ”
V java:S125x"<This block of commented-out lines of code should be removed.(¤¯¿Îúÿÿÿÿ
f
java:S1874·"JDon't override a deprecated method or explicitly mark it as "@Deprecated".(Ó»×¶þÿÿÿÿ
R java:S125í"<This block of commented-out lines of code should be removed.(©áèè
Y
java:S1854 "=Remove this useless assignment to local variable "longitude".(ËàÅ€ûÿÿÿÿ
X
java:S1854ž"<Remove this useless assignment to local variable "latitude".(û‘áäùÿÿÿÿ
I
java:S1481ž"-Remove this unused "latitude" local variable.(û‘áäùÿÿÿÿ
J
java:S1481 ".Remove this unused "longitude" local variable.(ËàÅ€ûÿÿÿÿ
R java:S125ß"<This block of commented-out lines of code should be removed.(É–Ö­
G
java:S1172"+Remove this unused method parameter "view".(çÑ¢‰ýÿÿÿÿ
U
java:S1155">Use isEmpty() to check whether the collection is empty or not.(”ó±’
G
java:S1172<18>"+Remove this unused method parameter "view".(•É‘Îûÿÿÿÿ
Q java:S125 "<This block of commented-out lines of code should be removed.(Ÿ…¦Q
U
java:S1117Ï"9Rename "query" which hides the field declared at line 83.(˜Ž“áûÿÿÿÿ
\
java:S2147Š"ECombine this catch with the one at line 520, which has the same body.(<28>”ý¾
n
java:S3776š"RRefactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.(—´¸…øÿÿÿÿ
O java:S106·"9Replace this use of System.out or System.err by a logger.(éÃÇâ
W java:S125¹"<This block of commented-out lines of code should be removed.(×ûÂÚÿÿÿÿÿ
>
java:S1068H"(Remove this unused "city" private field.(“øÌà
?
java:S1068S")Remove this unused "query" private field.(–ƒëÖ
H
java:S1068U"-Remove this unused "poiSearch" private field.(²¹·‘ÿÿÿÿÿ
B
java:S1068W",Remove this unused "cityCode" private field.(êýð†
I
java:S1068c".Remove this unused "reveSocket" private field.(éÛþ§ýÿÿÿÿ
K
java:S1068e"0Remove this unused "RECEIVE_PORT" private field.(<28>”µÆüÿÿÿÿ
K
java:S1068f"0Remove this unused "listenStatus" private field.(ì㋸ûÿÿÿÿ
I
java:S1068g".Remove this unused "serverAddr" private field.(<28>§±ùÿÿÿÿ
E
java:S1068ë".Remove this unused "mReceiveIp" private field.(úúÛò

@ -1,2 +0,0 @@
R xml:S55943"1Implement permissions on this exported component.(ˆ©…»ùÿÿÿÿ8…îö“<C3B6>1

@ -1,10 +0,0 @@
k java:S117'"QRename this local variable to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(ß<>±÷üÿÿÿÿ
q
java:S1450"[Remove the "mLoginButton" field and declare it as a local variable in the relevant methods.(žÉÕ<C389>
t
java:S1171"YMove the contents of this initializer to a standard constructor or to field initializers.(ÒÌâ–øÿÿÿÿ
G
java:S1602 ",Remove useless curly braces around statement(òã‘õúÿÿÿÿ
V
java:S11263"AReplace this if-then-else statement by a single return statement.(Ö<>¼z

@ -22,14 +22,4 @@ j
j
:app/src/main/java/com/example/sixaunyi/DetectActivity.java,9\6\9668392b13e33166876f2a7d3f10eb5405e757ad
k
;app/src/main/java/com/example/sixaunyi/ControlActivity.java,c\9\c99a584c2117ed2df42496451bb97ac1147f5ea0
P
app/src/main/AndroidManifest.xml,8\c\8c55c3ccc257e5907959013f99656e4c8ec3903e
k
;app/src/main/java/com/example/sixaunyi/SettingActivity.java,2\a\2ab0711d9aa5839cecbb6cf0ed96f5a3f6470de5
i
9app/src/main/java/com/example/sixaunyi/LoginActivity.java,f\7\f7730cde41931660f06cfc32899edf44acf4d104
h
8app/src/main/java/com/example/sixaunyi/MainActivity.java,7\c\7c120b7216f76ce98d1573d90803ab36f481640c
i
9app/src/main/java/com/example/sixaunyi/VideoActivity.java,7\1\71fd1993763df2214bbdad8cfee650733993fe44
;app/src/main/java/com/example/sixaunyi/ControlActivity.java,c\9\c99a584c2117ed2df42496451bb97ac1147f5ea0

@ -5,7 +5,7 @@ import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class SettingActivity extends AppCompatActivity {
private boolean if_jiankong;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

@ -20,6 +20,7 @@ import android.graphics.RectF;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
@ -48,7 +49,6 @@ public class VideoActivity extends AppCompatActivity {
private final static String Recieve_IP = "192.168.39.47";//填服务器IP与send_to_IP为同一个
private final static int SEND_PORT = 8888; //发送端口号
private final static int RECEIVE_PORT = 9999; //接收端口号
int TimeInterval = 5;
private static boolean listenStatus = true; //接收线程的循环标识
private byte[] buf;
static Bitmap bp;
@ -71,8 +71,8 @@ public class VideoActivity extends AppCompatActivity {
private ImageButton Setting_btn;
private ImageButton TakeOff_btn;
private ImageButton Landing_btn;
private String angle;
private float distance;
private String mangle;
private float mdistance;
// 创建一个可缓存的线程池
private ExecutorService mThreadPool = Executors.newCachedThreadPool();
//ReceiveData()显示到imageView
@ -99,31 +99,40 @@ public class VideoActivity extends AppCompatActivity {
@Override
public void onClick(View v) {
try {
sendOrder("TAKEOFF");
sendCommand("TAKEOFF");
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
Log.i(TAG, "onClick: BBBBBBBBackward");
Log.i(TAG, "onClick: TTTTTTTTTTTTTTakeoff");
}
});
Landing_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new UdpReceiveThread().start();
ReceiveHandler receiveHandler = new ReceiveHandler();
UdpReceiveThread StreamThread = new UdpReceiveThread(Recieve_IP,RECEIVE_PORT,receiveHandler);
try {
StreamThread.startListen();
StreamThread.start();
} catch (SocketException | UnknownHostException e) {
e.printStackTrace();
}
}
});
Setting_btn = findViewById(R.id.setting_button);
Button_Init();
Intent i = getIntent();
distance = i.getFloatExtra("Distance",0);
angle = i.getStringExtra("Angle");
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) {
try {
sendOrder("FORWARD");
sendCommand("FORWARD");
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
@ -134,7 +143,7 @@ public class VideoActivity extends AppCompatActivity {
@Override
public void onClick(View v) {
try {
sendOrder("BACKWORD");
sendCommand("BACKWORD");
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
@ -145,7 +154,7 @@ public class VideoActivity extends AppCompatActivity {
@Override
public void onClick(View v) {
try {
sendOrder("LEFT");
sendCommand("LEFT");
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
@ -156,7 +165,7 @@ public class VideoActivity extends AppCompatActivity {
@Override
public void onClick(View v) {
try {
sendOrder("RIGHT");
sendCommand("RIGHT");
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
@ -167,7 +176,7 @@ public class VideoActivity extends AppCompatActivity {
@Override
public void onClick(View v) {
try {
sendOrder("DOWN");
sendCommand("DOWN");
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
@ -178,7 +187,7 @@ public class VideoActivity extends AppCompatActivity {
@Override
public void onClick(View v) {
try {
sendOrder("UP");
sendCommand("UP");
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
@ -189,7 +198,7 @@ public class VideoActivity extends AppCompatActivity {
@Override
public void onClick(View v) {
try {
sendOrder("TURN_LEFT");
sendCommand("TURN_LEFT");
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
@ -200,7 +209,7 @@ public class VideoActivity extends AppCompatActivity {
@Override
public void onClick(View v) {
try {
sendOrder("TURN_RIGHT");
sendCommand("TURN_RIGHT");
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
@ -218,8 +227,14 @@ public class VideoActivity extends AppCompatActivity {
class ReceiveHandler extends Handler{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
imgShow.setImageBitmap(bp);
if (msg.what == 1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
imgShow.setImageBitmap(bp);
}
});
}
}
}
@ -233,38 +248,91 @@ public class VideoActivity extends AppCompatActivity {
/*
* UDP线
* */
public class UdpReceiveThread extends Thread
{
public class UdpReceiveThread extends Thread {
private DatagramSocket reveSocket;
private String Recieve_IP;
private int RECEIVE_PORT;
private Handler receiveHandler;
public UdpReceiveThread(String ip, int port, Handler handler) {
this.Recieve_IP = ip;
this.RECEIVE_PORT = port;
this.receiveHandler = handler;
}
public void startListen() throws SocketException, UnknownHostException {
reveSocket = new DatagramSocket(RECEIVE_PORT);
}
public void stopListen() {
if (reveSocket != null && !reveSocket.isClosed()) {
listenStatus = false;
//reveSocket.close();
}
}
@Override
public void run()
{
try
{
reveSocket = new DatagramSocket(RECEIVE_PORT);
serverAddr = InetAddress.getByName(Recieve_IP);
//ByteArrayOutputStream out = new ByteArrayOutputStream();
while(listenStatus)
{
byte[] inBuf= new byte[1024*1024];
DatagramPacket inPacket=new DatagramPacket(inBuf,inBuf.length);
//out.write(inPacket.getData());
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(Recieve_IP);
while (listenStatus) {
byte[] inBuf = new byte[1024 * 1024];
DatagramPacket inPacket = new DatagramPacket(inBuf, inBuf.length);
reveSocket.receive(inPacket);
if(!inPacket.getAddress().equals(serverAddr)){
if (!inPacket.getAddress().equals(serverAddr)) {
throw new IOException("未知名的报文");
}
ByteArrayInputStream in = new ByteArrayInputStream(inPacket.getData());
receiveHandler.sendEmptyMessage(1);
bp = BitmapFactory.decodeStream(in);
}
} catch (Exception e)
{
} catch (IOException e) {
e.printStackTrace();
}finally {
reveSocket.close();
}
}
}
/*
* UDP线Runnable
* */
class SendRunnable implements Runnable {
byte[] mData;
InetAddress mAddress;
int mPort;
public SendRunnable(byte[] data, InetAddress address, int port) {
mData = data;
mAddress = address;
mPort = port;
}
@Override
public void run() {
try {
// 创建发送数据报文
DatagramPacket packet = new DatagramPacket(mData, mData.length, mAddress, mPort);
// 创建 DatagramSocket 对象并发送数据报文
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
// 关闭 DatagramSocket 对象
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*封装函数*/
private void sendCommand(String str) throws UnknownHostException {
byte[] sendData = str.getBytes();
InetAddress address = InetAddress.getByName(SEND_to_IP);
SendRunnable sendRunnable1 = new SendRunnable(sendData, address, SEND_PORT);
mThreadPool.execute(sendRunnable1);
}
/*
* UDP线
* */
@ -294,13 +362,13 @@ public class VideoActivity extends AppCompatActivity {
}
//初始化UDP发送线程
private static void sendOrder(String Sendstr) throws UnknownHostException {
InetAddress sendIP = InetAddress.getByName(SEND_to_IP); // 目标 IP 地址
int sendPort = 8888; // 目标端口号
byte[] sendData = Sendstr.getBytes(); // 要发送的数据
UdpSendThread sendOrderThread = new UdpSendThread(sendIP, sendPort, sendData); // 创建新的线程对象
sendOrderThread.start(); // 启动线程发送数据
}
// private static void sendCommand(String Sendstr) throws UnknownHostException {
// InetAddress sendIP = InetAddress.getByName(SEND_to_IP); // 目标 IP 地址
// int sendPort = 8888; // 目标端口号
// byte[] sendData = Sendstr.getBytes(); // 要发送的数据
// UdpSendThread sendCommandThread = new UdpSendThread(sendIP, sendPort, sendData); // 创建新的线程对象
// sendCommandThread.start(); // 启动线程发送数据
// }
@ -317,7 +385,7 @@ public class VideoActivity extends AppCompatActivity {
// public void onUnlock() {
// // 在用户滑动解锁后执行操作
//// try {
//// sendOrder("TAKEOFF"+"___"+angle+"___"+String.valueOf(distance));
//// sendCommand("TAKEOFF"+"___"+angle+"___"+String.valueOf(distance));
//// } catch (UnknownHostException e) {
//// throw new RuntimeException(e);
//// }

Loading…
Cancel
Save