Reputation: 312
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
Upvotes: 2
Views: 2747
Reputation: 96
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.
Upvotes: 4
Reputation: 2100
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).
Upvotes: 0