0

我的应用程序中有 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();
        }

        }


    }
};

`

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

4

1 回答 1

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>

& 确保网络活动是在主线程以外的其他线程上完成的。此外,您还必须BroadcastReceiver适当地注册和注销。不要忘记在不需要时取消注册。

于 2014-04-21T09:41:08.030 回答