点击一键即可连接Wifi连接和套接字客户端

时间:2014-04-20 17:16:44

标签: android sockets wifi android-wifi tclientsocket

我的应用中有2个按钮。

  

按钮1:连接wifi   按钮2:创建客户端套接字

使用单独的2个按钮可以使用此功能。

void WifiConnect() { String networkSSID = "HI-LINK_DA79"; String networkPass = "12345678"; WifiConfiguration conf = new WifiConfiguration(); conf.SSID = "\"" + networkSSID + "\""; //ssid must be in quotes conf.preSharedKey = "\""+ networkPass +"\"";

    WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE); 
    int  r1 =-1;
     r1= wifiManager.addNetwork(conf);
     Log.d("Client", "add Network returned " + r1 );

    boolean b = wifiManager.enableNetwork(r1, true);        
    Log.d("Client", "enableNetwork returned " + b );

    boolean d= wifiManager.reconnect();
   Log.d("Client", "wifiManager.reconnect() returned " + d )


}

`Button.OnClickListener buttonWifiConnectOnClickListener = new Button.OnClickListener(){

    public void onClick(View v) {

    WifiConnect();


    }
};

Button.OnClickListener buttonConnectOnClickListener = new Button.OnClickListener(){

    public void onClick(View v) {

        if (socket == null) { 
            Log.i("Client", "socket():Creating --");
            new Thread(new ClientThread()).start();
            //connect.setText("Disconnect");
        }
        else
        {
        try {
            socket.close();
            socket=null;
            Log.i("Client", "socket():closed --");
        //  connect.setText("Connect");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        }


    }
};

`

但是,如果我只需点击一下按钮就可以将它们组合起来 `Button.OnClickListener buttonConnectOnClickListener = new Button.OnClickListener(){

    public void onClick(View v) {

        WifiConnect();    //WiFi connect

        if (socket == null) { 
            Log.i("Client", "socket():Creating --");
            new Thread(new ClientThread()).start();
            //connect.setText("Disconnect");
        }
        else
        {
        try {
            socket.close();
            socket=null;
            Log.i("Client", "socket():closed --");
        //  connect.setText("Connect");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        }


    }
};

`

看起来我创建套接字的线程不是在这里调用的,不知道为什么。

1 个答案:

答案 0 :(得分:0)

如果您希望两个操作都在单击按钮上工作,则添加BroadcastReceiver以收听Wifi连接状态,如下所示。 &安培;在onReceive()方法中执行套接字连接。单击同一按钮时,您的套接字连接无法正常工作,因为Wifi连接需要时间和时间。没有第一个Wifi连接正在创建套接字连接,所以它可能会抛出异常。

检查Wi-Fi是否已启用 ,这并不一定意味着已连接。这只意味着手机上的Wi-Fi模式已启用,并且能够连接到Wi-Fi网络。

这是在广播接收器中侦听实际Wi-Fi连接的方式:

public class WifiReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {     
        ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
        NetworkInfo netInfo = conMan.getActiveNetworkInfo();
        if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            Log.d("WifiReceiver", "Have Wifi Connection");
                    //If connected do socket connection here
            }
        else
            Log.d("WifiReceiver", "Don't have Wifi Connection");    
    }   
};

要访问活动网络信息,您需要为AndroidManifest.xml添加以下用户权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

以下意图接收器(或者您可以通过编程方式添加...)

<!-- Receive Wi-Fi connection state changes -->
<receiver android:name=".WifiReceiver">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

&安培;确保网络活动是在除主线程之外的其他线程上完成的。你还必须注册&amp;适当取消注册BroadcastReceiver。不要忘记在不需要时取消注册。