Android UDP and androidudp
Not much nonsense. Code directly!
I. Acceptor
1. Create a UDP connection
Public void init (){
Try {
// Function of the switch
IsRunning = true;
DatagramSocket mUdpSocket = new DatagramSocket (40000 );
RecivRequest ();
} Catch (Exception e ){
LogUtil. e (Constant. LOG_TAG, "Exception =" + e );
}
}
2. Obtain UDP data packets
Public void recivRequest (){
// This is a thread pool for processing time-consuming operations
ThreadPoolManager. getInstance (). addTask (new Runnable (){
@ Override
Public void run (){
While (isRunning ){
Try {
Byte [] recvBuffer = new byte [1024];
DatagramPacket dataPacket = new DatagramPacket (recvBuffer, recvBuffer. length );
MUdpSocket. receive (dataPacket );
ParsePackage (dataPacket );
} Catch (IOException e ){
If (null! = MUdpSocket &&! MUdpSocket. isClosed ()){
MUdpSocket. close ();
}
MUdpSocket = null;
E. printStackTrace ();
}
}
}
});
}
3. Start preparing to parse UDP Packets
Private void parsePackage (final DatagramPacket dataPacket ){
ThreadPoolManager. getInstance (). addTask (new Runnable (){
@ Override
Public void run (){
String [] datagramPacketSegment = getDatagramPacketSegment (dataPacket );
/* Obtain data based on your business needs */
}
});
}
4. Method for parsing data packets
Private String [] getDatagramPacketSegment (DatagramPacket dataPacket ){
If (null = dataPacket ){
Return null;
}
Int length = 0;
Byte [] bytes = dataPacket. getData ();
For (int I = 0; I <bytes. length; I ++ ){
If (bytes [I] = 0 ){
Length = I;
Break;
}
Length = I + 1;
}
String recivContent = new String (bytes, 0, length );
// This # refers to the delimiter discussed with the sender for distinguishing multiple messages.
String [] reciveString = recivContent. split ("#");
Return reciveString;
}
Ii. Sending end
This method receives two parameters, one is the Client IP address and the other is the content you want to send. If there are multiple content, use the # difference
Public void sendInteractiveRequest (String ipAddress, String contents ){
LogUtil. d (Constant. LOG_TAG, "sendInteractiveRequest ---> sendContents =" + contents );
Byte [] data = contents. getBytes ();
Byte [] recvBuffer = new byte [1024];
DatagramPacket dataPacket = new DatagramPacket (recvBuffer, recvBuffer. length );
DataPacket. setData (data );
DataPacket. setLength (data. length );
// Set the port number of the target host for this data bag
DataPacket. setPort (40000 );
InetAddress sendAddr = null;
Try {
// Return the host address based on the given host string name
SendAddr = InetAddress. getByName (ipAddress );
// Set the address of the receiver host
DataPacket. setAddress (sendAddr );
// Send data
MUdpSocket. send (dataPacket );
} Catch (Exception e ){
LogUtil. e (Constant. LOG_TAG, "Exception =" + e );
}
}
For example, the parameter is the key value of the remote control you want to send.
Private void sendKeycodeValue (final int keyCode ){
CommonUtil. setControllerVibrate (LVBXApp. getApp ());
String repeated redname = UserMgr. getUserName ();
// Send a simulated Operation Command
ThreadPoolManager. getInstance (). addTask (new Runnable (){
@ Override
Public void run (){
String mainCmd = remote1_type. PAD_CMD + "";
// 101 #15754 #99065918 #10 #78: F7: BE: 52: 61: 2A (partition type # accountName # interactivePasswd # macAddress # keyCodeValue)
String contents = mainCmd + "#" + UserMgr. getUserName () + "#" + UserMgr. getInteracPassword () + "#"
+ CommonUtil. getLocalMacAddress (LVBXApp. getApp () + "#" + keyCode;
String ipAdress = UserMgr. getPairedIp ();
InteractiveMgr. getInstance (). sendInteractiveRequest (ipAdress, contents );
}
});
}
OK. UDP Communication resolution till now, you can combine my previous file (simulate sending Remote Control commands) to remotely control the TV end by sending instructions on the mobile phone end!