Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
466 views
Welcome To Ask or Share your Answers For Others

1 Answer

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
";           
outputStream.write(txt.getBytes());

//in client 
String message = inputStream.readLine();

socket.close();

Server sends the text and client checks the input stream for a ' ' in it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...