12

Androidデバイスからデスクトップアプリケーションにメッセージを転送したい。私の質問は、インターネット接続を使用せずにAndroidWiFiデバイスをデスクトップWiFiデバイスに接続できるかどうかです。Bluetoothと同じように使いたいです。これは可能かどうか?可能であれば、どうすれば実装できますか?

よろしくお願いしますAmitThaper

4

3 に答える 3

15

これがmreicheltの提案の実装です。同じ問題が発生したときにこれを調べて、ソリューションの実装を投稿するだけだと思いました。とても簡単です。また、Androidデバイスからの着信要求をリッスンするJavaサーバーを構築しました(主にデバッグ目的で)。ワイヤレスで送信するコードは次のとおりです。

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" />
于 2010-12-09T08:20:57.500 に答える
2

これは、両方のデバイスが同じWi-Fiネットワークを使用していて、相互にpingを実行できる場合に簡単に可能です。デスクトップにJavaアプリケーションを作成するだけで、を作成できますServerSocket。次にSocket、デスクトップのIPアドレスを使用してAndroidアプリでを開き、を介してデータを送信できますOutputStream

于 2010-11-24T11:20:03.393 に答える
2

アミットは、マシンをワイヤレスで直接接続することを指していると思います。

アクセスポイントのプラグアンドプレイセットアップを可能にするWifi-direct仕様の現在の開発があります。現在の問題は、マシンの1つが他のマシンが接続を確立できるAPであることを確認することです。

これがアドホックネットワークとどのように関連しているかに興味があります。私には解決策がありませんが、私もこの質問に非常に興味があります!(これがあなたの質問Amitであると仮定します)。

于 2010-12-17T15:10:35.837 に答える