Below is a part of the process and the part of the code of the project:
1. The Android client:
Public class MainActivity extends AppCompatActivity {
Private EditText mEtIp mEtData;
private TextView tv;
Private OutputStream mOutputStream=null;
Private InputStream mInputStream=null;
Private Socket mSocket=null;
Private IP String;
Private String data.
Private Boolean socketStatus=false;
@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MEtIp=the findViewById (R.i d.e t_ip);
MEtData=https://bbs.csdn.net/topics/findViewById (R.i d.e t_data);
}
Public void connect (View View) {
IP=mEtIp. GetText (). The toString ();
Toast. MakeText (MainActivity. This, "" + IP, Toast. LENGTH_SHORT), show ();
Thread the Thread=new Thread () {
@ Override
Public void the run () {
Super. The run ();
if (! SocketStatus) {
Try {
//to connect server
MSocket=new Socket (IP, 2001);
If (mSocket!=null) {
SocketStatus=true;
}
MOutputStream=mSocket. GetOutputStream ();
MInputStream=mSocket. GetInputStream ();
} the catch (IOException e) {
e.printStackTrace();
}
}
}
};
Thread. The start ();
}
Public void the send (View View) {
Data=(https://bbs.csdn.net/topics/mEtData.getText). The toString ();
Toast. MakeText (MainActivity. This, "" + data, Toast. LENGTH_SHORT), show ();
If (data!=null) {
Data=https://bbs.csdn.net/topics/data + '\ 0';
}
Thread the Thread=new Thread () {
@ Override
Public void the run () {
Super. The run ();
If (socketStatus) {
Try {
MOutputStream. Write (data. GetBytes ());
//the problem here, I want to pass the data inside the input stream for a serial port, but can't get, and the Log is printed in Chinese garbled, but I am not Chinese in the serial
Byte [] buffer=new byte [mInputStream. Available ()];
MInputStream. Read (buffer);
String s1=new String (buffer);
The Log i. (" context ", s1);
} the catch (IOException e) {
e.printStackTrace();
}
}
}
};
Thread. The start ();
}
/* when the client interface returns, shut down the corresponding socket resources */
@ Override
Public void onBackPressed () {
Super. OnBackPressed ();
/* * close corresponding resources/
Try {
MOutputStream. Close ();
MSocket. Close ();
} the catch (IOException e) {
e.printStackTrace();
}
}
Public static String readStream (InputStream in) {
Try {
ByteArrayOutputStream baos=new ByteArrayOutputStream ();
byte[] buffer=new byte[1024];
Int len=1;
While ((len=in. Read (buffer))!=1) {
Baos. Write (buffer, 0, len);
}
The String content=baos. ToString ();
in.close();
Baos. Close ();
Return the content;
} the catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
}
This is the android side interface, enter the IP address to connect wifi, sent via send button again to obtain the data
This is a serial port debug software, after I sent the android data, Arduino board will I hope to get the data to print on the serial port, now I just want to in the mobile terminal access to these data,
The great god excuse me, is there a way to let me get on the Android end these data in the serial debugging software?? Thank you for the
CodePudding user response:
How could the JAVA realization of serial port to read and write?Through the C/C + + is needed to support, that is, through the JNI bridge, there is a GOOGLE open source, you consult:
https://code.google.com/p/android-serialport-api/
CodePudding user response:
See,,,,,