안드로이드 장치에서 데스크탑 응용 프로그램으로 메시지를 전송하고 싶습니다. 내 질문은 내가 인터넷 연결을 사용하지 않고 데스크톱 와이파이 장치와 안드로이드 와이파이 장치를 연결할 수 있습니다. 블루투스처럼 사용하고 싶습니다. 이것이 가능하거나 아닌가? 가능한 경우 어떻게 구현할 수 있습니까?WIFI에서 WIFI로 연결 안드로이드를 사용하여 연결
감사 및 감사 미트 Thaper
안드로이드 장치에서 데스크탑 응용 프로그램으로 메시지를 전송하고 싶습니다. 내 질문은 내가 인터넷 연결을 사용하지 않고 데스크톱 와이파이 장치와 안드로이드 와이파이 장치를 연결할 수 있습니다. 블루투스처럼 사용하고 싶습니다. 이것이 가능하거나 아닌가? 가능한 경우 어떻게 구현할 수 있습니까?WIFI에서 WIFI로 연결 안드로이드를 사용하여 연결
감사 및 감사 미트 Thaper
다음은 mreichelt의 제안을 구현 한 것입니다. 나는 똑같은 문제가있을 때 이것을 보았고 솔루션 구현을 게시 할 것이라고 생각했습니다. 정말 간단합니다. 나는 또한 안드로이드 장치 (주로 디버깅 목적으로)에서 들어오는 요청을 수신 대기하는 자바 서버를 만들었습니다. 무선을 통해 물건을 보내는 코드는 다음과 같습니다.
import java.net.*;
import java.io.*;
import java.util.*;
import android.app.Activity;
import android.content.Context;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
public class SMSConnection {
/* The socket to the server */
private Socket connection;
/* Streams for reading and writing the socket */
private BufferedReader fromServer;
private DataOutputStream toServer;
/* application context */
Context mCtx;
private static final String CRLF = "\r\n";
/* Create an SMSConnection object. Create the socket and the
associated streams. Initialize SMS connection. */
public SMSConnection(Context ctx) throws IOException {
mCtx=ctx;
this.open();
/* may anticipate problems with readers being initialized before connection is opened? */
fromServer = new BufferedReader(new InputStreamReader(connection.getInputStream()));
toServer = new DataOutputStream(connection.getOutputStream());
}
public boolean open(String host, int port) {
try {
connection = new Socket(host, port);
return true;
} catch(IOException e) {
Log.v("smswifi", "cannot open connection: " + e.toString());
}
return false;
}
/* Close the connection. */
public void close() {
try {
connection.close();
} catch (IOException e) {
Log.v("smswifi","Unable to close connection: " + e.toString());
}
}
/* Send an SMS command to the server. Check that the reply code
is what is is supposed to be according to RFC 821. */
public void sendCommand(String command) throws IOException {
/* Write command to server. */
this.toServer.writeBytes(command+this.CRLF);
/* read reply */
String reply = this.fromServer.readLine();
}
}
연결 클래스의 기본 골격입니다. 클래스를 인스턴스화하고 호스트 및 포트로 작성한 인스턴스에서 open을 호출하면 (완료되면 연결을 닫는 것을 잊지 말라) sendCommand의 본문을 원하는대로 변경할 수 있습니다. 예를 들어, 함수 몸체에 읽기/쓰기 연산을 포함 시켰습니다.
여기는 연결을 수신하고 각 요청을 처리하기 위해 스레드를 생성하는 원격 시스템에서 서버를 실행하는 코드입니다. 디버깅을 위해 위의 코드와 쉽게 상호 작용할 수 있습니다.
import java.io.*;
import java.net.*;
import java.util.*;
public final class smsd {
///////MEMBER VARIABLES
ServerSocket server=null;
Socket client=null;
///////MEMBER FUNCTIONS
public boolean createSocket(int port) {
try{
server = new ServerSocket(port);
} catch (IOException e) {
System.out.println("Could not listen on port "+port);
System.exit(-1);
}
return true;
}
public boolean listenSocket(){
try{
client = server.accept();
} catch (IOException e) {
System.out.println("Accept failed: ");
System.exit(-1);
}
return true;
}
public static void main(String argv[]) throws Exception {
//
smsd mySock=new smsd();
//establish the listen socket
mySock.createSocket(3005);
while(true) {
if(mySock.listenSocket()) {
//make new thread
// Construct an object to process the SMS request message.
SMSRequest request = new SMSRequest(mySock.client);
// Create a new thread to process the request.
Thread thread = new Thread(request);
// Start the thread.
thread.start();
}
}
//process SMS service requests in an infinite loop
}
///////////end class smsd/////////
}
final class SMSRequest implements Runnable {
//
final static String CRLF = "\r\n";
Socket socket;
// Constructor
public SMSRequest(Socket socket) throws Exception
{
this.socket = socket;
}
// Implement the run() method of the Runnable interface.
public void run()
{
try {
processRequest();
} catch (Exception e) {
System.out.println(e);
}
}
private static void sendBytes(FileInputStream fis, OutputStream os) throws Exception
{
// Construct a 1K buffer to hold bytes on their way to the socket.
byte[] buffer = new byte[1024];
int bytes = 0;
// Copy requested file into the socket's output stream.
while((bytes = fis.read(buffer)) != -1) {
os.write(buffer, 0, bytes);
}
}
private void processRequest() throws Exception
{
// Get a reference to the socket's input and output streams.
InputStream is = this.socket.getInputStream();
DataOutputStream os = new DataOutputStream(this.socket.getOutputStream());
// Set up input stream filters.
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
// Get the request line of the SMS request message.
String requestLine = br.readLine();
//print message to screen
System.out.println(requestLine);
//send a reply
os.writeBytes("200");
// Close streams and socket.
os.close();
br.close();
socket.close();
}
}
nb4namingconventions.
거의 잊어 버렸습니다. 무선을 사용하려면 AndroidManifest.xml의 태그 안에 이러한 권한을 설정해야합니다.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
양쪽 장치가 동일한 무선 네트워크를 사용하여 서로 핑 할 경우이 쉽게 가능하다. 바탕 화면에 ServerSocket
이라는 Java 응용 프로그램을 만들면됩니다. 그런 다음 데스크톱의 IP 주소를 사용하여 Socket
을 Android 앱에 열고 OutputStream
을 통해 데이터를 보낼 수 있습니다.
저는 Amit이 컴퓨터가 무선으로 서로 직접 연결되는 것을 말합니다.
현재 액세스 포인트의 플러그 - 인 설정을 허용하는 Wifi-direct 사양 개발이 있습니다. 문제는 현재 다른 컴퓨터가 연결할 수있는 AP 중 하나가 해당 컴퓨터에 있는지 확인하는 것입니다.
이것이 Ad-Hoc 네트워크와 어떤 관련이 있는지 알고 싶습니다. 나는 해결책이 없지만이 질문에도 상당히 관심이 있습니다! (이 질문을 Amit이라고 가정).
예 Eric이 정의한대로이 방법으로 연결하고 싶습니다. 대답을 명확히하는 예는 블루투스 전화입니다. 나는 어떤 라우터도 사용하지 않고 블루투스처럼 WiFi로 2 개의 장치를 페어링하기를 원한다. –
해당 질문을 읽기가 어렵습니다. 형식을 확인하십시오! –