kleiser sarifo kleiser sarifo - 8 months ago 48
Android Question

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. Any ideas how to get started? Sorry for my bad English

Answer Source

You should use wi-fi manager in client and server programs.

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)" , 9000);

Then use these methods in both programs for send-receive data

DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
BufferedReader inputStream = new BufferedReader(newInputStreamReader(socket.getInputStream()));


! Then For send text to Server !

In Client :

String txt = "Hello from Client to Server\n";           

And in Server :

String message = inputStream.readLine();

Client send text, in Server inputStream checking text until read '\n' in text.