0

对于 android 4.2,如果需要不支持并发连接并且从代码中我想在启用 wifi direct 后禁用 WIFI。

我可以使用如下代码(在上一个问题中找到)来做到这一点:

WifiManager wifiManager  = (WifiManager)this.getSystemService(this.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);        //True - to enable WIFI connectivity .
                                         //False -disable WIFI connectivity.
4

2 回答 2

0

It isn't possible!

Wifi Direct only find peers, connect and share information, when WIFI connection is ON and is the same for all devices.

于 2013-11-07T19:54:48.340 回答
0

试试这个代码

Button btnWiFi = (Button)findViewById(R.id.btnWiFi);

        btnWiFi.setOnClickListener(new OnClickListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                WifiManager wifi =(WifiManager)getSystemService(Context.WIFI_SERVICE);
                if(wifi.isWifiEnabled())
                {
                    Toast.makeText(getBaseContext(), "WiFi is Enabled", Toast.LENGTH_LONG).show();

                }

                else
                {
                     AlertDialog builder = new AlertDialog.Builder(MainActivity.this).create();
                     builder.setTitle("Caution");
                     builder.setMessage("WiFi is Disabled."+"\n"+"Press Ok To Turn On WiFi.");
                     builder.setIcon(R.drawable.caution);
                     builder.setButton("OK", new DialogInterface.OnClickListener() {
                         public void onClick(final DialogInterface dialog, final int which) {
                         // Write your code here to execute after dialog closed
                             final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                             wifi.setWifiEnabled(true);


                         }
                 });

                     builder.setButton2("Cancel", new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog, int which) {

                         }
                 });

                     builder.show();
                }


            }
        });
于 2013-07-24T12:28:32.183 回答