Android native socket communication is currently underway. Write the socket server program in native. According to the client requirements, AT command is sent through the PC serial port. native is responsible for receiving commands sent from the PC end, filtering valid commands, and sending them to the APP end through socket. After the APP receives the commands, it performs related tests, then, the test information is returned to native, which is sent to the PC-side serial port to display the test result.
Native socket server implementation principle: Create a background service, start a process, listen to serial messages.
The process is as follows:
1. Add services in init. rc
Chown system/dev/ttyGS0
Chmod 0666/dev/ttyGS0
On property: ro. baseband = "unknown"
Start
Service at/system/bin/
Socket sockettest stream 0666 radio system
User system
Group system inet
Disabled
2. native service program/system/at. c
# If 1
# Define LOG_TAG ""
# Define SOCKET_NAME "sockettest"
# Include <utils/Log. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <string. h>
# Include <sys/types. h>
# Include <netinet/in. h>
# Include <sys/socket. h>
# Include <sys/wait. h>
# Include <sys/un. h>
# Include <cutils/sockets. h>
# Include <fcntl. h> // open (), read (), write ()
# Define BUFSIZE 100
Int count = 0;
# Define LOGE (...) _ android_log_print (ANDROID_LOG_ERROR, "myserver", _ VA_ARGS __)
Int main (){
# If 1
Int connect_number = 4;
Int fdListen =-1, new_fd =-1;
Int fd, ret;
Char ttyBuf [BUFSIZE];
Char * msg = ""; // "ttyGS0 test ";
Char * end = "\ r \ n ";
Struct sockaddr peeraddr;
Socklen_t socklen = sizeof (peeraddr );
Int numbytes;
Char buff [256];
Fd_set fds;
Int max = 0;
While (1 ){
Fd = open ("/dev/ttyGS0", O_RDWR); // enable the usb serial device
// Printf ("envi serial test program \ n ");
LOGD ("envi serial test program", "myserver ");
If (fd <0)
{
// Printf ("\ n ");
LOGD ("Error: open/dev/ttyGS0 error! "," Myserver ");
Return 1;
}
FdListen = android_get_control_socket (SOCKET_NAME); // create a local connection to the native socket server
If (fdListen <0 ){
LOGD ("Failed to get socket", "myserver ");
Exit (-1 );
}
LOGD ("get", "myserver ");
Ret = listen (fdListen, connect_number); // listen
If (ret <0 ){
LOGD ("listen socket failed", "myserver ");
// Perror ("listen ");
// Exit (-1 );
}
LOGD ("geta", "myserver ");
// It block until client connected. accept (socket_id, & peeraddr, & socklen );
# If 1
New_fd = accept (fdListen, & peeraddr, & socklen); // connect to the client
If (new_fd <0 ){
// LOGD ("Error on accept () errno: % d", errno );
LOGD ("Error on accept ()", "myserver ");
// Perror ("accept ");
// Exit (-1 );
Continue;
}
LOGD ("getb", "myserver ");
// Send at command from ttyGS0
While (1 ){
// Read from ttyGS0
Memset (ttyBuf, 0, sizeof (ttyBuf ));
Ret = read (fd, ttyBuf, BUFSIZE );
If (ret <0)
{
// Printf ("Error: read device error! \ N ");
// Return 1;
LOGD ("Error: read device error", "myserver", errno );
}
If (ttyBuf [0]! = NULL ){
LOGD ("while count", "myserver", ttyBuf [0]);
LOGD ("myserver", "yzy is" + ttyBuf [0]);
// Strcat (ttyBuf, end); // add/r/n
If (send (new_fd, ttyBuf, strlen (ttyBuf), 0) <0 ){
LOGE ("send failed ");
}
LOGD ("get1", "myserver ");
# If 1
FD_ZERO (& fds );
FD_SET (new_fd, & fds );
If (new_fd> max)
Max = new_fd;
If (select (max + 1, & fds, NULL) <0) // t for read Operations
{
LOGE ("select () failed", "myserver ");
}
If (FD_ISSET (new_fd, & fds )){
If (numbytes = recv (new_fd, buff, sizeof (buff), 0) =-1)
{
LOGE ("recv failed", "myserver ");
// Perror ("recv ");
// Exit (-1 );
}
}
# Endif
LOGD ("get2", "myserver ");
If (numbytes> 0 ){
Ret = write (fd, buff, strlen (buff ));
If (ret <0)
{
LOGD ("write ttyGS0 error", "myserver ");
// Return 1;
}
}
# Endif
}
Close (fd );
Close (new_fd );
Close (fdListen );
# Endif
}
Return 0;
}
# Endif
3. The APP uses localSocket to create the client and connect to the native socket server using the connect () function.
LocalSocket ls = null;
LocalSocketAddress lsa;
Ls = new LocalSocket ();
Lsa = new LocalSocketAddress (SOCKET_NAME, LocalSocketAddress. Namespace. RESERVED); // SOCKET_NAME must be the same as native and init. rc, Which is sockettest
Ls. connect (lsa );
If the socket is connected, you can send and receive messages.
4. Conclusion: The communication between native and framework is through jni. Generally, the framework calls jni. If native has a message, how can we let the framework know? The above uses socket Communication to allow the framework and native to communicate with each other.