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. Any ideas how to get started? Sorry for my bad English
Asked
Active
Viewed 2,517 times
2
-
you are attempting to build a server-client application. – TheConsultant Jul 24 '15 at 13:24
-
look on this http://stackoverflow.com/questions/20345155/android-receive-and-send-data-through-wifi-connection-to-hardware . It may help you. – Janny Jul 24 '15 at 13:24
-
Thanks, I will try that – kleiser sarifo Jul 24 '15 at 13:28
-
Ask Your Question in this Link http://stackoverflow.com/questions/10388250/how-to-send-string-from-android-to-pc-over-wifi I Hope yoursuccessful. – Mir Hussain Jul 24 '15 at 13:53
2 Answers
4
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-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
In server use :
ServerSocket serverSocket = new ServerSocket(9000);
Socket socket = serverSocket.accept();
And in client :
socket = new Socket()
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
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
BufferedReader inputStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//in server
String txt = "Hello from Server to Client\n";
outputStream.write(txt.getBytes());
//in client
String message = inputStream.readLine();
socket.close();
Server sends the text and client checks the input stream for a '\n' in it.

Pouria Ghavidel
- 96
- 1
- 12
0
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).

Marta Tenés
- 2,102
- 1
- 13
- 22