Android WiFi Direct - 客户端/服务器 - ECONNREFUSED(连接被拒绝)更新

时间:2015-05-24 14:08:34

标签: java android client-server android-wifi wifi-direct

我的Android客户端/服务器应用有问题。我想通过WiFi Direct连接设备并通过一些媒体文件发送。

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

ServerActivity:

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);
    }

}

Server服务:

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();
    }
}

ClientActivity:

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);

    }

}

ClientService:

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 个答案:

答案 0 :(得分:1)

你的代码的主要问题是你需要让它成为事件驱动的,在当前的状态下,我甚至对它做了什么都感到惊讶。

所以,请尝试使用以下内容: 1.开始发现同行,一旦获得Peers Changed事件,然后执行requestPeers 2.如果你需要一些听众等为你做好准备,那么你可能会通过宣传服务来识别这些设备,所以下一步看一下服务的发现。 3.发现服务后,启动5秒计时器等待任何其他计时,在发现每个服务后重置计时器,一旦计时器最终运行不间断5秒,您可能已经发现当时可用的所有服务。 4.选择ONE服务并连接到该设备(您不能真正拥有多个连接)。 5.一旦获得连接事件,请使用requestConnectionInfo获取连接信息。

然后请记住,在开始之前,您需要创建本地服务,以便对方可以看到它。然后,如果您的设备被选为群组所有者,您可以保持广告,并让更多客户端连接到您。但如果您被选为客户端,那么您就无法拥有传入连接,因此您应该删除广告。