Android Wifi Printing Example Code With Actual Printer Device
How to send data through wifi in android programmatically?
This example demonstrate most ship data through wifi in android programmatically
Need Server and Client Project
Server
Step 1
− Create a new project in Android Studio, go to File ⇒ New Projection and fill all required details to create a new project.
Footstep ii
− Add the post-obit code to res/layout/activity_main.xml.
<?xml version = "1.0" encoding = "utf-8"?> <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" android:layout_margin = "16dp" tools:context = ".MainActivity"> <TextView android:id = "@+id/tvIP" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:textAppearance = "@manner/Base.TextAppearance.AppCompat.Medium" /> <TextView android:id = "@+id/tvPort" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_below = "@+id/tvIP" android:textAppearance = "@style/Base of operations.TextAppearance.AppCompat.Medium" /> <TextView android:id = "@+id/tvConnectionStatus" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_below = "@+id/tvPort" android:textAppearance = "@mode/Base of operations.TextAppearance.AppCompat.Medium" /> <TextView android:id = "@+id/tvMessages" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_above = "@+id/etMessage" android:layout_below = "@+id/tvConnectionStatus" android:inputType = "textMultiLine" android:textAppearance = "@style/Base.TextAppearance.AppCompat.Medium" /> <EditText android:id = "@+id/etMessage" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_above = "@+id/btnSend" android:hint = "Enter Bulletin" android:inputType = "text" /> <Push android:id = "@+id/btnSend" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_alignParentBottom = "true" android:text = "Ship" /> </RelativeLayout>
Step 3
− Add the following lawmaking to src/MainActivity.java
packet com.server.myapplication.server; import android.note.SuppressLint; import android.net.wifi.WifiInfo; import android.internet.wifi.WifiManager; import android.os.Packet; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import coffee.io.PrintWriter; import java.net.InetAddress; import java.net.ServerSocket; import coffee.net.Socket; import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.nio.ByteOrder; @SuppressLint("SetTextI18n") public grade MainActivity extends AppCompatActivity { ServerSocket serverSocket; Thread Thread1 = cipher; TextView tvIP, tvPort; TextView tvMessages; EditText etMessage; Button btnSend; public static Cord SERVER_IP = ""; public static final int SERVER_PORT = 8080; String message; @Override protected void onCreate(Parcel savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvIP = findViewById(R.id.tvIP); tvPort = findViewById(R.id.tvPort); tvMessages = findViewById(R.id.tvMessages); etMessage = findViewById(R.id.etMessage); btnSend = findViewById(R.id.btnSend); try { SERVER_IP = getLocalIpAddress(); } grab (UnknownHostException due east) { eastward.printStackTrace(); } Thread1 = new Thread(new Thread1()); Thread1.offset(); btnSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { message = etMessage.getText().toString().trim(); if (!message.isEmpty()) { new Thread(new Thread3(message)).first(); } } }); } private Cord getLocalIpAddress() throws UnknownHostException { WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); affirm wifiManager ! = null; WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int ipInt = wifiInfo.getIpAddress(); return InetAddress.getByAddress(ByteBuffer.classify(4).gild(ByteOrder.LITTLE_ENDIAN).putInt(ipInt).array()).getHostAddress(); } individual PrintWriter output; individual BufferedReader input; class Thread1 implements Runnable { @Override public void run() { Socket socket; endeavor { serverSocket = new ServerSocket(SERVER_PORT); runOnUiThread(new Runnable() { @Override public void run() { tvMessages.setText("Not continued"); tvIP.setText("IP: " + SERVER_IP); tvPort.setText("Port: " + String.valueOf(SERVER_PORT)); } }); effort { socket = serverSocket.accept(); output = new PrintWriter(socket.getOutputStream()); input = new BufferedReader(new InputStreamReader(socket.getInputStream())); runOnUiThread(new Runnable() { @Override public void run() { tvMessages.setText("Connected\n"); } }); new Thread(new Thread2()).get-go(); } grab (IOException eastward) { east.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } } private course Thread2 implements Runnable { @Override public void run() { while (true) { try { final Cord bulletin = input.readLine(); if (bulletin ! = zip) { runOnUiThread(new Runnable() { @Override public void run() { tvMessages.append("client:" + message + "\n"); } }); } else { Thread1 = new Thread(new Thread1()); Thread1.start(); render; } } grab (IOException due east) { e.printStackTrace(); } } } } course Thread3 implements Runnable { private String message; Thread3(String message) { this.message = bulletin; } @Override public void run() { output.write(message); output.flush(); runOnUiThread(new Runnable() { @Override public void run() { tvMessages.suspend("server: " + bulletin + "\due north"); etMessage.setText(""); } }); } } }
Step 4
− Add the following code to androidManifest.xml
<?xml version = "one.0" encoding = "utf-8"?> <manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.example.myapplication"> <uses-permission android:name = "android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name = "android.permission.INTERNET"/> <awarding android:allowBackup = "true" android:icon = "@mipmap/ic_launcher" android:characterization = "@cord/app_name" android:roundIcon = "@mipmap/ic_launcher_round" android:supportsRtl = "true" android:theme = "@style/AppTheme"> <action android:name = ".MainActivity" android:label = "Server"> <intent-filter> <action android:name = "android.intent.action.Primary" /> <category android:name = "android.intent.category.LAUNCHER" /> </intent-filter> </activity> </awarding> </manifest>
Customer
Step ane
− Create a new projection in Android Studio, go to File ⇒ New Projection and fill all required details to create a new project.
Footstep 2
− Add the following code to res/layout/activity_main.xml.
<?xml version = "1.0" encoding = "utf-8"?> <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" android:layout_margin = "16dp" tools:context = ".MainActivity"> <EditText android:id = "@+id/etIP" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:hint = "IP-Address" android:inputType = "text" /> <EditText android:id = "@+id/etPort" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_below = "@+id/etIP" android:hint = "Port No" android:inputType = "number" /> <Push button android:id = "@+id/btnConnect" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_below = "@+id/etPort" android:layout_gravity = "center" android:layout_marginTop = "16dp" android:text = "Connect To Server" /> <TextView android:id = "@+id/tvMessages" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_above = "@+id/etMessage" android:layout_below = "@+id/btnConnect" android:inputType = "textMultiLine" android:textAppearance = "@style/Base.TextAppearance.AppCompat.Medium" /> <EditText android:id = "@+id/etMessage" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_above = "@+id/btnSend" android:hint = "Enter Message" android:inputType = "text" /> <Button android:id = "@+id/btnSend" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_alignParentBottom = "truthful" android:text = "SEND" /> </RelativeLayout>
Step 3
− Add together the post-obit code to res/layout/MainActivity.java.
package com.client.myapplication.client; import android.annotation.SuppressLint; import android.os.Package; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import coffee.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; @SuppressLint("SetTextI18n") public class MainActivity extends AppCompatActivity { Thread Thread1 = null; EditText etIP, etPort; TextView tvMessages; EditText etMessage; Button btnSend; String SERVER_IP; int SERVER_PORT; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etIP = findViewById(R.id.etIP); etPort = findViewById(R.id.etPort); tvMessages = findViewById(R.id.tvMessages); etMessage = findViewById(R.id.etMessage); btnSend = findViewById(R.id.btnSend); Push button btnConnect = findViewById(R.id.btnConnect); btnConnect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View 5) { tvMessages.setText(""); SERVER_IP = etIP.getText().toString().trim(); SERVER_PORT = Integer.parseInt(etPort.getText().toString().trim()); Thread1 = new Thread(new Thread1()); Thread1.start(); } }); btnSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String message = etMessage.getText().toString().trim(); if (!bulletin.isEmpty()) { new Thread(new Thread3(bulletin)).outset(); } } }); } private PrintWriter output; private BufferedReader input; form Thread1 implements Runnable { @Override public void run() { Socket socket; try { socket = new Socket(SERVER_IP, SERVER_PORT); output = new PrintWriter(socket.getOutputStream()); input = new BufferedReader(new InputStreamReader(socket.getInputStream())); runOnUiThread(new Runnable() { @Override public void run() { tvMessages.setText("Connected\due north"); } }); new Thread(new Thread2()).showtime(); } catch (IOException eastward) { eastward.printStackTrace(); } } } class Thread2 implements Runnable { @Override public void run() { while (true) { effort { final String message = input.readLine(); if (message ! = null) { runOnUiThread(new Runnable() { @Override public void run() { tvMessages.append("server: " + message + "\northward"); } }); } else { Thread1 = new Thread(new Thread1()); Thread1.start(); return; } } catch (IOException e) { due east.printStackTrace(); } } } } course Thread3 implements Runnable { private Cord message; Thread3(String message) { this.message = message; } @Override public void run() { output.write(message); output.affluent(); runOnUiThread(new Runnable() { @Override public void run() { tvMessages.suspend("client: " + message + "\n"); etMessage.setText(""); } }); } } }
Pace 4
− Add together the following code to androidManifest.xml
<?xml version = "1.0" encoding = "utf-eight"?> <manifest xmlns:android = "http://schemas.android.com/apk/res/android" packet = "com.client.myapplication.client"> <uses-permission android:name = "android.permission.INTERNET" /> <uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" /> <awarding android:allowBackup = "true" android:icon = "@mipmap/ic_launcher" android:label = "@cord/app_name" android:roundIcon = "@mipmap/ic_launcher_round" android:supportsRtl = "true" android:theme = "@mode/AppTheme"> <activity android:proper noun = "com.client.myapplication.customer.MainActivity" android:label = "Client"> <intent-filter> <activeness android:proper noun = "android.intent.activity.Main" /> <category android:proper noun = "android.intent.category.LAUNCHER" /> </intent-filter> </action> </application> </manifest>
Let’due south try to run your both server and client application. I assume you have connected your actual Android Mobile device with your reckoner. To run the app from android studio, open one of your project’southward action files and click Run icon from the toolbar. Select your mobile device as an selection and and then check your mobile device which will display your default screen –
Server:-
Client:-
Server:-
Click
here
to download the project lawmaking
-
Related Questions & Answers - Android scan wifi networks programmatically
- How to programmatically turn on Wifi on Android device?
- How to I connect to wifi network on Android programmatically?
- How to send data to previous activity in Android?
- How to disable wifi in android?
- How to enable wifi in android?
- How to send data back to the primary Activity in android?
- How to programmatically plow off and turn on WiFi in Kotlin?
- How to ship data from one Fragment to another Fragment in Android?
- How to Use WiFi Direct on Android?
- How to send data from 1 activity to another in Android using intent?
- How to send data from 1 activity to another in Android using bundle?
- How to send information from one activity to another in Android without intent?
- How to send data back to the Main Activity in Android using Kotlin?
- How to programmatically configure Chrome extension through Selenium WebDriver?