Android Talking about Connecting Printers with WiFi in the Same LAN
Recent work is really busy, so-called busy leisure, in order to share with you how to use WiFi to connect printers under the same LAN.
When I first received such a task, my mind was not so messy. I still had a clear idea. Maybe it was because I had done WiFi before.
Take the printer to arrange some relevant information, then implement various codes, turn off WiFi, switch, get the list and so on a series of functions.
It was realized once, but the hematemesis happened. The original product requirement was not so complicated, but a very simple requirement, input directly.
ip address and port number, then you can connect successfully. We are warned that it is better to understand the product requirements before developing it.
In order to avoid doing useless work, the next step is to enter the code implementation link.
1. Using WiFi to connect the printer in the same LAN can print out the text. Can you imagine how to achieve it?
Socket communication, yes, I believe that this is the best communication tool, then what is socket, what are its benefits? Here we briefly share the communication mechanism principle of socket, the so-called socket is also commonly known as "socket", used to describe IP address and port, is a handle of the communication chain. Applications usually send or respond to network requests through sockets.
Taking J2SDK-1.3 as an example, the Socket and ServerSocket class libraries are located in the java.net package. Server Socket is used on the server side, and Socket is used when establishing network connection. When the connection is successful, both ends of the application will generate a Socket instance, which will be manipulated to complete the required session. For a network connection, sockets are equal, and there is no difference, because there are different levels on the server side or on the client side. Both Sockets and ServerSockets work through the SocketImpl class and its subclasses.
Here's a very simple demo of communication between the server and the front end
// This is the server-side code.
new Thread(new Runnable() {
@Override
public void run() {
try {
//The port number 9999 here should be the same as the port number of the client.
ServerSocket ss = new ServerSocket(9999);
Log.d(TAG, "Start the server....");
Socket s = ss.accept();
Log.d(TAG, "Client:" +s.getInetAddress().getLocalHost() + "Connected to the server");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(s.getInputStream()));
//Read the message sent by the client
String mess = bufferedReader.readLine();
Log.d(TAG, "Client:" + mess);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bw.write(mess + "\n");
bw.flush();
} catch (IOException e) {
Log.d(TAG, "service IOException: " + e);
}
}
}).start();
// This is the client's code implementation
new Thread(new Runnable() {
@Override
public void run() {
try {
Looper.prepare();
//Enter the ip address and port number directly here.
Socket socket = new Socket("127.0.0.1", 9999);
//The connection is currently successful and active
if (socket.isConnected() && socket.getKeepAlive()) {
//Get the input stream from the server, which can be used or not, depending on the product.
InputStream inputStream = socket.getInputStream();
//Obtaining the output stream of the server must be fetched, because this is related to the operation of sending messages to the server.
OutputStream os = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
//Send a message to the server. In the printer, the action of n is necessary. Without the action of n, the printer will not print anything.
bw.write("Test client and server communication, the server receives the message and returns it to the client\n");
bw.flush();
//Read the message returned by the server
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String mess = br.readLine();
Log.d(TAG, "The server:" + mess);
} else {
Log.d(TAG, "client connection failed");
}
} catch (IOException e) {
Log.d(TAG, "client IOException: " + e);
}
}
}).start();
Okay, that's a simple demo that serves the client and interacts with the client. A careful child's shoe will find that I have something in the client's code that simply talks about printing operations.
Actually, it's a very simple thing to interact with printer. It doesn't need to be so complicated. We just need to treat printer as a server and APP as a client. Then we can realize the principle of interaction with printer.
1. First create a Socket
Socket socket =new Socket("127.0.0.1, 9999);, just need to implement this line of code, because it will automatically help you do the connection action,
2 and then determine if a communication has been established
if(socket.isConnected() && socket.getKeepAlive()){ }
When the connection is successful, you need to get the output stream OutputStream output Stream = socket. getOutputStream ();
4 Print out the text you want to print directly using the output stream.
try{
byte[] data = text.getBytes("GB2312");
outputStream.write(data,0, data.length);
outputStream.flush();
}catch(IOException e) {
Log.e("printText "+ e);
}
By the way, you need to execute a line of code before you can operate the printer, that is, reset the printer.
private static final byte[] RESET= {0x1b,0x40};