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

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

java - 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

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...