kleiser sarifo kleiser sarifo - 7 days ago 6
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

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()));

socket.close();

! Then For send text to Server !

In Client :

String txt = "Hello from Client to Server\n";           
outputStream.write(txt.getBytes());

And in Server :

String message = inputStream.readLine();

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

Comments