Send Text From Android To Pc Via Wi-fi Connection
I'm new to android programming and stackoverflow. I want to create an app that sends some info (like a text) to a PC on the same network (Wi-fi) and read on the PC using a Java app
Solution 1:
You should use wi-fi manager in client and server programs and set wifi direct between PC and Android.
For the permissions use this:
<uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE" /><uses-permissionandroid:name="android.permission.INTERNET" />
In server use :
ServerSocketserverSocket=newServerSocket(9000);
Socketsocket= serverSocket.accept();
And in client :
socket = newSocket()
socket.connect("192.168.49.(Server Device wi-fi IP(zero to 255))" , 9000);
Then use these methods in both programs for send-receive data
DataOutputStreamoutputStream=newDataOutputStream(socket.getOutputStream());
BufferedReaderinputStream=newBufferedReader(newInputStreamReader(socket.getInputStream()));
//in serverStringtxt="Hello from Server to Client\n";
outputStream.write(txt.getBytes());
//in client Stringmessage= inputStream.readLine();
socket.close();
Server sends the text and client checks the input stream for a '\n' in it.
Solution 2:
As user5001333 said, you must build a server-client pattern using sockets, for example.
In Android you can't perform network operations on the main thread so you have to create a background thread (like asynctask) which establish the connection between you (client) and the pc (server).
Post a Comment for "Send Text From Android To Pc Via Wi-fi Connection"