Android WiFi Direct - client/server - ECONNREFUSED(连接被拒绝)更新

Android WiFi Direct - client/server - ECONNREFUSED (Connection refused) UPDATE

我的 Android client/server 应用程序有问题。我想通过 WiFi Direct 连接设备并发送一些媒体文件。

我为客户端和服务器创建了一个 Activity 和服务。代码如下。

服务器Activity:

import java.util.ArrayList;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WpsInfo;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.ActionListener;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ServerActivity extends Activity {

    private WifiP2pManager wifiManager;
    private Channel wifichannel;
    private BroadcastReceiver wifiServerReceiver;
    private IntentFilter wifiServerReceiverIntentFilter;
    private WifiP2pConfig config;
    private String deviceName;

    private Intent intent;

    PeerListListener myPeerListListener;
    ArrayList<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();
    WifiP2pDeviceList peerList;

    TextView text;
    EditText et2;
    Button button1;
    ListView listView;
    ArrayAdapter<String> BTArrayAdapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.server_activity);

        // Block auto opening keyboard
        this.getWindow()
                .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        BTArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);

        listView = (ListView) findViewById(R.id.listView1);
        listView.setAdapter(BTArrayAdapter);

        wifiManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        wifichannel = wifiManager.initialize(this, getMainLooper(), null);

        wifiServerReceiverIntentFilter = new IntentFilter();
        ;
        wifiServerReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        wifiServerReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        wifiServerReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
        wifiServerReceiverIntentFilter
                .addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

        intent = null;

        registerReceiver(wifiServerReceiver, wifiServerReceiverIntentFilter);

        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                wifiManager.discoverPeers(wifichannel, null);
                wifiManager.requestPeers(wifichannel, new PeerListListener() {
                    @Override
                    public void onPeersAvailable(WifiP2pDeviceList peerList) {

                        peers.clear();
                        peers.addAll(peerList.getDeviceList());
                    }

                });

                for (int i = 0; i < peers.size(); i++) {
                    WifiP2pDevice device = peers.get(i);
                    deviceName = device.deviceName;
                    config = new WifiP2pConfig();
                    config.deviceAddress = device.deviceAddress;
                    config.wps.setup = WpsInfo.PBC;

                    wifiManager.connect(wifichannel, config, new ActionListener() {

                        @Override
                        public void onSuccess() {
                            Toast.makeText(
                                    getApplicationContext(),
                                    "Połączono z: " + deviceName + "\n Mac: "
                                            + config.deviceAddress, Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onFailure(int reason) {
                            Toast.makeText(getApplicationContext(), "Nie udało się połączyć",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                wifiManager.requestConnectionInfo(wifichannel,
                        new WifiP2pManager.ConnectionInfoListener() {

                            @Override
                            public void onConnectionInfoAvailable(final WifiP2pInfo info) {
                                // TODO Auto-generated method stub
                                String groupOwnerAddress = info.groupOwnerAddress.getHostAddress();

                                Toast.makeText(getApplicationContext(),
                                        "GroupOwnAddress: " + groupOwnerAddress, Toast.LENGTH_SHORT)
                                        .show();
                            }

                        });

                StartServer(null);

            }
        });
    }

    public void StartServer(View v) {
        // Construct our Intent specifying the Service

        intent = new Intent(this, ServerService.class);
        startService(intent);
    }

}

服务器服务:

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.widget.Toast;

public class ServerService extends IntentService {

    Handler mHandler;
    private int port = 2178;

    ServerSocket welcomeSocket = null;
    Socket socket = null;

    public ServerService() {
        super("ServerService");
        mHandler = new Handler();
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        try {
            mHandler.post(new DisplayToast(this, "Creating ServerSocket.."));
            welcomeSocket = new ServerSocket(port);
            welcomeSocket.setReuseAddress(true);
            mHandler.post(new DisplayToast(this, "Waiting for connection on port: "
                    + welcomeSocket.getLocalPort()));

            socket = welcomeSocket.accept();
            // while(true && flag){
            // socket = welcomeSocket.accept();
            mHandler.post(new DisplayToast(this, "Coneccted!"));

            // }

            mHandler.post(new DisplayToast(this, "Succes!"));
        } catch (IOException e) {
            mHandler.post(new DisplayToast(this, "IOException"));

        } catch (Exception e) {
            mHandler.post(new DisplayToast(this, "Exception"));

        }

        mHandler.post(new DisplayToast(this, "ServerService"));

    }

    /*
    public void onDestroy() {
        try {
            welcomeSocket.close();
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        stopSelf();
    }*/

}

class DisplayToast2 implements Runnable {
    private final Context mContext;
    String mText;

    public DisplayToast2(Context mContext, String text) {
        this.mContext = mContext;
        mText = text;
    }

    public void run() {
        Toast.makeText(mContext, mText, Toast.LENGTH_SHORT).show();
    }
}

客户Activity:

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class ClientActivity extends Activity {

    Button button;
    private WifiP2pManager wifiManager;
    private Channel wifichannel;
    private BroadcastReceiver wifiClientReceiver;

    private IntentFilter wifiClientReceiverIntentFilter;
    private WifiP2pInfo wifiInfo;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.client_activity);

        // Block auto opening keyboard
        this.getWindow()
                .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        wifiManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);

        wifichannel = wifiManager.initialize(this, getMainLooper(), null);
        wifiClientReceiver = new WiFiClientBroadcastReceiver(wifiManager, wifichannel, this);

        wifiClientReceiverIntentFilter = new IntentFilter();
        ;
        wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
        wifiClientReceiverIntentFilter
                .addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

        wifiInfo = null;

        registerReceiver(wifiClientReceiver, wifiClientReceiverIntentFilter);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                StartClient(null);

            }
        });

    }

    public void setNetworkToReadyState(boolean status, WifiP2pInfo info, WifiP2pDevice device) {
        wifiInfo = info;
        // targetDevice = device;
        // connectedAndReadyToSendFile = status;
    }

    public void StartClient(View v) {

        Intent intent = new Intent(this, ClientService.class);
        intent.putExtra("wifiInfo", wifiInfo);

        if (wifiInfo == null) {
            Toast.makeText(this, "WifiInfo = null!", Toast.LENGTH_SHORT).show();
        }
        startService(intent);

    }

}

客户端服务:

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.p2p.WifiP2pInfo;
import android.os.Handler;
import android.widget.Toast;

public class ClientService extends IntentService {

    Handler mHandler;
    private WifiP2pInfo wifiInfo;
    private int port = 2178;

    Socket clientSocket = null;
    OutputStream os = null;

    public ClientService() {
        super("ClientService");
        mHandler = new Handler();

    }

    @Override
    protected void onHandleIntent(Intent intent) {

        wifiInfo = (WifiP2pInfo) intent.getExtras().get("wifiInfo");

        InetAddress targetIP = wifiInfo.groupOwnerAddress;
        clientSocket = new Socket();

        if(wifiInfo.isGroupOwner)
        {
            try {
                mHandler.post(new DisplayToast(this, "Try to connect: /N IP: " + targetIP + "/nPort: " +port));

                clientSocket.connect(new InetSocketAddress(targetIP, port));

                mHandler.post(new DisplayToast(this, "Connected!"));

                //os = clientSocket.getOutputStream();

            } catch (IOException e) {
                mHandler.post(new DisplayToast(this, "serwer IOException: " + e.getMessage()));

            } catch (Exception e) {
                mHandler.post(new DisplayToast(this, "serwer Exception"));

            }
        }else{
            mHandler.post(new DisplayToast(this, "Group owner = " + wifiInfo.isGroupOwner));
        }


    }

    /*
    public void onDestroy()
    {

        try {
            os.close();
            clientSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        stopSelf();
    }*/
}

class DisplayToast implements Runnable {
    private final Context mContext;
    String mText;

    public DisplayToast(Context mContext, String text) {
        this.mContext = mContext;
        mText = text;
    }

    public void run() {
        Toast.makeText(mContext, mText, Toast.LENGTH_SHORT).show();
    }
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".AppModeSelection"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".ServerService"
            android:exported="false" />
        <service
            android:name=".ClientService"
            android:exported="false" />

        <activity android:name=".ServerActivity" >
        </activity>
        <activity android:name=".ClientActivity" >
        </activity>
    </application>

</manifest>

我正在使用两部支持 WiFi Direct 的手机(Samsung GT-I9505 Android 4.4.2 和 Samsung GT-I8190N Adroid 4.1.2)。首先,我关闭了所有连接(BT、WIFI),然后通过 WiFi Direct 连接。 GT-I9505 是服务器,另一台是客户端。建立连接时出现 IOException 错误:

IOException:无法连接到/192.168.49.1(端口 2178):连接失败:ECONNREFUSED(连接被拒绝)

我尝试使用很多其他 Pors,但没有任何效果。我做错了什么?

提前感谢您的帮助!

更新:

我注意到,当我直接断开 WiFi 连接并再次连接时,但在其他方向上它可以工作,但只是暂时的。

你的代码的主要问题是你确实需要让它成为事件驱动的,在当前状态下我很惊讶它甚至真的做了任何事情。

因此,请尝试将其制作成如下所示: 1. 开始 Discovering peers,一旦你得到 Peers Changed 事件,然后做 requestPeers 2. 如果您需要一些侦听器等在另一端为您做好准备,那么您可能会通过广告服务来识别这些设备,因此接下来开始发现服务。 3. 发现服务后,启动 5 秒计时器等待任何其他服务,在每次发现服务后重置计时器,一旦计时器最终达到 运行 不间断的 5 秒,您可能已经发现了所有可用的服务那时候。 4. Select 一项服务并连接到该设备(你真的不能有多个连接)。 5. 获得已连接事件后,使用 requestConnectionInfo 获取连接信息。

那么请记住,在开始之前,您需要创建本地服务,以便对方可以看到它。然后,如果您的设备被选为组所有者,您可以继续播放广告,让更多客户端连接到您。但是如果你被选为Client,那么你就不能有传入的连接,所以你应该去掉广告。