I have a printer connected through LAN to the same network that of an android tab. The printing should be a specific format with images and tables. I don't want any dialogs shown during the printing process. The printing process should be automatic.
try {
Socket sock = new Socket("192.168.10.22", 9100);
PrintWriter oStream = new PrintWriter(sock.getOutputStream());
oStream.println("HI,test from Android Device");
oStream.println("\n\n\n");
oStream.println("Printing works!!!");
oStream.println("\n\n\n");
oStream.close();
} catch (Exception e) {
e.printStackTrace();
}
I tried the above code but I can't change the font size or print images
I am trying to connect to dialogic xms server using autobahn WebSockets:
As read I need to add a protocol rtcweb
This is my code :
String uri = "ws://myUrl:port";
WebSocketConnection ws = new WebSocketConnection();;
wsObserver = new WebSocketObserver();
String[] protocols = {"rtcweb"};
try {
ws.connect(new URI(uri), protocols, wsObserver, new WebSocketOptions() );
} catch (WebSocketException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
But when i add the protocol rtcweb I get this onClose; and the connection still didn't open :
Code: CONNECTION_LOST. Reason: WebSockets connection lost
I've been trying to implement a simple socket communication between two Android emulators but just can't seem to get it.
My server:
public void run() {
if (SERVERIP != null) {
try {
serverStatus.setText("My IP: " + SERVERIP);
serverSocket = new ServerSocket(6798);
serverStatus.setText("ServerSocket Created");
}
catch(Exception e) {
e.printStackTrace();
}
try {
while (true) {
serverStatus.setText("waiting for client");
Socket client = serverSocket.accept();
serverStatus.setText("Connected.");
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line = in.readLine();
serverStatus.setText(line);
in.close();
client.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
else
serverStatus.setText("Couldn't detect internet connection.");
}
My Client:
try {
InetAddress ina = InetAddress.getByName("10.0.2.2");
socket = new Socket(ina, 6789);
}
catch (Exception e) {
e.printStackTrace();
}
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
out.println("Hey Server!");
dispText.setText("sent");
}
catch (Exception e) {
e.printStackTrace();
}
The Client side goes on to display the message "sent" however the Server does not move beyond "waiting for client" (stuck on the blocking accept()).
I've used telnet on my Windows machine to redirect port 6789 to 6798 on the server emulator's console. I've also tried turning off my firewall and the other suggestions posted on the similar questions asked here. Please help as just can't seem to get it and feel like I'm making a very stupid mistake.
Also, can anyone please tell me how it is possible for the Client to move beyond the Socket creation code line if the Server is still stuck on accept(). Or, does it not matter to the client that the Server isn't responding as long as it is listening on the port??
Android emulators are placed behind a virtual firewall/router by design, and cannot see each other, even when they are on the same network. The "Using Network Redirection", as well as "Interconnecting Emulator Instances" part of Google's doc on the emulator explains how to communicate with an emulator instance.
As for your last question. Use the empty constructor for socket, and then use the connect call with a specified timeout.
I want to print file using wifi printing in my android application.I have scanned wifi printer and i am having wifi printer IP address . Now how can i transfer file from android phone to printer to print it ? I am using following code to transfer file to printer `public void uploadFile(File fileName){
FTPClient client = new FTPClient();
try {
client.connect(FTP_HOST,21);
client.login(FTP_USER, FTP_PASS);
client.setType(FTPClient.TYPE_BINARY);
client.changeDirectory("/");
client.upload(fileName, new MyTransferListener());
} catch (Exception e) {
e.printStackTrace();
try {
client.disconnect(true);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}`
Can anybody help me ? Thanks in advance.
Using Socket you can print from android using network printer in WIFI Connection
Socket objSocket = new Socket();
String sIP = "192.168.1.10"
String sPort = "9100"
InetSocketAddress objEndPoint = new InetSocketAddress(sIP, Integer.parseInt(sPort));
DataOutputStream objOutputStream;
objSocket.connect(objEndPoint, 3000);
objOutputStream = new DataOutputStream(objSocket.getOutputStream());
objOutputStream.write(("Test Print").getBytes());
objOutputStream.close();
objSocket.close();
I think you can might do it without sending whole file, just pick the Strings from the file and make connection with the wifi device, send the text to the device, it will print it. I have done same thing with the bluetooth printer.
I want to code an Android app, which will connect to a network printer with a specific IP address, and then make a printing.
For printing I know that I need to write my own Postscript for specific files types, and connecting to a network is not a problem over WIFI.
How to connect to the network printer?
Any device connected to a network will communicate via their IP and Ports / sockets. The simplest way to connect via telnet or socket and write the data to their socket buffers.
try
{
Socket sock = new Socket("192.168.1.222", 9100);
PrintWriter oStream = new PrintWriter(sock.getOutputStream());
oStream.println("HI,test from Android Device");
oStream.println("\n\n\n");
oStream.close();
sock.close();
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
You might be able to use lpdspooler, that is, if the printer supports LPR/LPD. If you can give some more details about the environment (printer, etc), I might be able to give more information.
Just Add This Code After oncreate Method
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
Star has an Android SDK which has port discovery. It'll find any of their wifi receipt printers on your network. http://starmicronics.com/support/SDKDocumentation.aspx
Try to use PrintManager: https://developer.android.com/training/printing/custom-docs
private void doPrint() {
// Get a PrintManager instance
PrintManager printManager = (PrintManager) getActivity()
.getSystemService(Context.PRINT_SERVICE);
// Set job name, which will be displayed in the print queue
String jobName = getActivity().getString(R.string.app_name) + " Document";
// Start a print job, passing in a PrintDocumentAdapter implementation
// to handle the generation of a print document
printManager.print(jobName, new MyPrintDocumentAdapter(getActivity()),
null); //
}
My solution. I used Epson TM series. I think the port is 9100 for default.
In Manifest add:
<uses-permission android:name="android.permission.INTERNET"/>
in the activity use a Thread otherwise u you can the android.os.NetworkOnMainThreadException error.
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
try {
Socket sock = new Socket("192.168.1.168", 9100);
PrintWriter oStream = new PrintWriter(sock.getOutputStream());
oStream.println("Hi, test from Android Device");
oStream.println("\n");
oStream.close();
sock.close();
} catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}});
If not enough, add in manifest these:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Well, you cant connect any devices directly as you will need the driver installed. there are 3rd party apps like Google Cloud print that works seamlessly with Android though.